Skip to content

Commit

Permalink
fix: Filters - Clearing autocomplete filter using "Clear Filter" rath…
Browse files Browse the repository at this point in the history
…er than the 'X' button doesn't clear the selected value.

#1100
  • Loading branch information
avightclav authored and avightclav committed Jan 2, 2025
1 parent 3dbab32 commit 7ea078b
Showing 1 changed file with 17 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import {
type ChangeEvent,
type MouseEvent,
SyntheticEvent,
useCallback,
useEffect,
useRef,
useState,
} from 'react';
import Autocomplete from '@mui/material/Autocomplete';
import Autocomplete, { AutocompleteInputChangeReason } from '@mui/material/Autocomplete';
import Box from '@mui/material/Box';
import Checkbox from '@mui/material/Checkbox';
import Chip from '@mui/material/Chip';
Expand Down Expand Up @@ -143,6 +144,8 @@ export const MRT_FilterTextField = <TData extends MRT_RowData>({
? (column.getFilterValue() as [string, string])?.[
rangeFilterIndex as number
] || ''
: isAutocompleteFilter
? typeof column.getFilterValue() === 'string' ? column.getFilterValue() as string : ''
: ((column.getFilterValue() as string) ?? ''),
);
const [autocompleteValue, setAutocompleteValue] =
Expand Down Expand Up @@ -184,9 +187,13 @@ export const MRT_FilterTextField = <TData extends MRT_RowData>({
textFieldProps?.onChange?.(event);
};

const handleAutocompleteInputChange = (_event: SyntheticEvent, newValue: string, _reason: AutocompleteInputChangeReason) => {
handleChange(newValue)
};

const handleAutocompleteChange = (newValue: DropdownOption | null) => {
setAutocompleteValue(newValue);
handleChange(getValueAndLabel(newValue).value);
handleChangeDebounced(getValueAndLabel(newValue).value);
};

const handleClear = () => {
Expand All @@ -200,6 +207,12 @@ export const MRT_FilterTextField = <TData extends MRT_RowData>({
newFilterValues[rangeFilterIndex as number] = undefined;
return newFilterValues;
});
} else if (isAutocompleteFilter) {
setAutocompleteValue(null)
setFilterValue('')
// when using 'autocomplete' this function is called only inside effect and only if the filterValue === undefined
// so the following call is excessive; should be uncommented if the handleClear becomes part of the Autocomplete component callbacks
// column.setFilterValue(undefined)
} else {
setFilterValue('');
column.setFilterValue(undefined);
Expand Down Expand Up @@ -434,6 +447,8 @@ export const MRT_FilterTextField = <TData extends MRT_RowData>({
options={
dropdownOptions?.map((option) => getValueAndLabel(option)) ?? []
}
inputValue={filterValue as string}
onInputChange={handleAutocompleteInputChange}
{...autocompleteProps}
renderInput={(builtinTextFieldProps: TextFieldProps) => (
<TextField
Expand All @@ -455,7 +470,6 @@ export const MRT_FilterTextField = <TData extends MRT_RowData>({
...commonTextFieldProps?.slotProps?.htmlInput,
},
}}
onChange={handleTextFieldChange}
onClick={(e: MouseEvent<HTMLInputElement>) => e.stopPropagation()}
/>
)}
Expand Down

0 comments on commit 7ea078b

Please sign in to comment.