File tree Expand file tree Collapse file tree
src/custom/StyledSearchBar Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11import { SxProps , Theme , useTheme } from '@mui/material' ;
2- import React from 'react' ;
2+ import { debounce } from 'lodash' ;
3+ import React , { useCallback , useState } from 'react' ;
34import { InputAdornment } from '../../base' ;
45import { SearchIcon } from '../../icons' ;
56import { InputAdornmentEnd , StyledSearchInput } from './style' ;
@@ -36,14 +37,31 @@ function StyledSearchBar({
3637 endAdornment
3738} : SearchBarProps ) : JSX . Element {
3839 const theme = useTheme ( ) ;
40+ const [ inputValue , setInputValue ] = useState ( value ) ;
41+
42+ const debouncedOnChange = useCallback (
43+ ( event : React . ChangeEvent < HTMLInputElement > ) => {
44+ debounce ( ( ) => {
45+ if ( onChange ) {
46+ onChange ( event ) ;
47+ }
48+ } , 300 ) ( ) ;
49+ } ,
50+ [ onChange ]
51+ ) ;
52+
53+ const handleChange = ( event : React . ChangeEvent < HTMLInputElement > ) => {
54+ setInputValue ( event . target . value ) ;
55+ debouncedOnChange ( event ) ;
56+ } ;
3957
4058 return (
4159 < StyledSearchInput
4260 type = "search"
4361 label = { label }
4462 fullWidth
45- value = { value }
46- onChange = { onChange }
63+ value = { inputValue }
64+ onChange = { handleChange }
4765 sx = { sx }
4866 placeholder = { placeholder ?? 'Search' }
4967 startAdornment = {
You can’t perform that action at this time.
0 commit comments