Skip to content

Commit

Permalink
feat: add decounce for searching text
Browse files Browse the repository at this point in the history
* fix: Esc to close searchFile input

* feat: add 'Escape' to close search text and click background to close search file

* fix: search text input css

* feat: add search text debounce
  • Loading branch information
MatthewChenShow authored Apr 15, 2024
1 parent 0cfae1e commit 2c8b836
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/components/searchtext/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ interface SelectedRow {
type SearchResultType = Record<string, { code: string; line: number }[]>[];

const SearchAndReplace: React.FC<SearchAndReplaceProps> = ({onSelectedLine, listFiles, style, onClose}) => {
const [searchText, setSearchText] = useState('');
const [searchText, setSearchText] = useState('');
const [resultText, setResultText] = useState('');
const [searchResults, setSearchResults] = useState<SearchResultType>([]);
const [unExpandedTitles, setUnExpandedTitles] = useState<Record<number, boolean>>({});
const [selectedRow, setSelectedRow] = useState<SelectedRow>({ titleIndex: -1, rowIndex: -1 });
Expand Down Expand Up @@ -108,6 +109,14 @@ const SearchAndReplace: React.FC<SearchAndReplaceProps> = ({onSelectedLine, list
}
}, [selectedRow, allSelectResults]);

useEffect(() => {
const delayDebounceFn = setTimeout(() => {
setResultText(searchText);
}, 500);

return () => clearTimeout(delayDebounceFn);
}, [searchText]);

useEffect(() => {
const current = innerRef?.current as unknown as HTMLElement;
if (current) {
Expand Down Expand Up @@ -161,7 +170,7 @@ const SearchAndReplace: React.FC<SearchAndReplaceProps> = ({onSelectedLine, list
<SearchResult
searchResults={searchResults}
unExpandedTitles={unExpandedTitles}
searchText={searchText}
searchText={resultText}
selectedRow={selectedRow}
handleRowSelection={handleRowSelection}
toggleExpand={toggleExpand}
Expand Down

0 comments on commit 2c8b836

Please sign in to comment.