Skip to content

Commit bdba671

Browse files
Merge pull request #708 from senthil-athiban/feat/add-debounce-mechanism
[FEAT]: add lodash debounce mechanism in search_bar
2 parents 8808bfa + 0ea46e0 commit bdba671

File tree

3 files changed

+23
-12
lines changed

3 files changed

+23
-12
lines changed

package-lock.json

Lines changed: 2 additions & 0 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
"@types/react-redux": "^7.1.33",
@@ -114,5 +115,8 @@
114115
},
115116
"publishConfig": {
116117
"access": "public"
118+
},
119+
"dependencies": {
120+
"lodash": "^4.17.21"
117121
}
118122
}

src/custom/SearchBar.tsx

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
/* eslint-disable react-hooks/exhaustive-deps */
12
import { outlinedInputClasses } from '@mui/material/OutlinedInput';
23
import { Theme, ThemeProvider, createTheme, useTheme } from '@mui/material/styles';
3-
import React from 'react';
4+
import debounce from 'lodash/debounce';
5+
import React, { useCallback } from 'react';
46
import { ClickAwayListener } from '../base/ClickAwayListener';
57
import { TextField } from '../base/TextField';
68
import { CloseIcon, SearchIcon } from '../icons';
@@ -75,11 +77,22 @@ function SearchBar({
7577
const searchRef = React.useRef<HTMLInputElement | null>(null);
7678
const theme = useTheme();
7779

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

8294
const handleClearIconClick = (): void => {
95+
debouncedOnSearch('');
8396
setSearchText('');
8497
setExpanded(false);
8598
if (onClear) {
@@ -89,6 +102,7 @@ function SearchBar({
89102

90103
const handleSearchIconClick = (): void => {
91104
if (expanded) {
105+
debouncedOnSearch('');
92106
setSearchText('');
93107
setExpanded(false);
94108
} else {
@@ -101,12 +115,6 @@ function SearchBar({
101115
}
102116
};
103117

104-
// const handleClickAway = (): void => {
105-
// if (expanded) {
106-
// setExpanded(false);
107-
// }
108-
// };
109-
110118
return (
111119
<ClickAwayListener
112120
onClickAway={(event) => {
@@ -125,10 +133,7 @@ function SearchBar({
125133
<TextField
126134
variant="standard"
127135
value={searchText}
128-
onChange={(e: React.ChangeEvent<HTMLInputElement>) => {
129-
handleSearchChange(e);
130-
onSearch(e.target.value);
131-
}}
136+
onChange={handleSearchChange} // Updated to use the new handler
132137
inputRef={searchRef}
133138
placeholder={placeholder}
134139
style={{

0 commit comments

Comments
 (0)