Skip to content

Commit

Permalink
Merge pull request #708 from senthil-athiban/feat/add-debounce-mechanism
Browse files Browse the repository at this point in the history
[FEAT]: add lodash debounce mechanism in search_bar
  • Loading branch information
sudhanshutech committed Aug 27, 2024
2 parents 8808bfa + 0ea46e0 commit bdba671
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 12 deletions.
2 changes: 2 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"@reduxjs/toolkit": "^2.2.5",
"@testing-library/react": "^14.1.2",
"@types/jest": "^29.5.11",
"@types/lodash": "^4.17.7",
"@types/react": "^18.2.45",
"@types/react-dom": "^18.2.18",
"@types/react-redux": "^7.1.33",
Expand Down Expand Up @@ -114,5 +115,8 @@
},
"publishConfig": {
"access": "public"
},
"dependencies": {
"lodash": "^4.17.21"
}
}
29 changes: 17 additions & 12 deletions src/custom/SearchBar.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
/* eslint-disable react-hooks/exhaustive-deps */
import { outlinedInputClasses } from '@mui/material/OutlinedInput';
import { Theme, ThemeProvider, createTheme, useTheme } from '@mui/material/styles';
import React from 'react';
import debounce from 'lodash/debounce';
import React, { useCallback } from 'react';
import { ClickAwayListener } from '../base/ClickAwayListener';
import { TextField } from '../base/TextField';
import { CloseIcon, SearchIcon } from '../icons';
Expand Down Expand Up @@ -75,11 +77,22 @@ function SearchBar({
const searchRef = React.useRef<HTMLInputElement | null>(null);
const theme = useTheme();

// Debounce the onSearch function
const debouncedOnSearch = useCallback(
debounce((value) => {
onSearch(value);
}, 300),
[onSearch]
);

const handleSearchChange = (event: React.ChangeEvent<HTMLInputElement>): void => {
setSearchText(event.target.value);
const value = event.target.value;
setSearchText(value);
debouncedOnSearch(value);
};

const handleClearIconClick = (): void => {
debouncedOnSearch('');
setSearchText('');
setExpanded(false);
if (onClear) {
Expand All @@ -89,6 +102,7 @@ function SearchBar({

const handleSearchIconClick = (): void => {
if (expanded) {
debouncedOnSearch('');
setSearchText('');
setExpanded(false);
} else {
Expand All @@ -101,12 +115,6 @@ function SearchBar({
}
};

// const handleClickAway = (): void => {
// if (expanded) {
// setExpanded(false);
// }
// };

return (
<ClickAwayListener
onClickAway={(event) => {
Expand All @@ -125,10 +133,7 @@ function SearchBar({
<TextField
variant="standard"
value={searchText}
onChange={(e: React.ChangeEvent<HTMLInputElement>) => {
handleSearchChange(e);
onSearch(e.target.value);
}}
onChange={handleSearchChange} // Updated to use the new handler
inputRef={searchRef}
placeholder={placeholder}
style={{
Expand Down

0 comments on commit bdba671

Please sign in to comment.