Skip to content

Commit a88ff1d

Browse files
feat(searchbar): add lodash debounce mechanism in searchbar
Signed-off-by: Senthil Athiban <[email protected]>
1 parent 7755428 commit a88ff1d

File tree

3 files changed

+38
-16
lines changed

3 files changed

+38
-16
lines changed

package-lock.json

Lines changed: 18 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
"@reduxjs/toolkit": "^2.2.5",
4747
"@testing-library/react": "^14.1.2",
4848
"@types/jest": "^29.5.11",
49+
"@types/lodash": "^4.17.7",
4950
"@types/react": "^18.2.45",
5051
"@types/react-dom": "^18.2.18",
5152
"@typescript-eslint/eslint-plugin": "^6.12.0",
@@ -106,5 +107,8 @@
106107
},
107108
"publishConfig": {
108109
"access": "public"
110+
},
111+
"dependencies": {
112+
"lodash": "^4.17.21"
109113
}
110114
}

src/custom/SearchBar.tsx

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { outlinedInputClasses } from '@mui/material/OutlinedInput';
22
import { Theme, ThemeProvider, createTheme, useTheme } from '@mui/material/styles';
3-
import React from 'react';
3+
import debounce from 'lodash/debounce';
4+
import React, { useCallback } from 'react';
45
import { ClickAwayListener } from '../base/ClickAwayListener';
56
import { TextField } from '../base/TextField';
67
import { CloseIcon, SearchIcon } from '../icons';
@@ -75,11 +76,22 @@ function SearchBar({
7576
const searchRef = React.useRef<HTMLInputElement | null>(null);
7677
const theme = useTheme();
7778

79+
// Debounce the onSearch function
80+
const debouncedOnSearch = useCallback(
81+
debounce((value) => {
82+
onSearch(value);
83+
}, 300),
84+
[onSearch]
85+
);
86+
7887
const handleSearchChange = (event: React.ChangeEvent<HTMLInputElement>): void => {
79-
setSearchText(event.target.value);
88+
const value = event.target.value;
89+
setSearchText(value);
90+
debouncedOnSearch(value);
8091
};
8192

8293
const handleClearIconClick = (): void => {
94+
debouncedOnSearch('');
8395
setSearchText('');
8496
setExpanded(false);
8597
if (onClear) {
@@ -89,6 +101,7 @@ function SearchBar({
89101

90102
const handleSearchIconClick = (): void => {
91103
if (expanded) {
104+
debouncedOnSearch('');
92105
setSearchText('');
93106
setExpanded(false);
94107
} else {
@@ -101,12 +114,6 @@ function SearchBar({
101114
}
102115
};
103116

104-
// const handleClickAway = (): void => {
105-
// if (expanded) {
106-
// setExpanded(false);
107-
// }
108-
// };
109-
110117
return (
111118
<ClickAwayListener
112119
onClickAway={(event) => {
@@ -125,10 +132,7 @@ function SearchBar({
125132
<TextField
126133
variant="standard"
127134
value={searchText}
128-
onChange={(e: React.ChangeEvent<HTMLInputElement>) => {
129-
handleSearchChange(e);
130-
onSearch(e.target.value);
131-
}}
135+
onChange={handleSearchChange} // Updated to use the new handler
132136
inputRef={searchRef}
133137
placeholder={placeholder}
134138
style={{

0 commit comments

Comments
 (0)