11import {
22 type ChangeEvent ,
33 type MouseEvent ,
4+ SyntheticEvent ,
45 useCallback ,
56 useEffect ,
67 useRef ,
78 useState ,
89} from 'react' ;
9- import Autocomplete from '@mui/material/Autocomplete' ;
10+ import Autocomplete , { AutocompleteInputChangeReason } from '@mui/material/Autocomplete' ;
1011import Box from '@mui/material/Box' ;
1112import Checkbox from '@mui/material/Checkbox' ;
1213import Chip from '@mui/material/Chip' ;
@@ -143,11 +144,13 @@ export const MRT_FilterTextField = <TData extends MRT_RowData>({
143144 ? ( column . getFilterValue ( ) as [ string , string ] ) ?. [
144145 rangeFilterIndex as number
145146 ] || ''
147+ : isAutocompleteFilter
148+ ? typeof column . getFilterValue ( ) === 'string' ? column . getFilterValue ( ) as string : ''
146149 : ( ( column . getFilterValue ( ) as string ) ?? '' ) ,
147150 ) ;
148151 const [ autocompleteValue , setAutocompleteValue ] =
149152 useState < DropdownOption | null > (
150- isAutocompleteFilter ? ( filterValue as DropdownOption | null ) : null ,
153+ ( ) => isAutocompleteFilter ? ( ( column . getFilterValue ( ) || null ) as DropdownOption | null ) : null ,
151154 ) ;
152155
153156 const handleChangeDebounced = useCallback (
@@ -184,9 +187,13 @@ export const MRT_FilterTextField = <TData extends MRT_RowData>({
184187 textFieldProps ?. onChange ?.( event ) ;
185188 } ;
186189
190+ const handleAutocompleteInputChange = ( _event : SyntheticEvent , newValue : string , _reason : AutocompleteInputChangeReason ) => {
191+ handleChange ( newValue )
192+ } ;
193+
187194 const handleAutocompleteChange = ( newValue : DropdownOption | null ) => {
188195 setAutocompleteValue ( newValue ) ;
189- handleChange ( getValueAndLabel ( newValue ) . value ) ;
196+ handleChangeDebounced ( getValueAndLabel ( newValue ) . value ) ;
190197 } ;
191198
192199 const handleClear = ( ) => {
@@ -200,6 +207,12 @@ export const MRT_FilterTextField = <TData extends MRT_RowData>({
200207 newFilterValues [ rangeFilterIndex as number ] = undefined ;
201208 return newFilterValues ;
202209 } ) ;
210+ } else if ( isAutocompleteFilter ) {
211+ setAutocompleteValue ( null )
212+ setFilterValue ( '' )
213+ // when using 'autocomplete' this function is called only inside effect and only if the filterValue === undefined
214+ // so the following call is excessive; should be uncommented if the handleClear becomes part of the Autocomplete component callbacks
215+ // column.setFilterValue(undefined)
203216 } else {
204217 setFilterValue ( '' ) ;
205218 column . setFilterValue ( undefined ) ;
@@ -434,6 +447,8 @@ export const MRT_FilterTextField = <TData extends MRT_RowData>({
434447 options = {
435448 dropdownOptions ?. map ( ( option ) => getValueAndLabel ( option ) ) ?? [ ]
436449 }
450+ inputValue = { filterValue as string }
451+ onInputChange = { handleAutocompleteInputChange }
437452 { ...autocompleteProps }
438453 renderInput = { ( builtinTextFieldProps : TextFieldProps ) => (
439454 < TextField
@@ -455,7 +470,6 @@ export const MRT_FilterTextField = <TData extends MRT_RowData>({
455470 ...commonTextFieldProps ?. slotProps ?. htmlInput ,
456471 } ,
457472 } }
458- onChange = { handleTextFieldChange }
459473 onClick = { ( e : MouseEvent < HTMLInputElement > ) => e . stopPropagation ( ) }
460474 />
461475 ) }
0 commit comments