11import { outlinedInputClasses } from '@mui/material/OutlinedInput' ;
22import { 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' ;
45import { ClickAwayListener } from '../base/ClickAwayListener' ;
56import { TextField } from '../base/TextField' ;
67import { 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