Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: allow searchbox to not autoload and use enter instead #1468

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/i18n/en_US.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export default {
search: {
placeholder: 'Type a keyword...',
button: 'Search',
},
sort: {
sortAsc: 'Sort column ascending',
Expand Down
8 changes: 8 additions & 0 deletions src/theme/mermaid/search.scss
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
@import 'colors';

.gridjs {
&-search {
float: left;

&-input {
width: 250px;
}
button, select {
padding: 10px 14px;
border: 1px solid $gray4;
background-color: $white;
border-radius: 6px
}
}
}
50 changes: 44 additions & 6 deletions src/view/plugin/search/search.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { classJoin, className } from '../../../util/className';
import ServerGlobalSearchFilter from '../../../pipeline/filter/serverGlobalSearch';
import { TCell } from '../../../types';
import { useConfig } from '../../../hooks/useConfig';
import { useCallback, useEffect, useState } from 'preact/hooks';
import { useCallback, useEffect, useRef, useState } from 'preact/hooks';
import { useTranslator } from '../../../i18n/language';
import * as actions from './actions';
import { useStore } from '../../../hooks/useStore';
Expand All @@ -15,6 +15,7 @@ export interface SearchConfig {
keyword?: string;
ignoreHiddenColumns?: boolean;
debounceTimeout?: number;
showSearchButton?: boolean;
selector?: (cell: TCell, rowIndex: number, cellIndex: number) => string;
server?: {
url?: (prevUrl: string, keyword: string) => string;
Expand All @@ -31,6 +32,7 @@ export function Search() {
const _ = useTranslator();
const { dispatch } = useStore();
const state = useSelector((state) => state.search);
const inputRef = useRef<HTMLInputElement>(null);

useEffect(() => {
if (!processor) return;
Expand Down Expand Up @@ -74,30 +76,66 @@ export function Search() {
return () => config.pipeline.unregister<object, object>(processor);
}, [config, processor]);

const performSearch = (keyword: string) => {
dispatch(actions.SearchKeyword(keyword));
};

// Method to handle debounced input
const debouncedOnInput = useCallback(
debounce(
(event: JSX.TargetedEvent<HTMLInputElement>) => {
if (props.debounceTimeout < 0) {
return;
}
if (event.target instanceof HTMLInputElement) {
dispatch(actions.SearchKeyword(event.target.value));
performSearch(event.target.value);
}
},
processor instanceof ServerGlobalSearchFilter
? props.debounceTimeout || 250
: 0,
processor instanceof ServerGlobalSearchFilter ? props.debounceTimeout || 250 : 0,
),
[props, processor],
);

// Method to handle keydown event for Enter key
const handleKeyDown = (event: JSX.TargetedKeyboardEvent<HTMLInputElement>) => {
if (event.key === 'Enter') {
if (event.target instanceof HTMLInputElement) {
performSearch(event.target.value);
}
}
};

// Method to handle search button click
const handleSearchClick = () => {
if (inputRef.current) {
performSearch(inputRef.current.value);
}
};


return (
<div className={className(classJoin('search', config.className?.search))}>
<div className={classJoin(className('search'), config.className?.search)}>
<input
type="search"
ref={inputRef}
placeholder={_('search.placeholder')}
aria-label={_('search.placeholder')}
onInput={debouncedOnInput}
onKeyDown={handleKeyDown}
className={classJoin(className('input'), className('search', 'input'))}
defaultValue={state?.keyword || ''}
/>
{props.showSearchButton && (
<button
className={classJoin(
className('button'),
className('search', 'button'),
)}
onClick={handleSearchClick}
>
{_('search.button')}
</button>
)}
</div>
);
}