1+ /* eslint-disable react-hooks/exhaustive-deps */
12import { outlinedInputClasses } from '@mui/material/OutlinedInput' ;
23import { 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' ;
46import { ClickAwayListener } from '../base/ClickAwayListener' ;
57import { TextField } from '../base/TextField' ;
68import { 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