From 99ff7615134dfee71266a5a9aa406d308e000d4b Mon Sep 17 00:00:00 2001 From: Dmitrii Zolotukhin <118734134+dmitry-simple-it@users.noreply.github.com> Date: Mon, 5 Feb 2024 05:47:41 +0400 Subject: [PATCH] Added autocompleteValue useState variable to store full newValue and show value in getOptionLabel not value if dropdownOptions initially has array ob objects structure: [{value, label}] (#972) Co-authored-by: Dmitrii Zolotukhin --- .../components/inputs/MRT_FilterTextField.tsx | 65 ++++++++++--------- 1 file changed, 35 insertions(+), 30 deletions(-) diff --git a/packages/material-react-table/src/components/inputs/MRT_FilterTextField.tsx b/packages/material-react-table/src/components/inputs/MRT_FilterTextField.tsx index 2ce3f8742..b2c64a998 100644 --- a/packages/material-react-table/src/components/inputs/MRT_FilterTextField.tsx +++ b/packages/material-react-table/src/components/inputs/MRT_FilterTextField.tsx @@ -30,6 +30,7 @@ import { type TimePickerProps, } from '@mui/x-date-pickers/TimePicker'; import { + type DropdownOption, type MRT_Header, type MRT_RowData, type MRT_TableInstance, @@ -434,37 +435,41 @@ export const MRT_FilterTextField = ({ }, }} /> - ) : isAutocompleteFilter ? ( - getValueAndLabel(option).label} - onChange={(_e, newValue) => - handleChange(getValueAndLabel(newValue).value) - } - options={ - dropdownOptions?.map((option) => getValueAndLabel(option)) ?? [] - } - {...autocompleteProps} - renderInput={(builtinTextFieldProps) => ( - { + const [autocompleteValue, setAutocompleteValue] = useState(null); + const handleAutocompleteChange = (newValue: DropdownOption) => { + setAutocompleteValue(newValue); + handleChange(getValueAndLabel(newValue).value); + }; + return getValueAndLabel(option).label} + onChange={(_e, newValue) => handleAutocompleteChange(newValue)} + options={ + dropdownOptions?.map((option) => getValueAndLabel(option)) ?? [] + } + {...autocompleteProps} + renderInput={(builtinTextFieldProps) => ( + ) => e.stopPropagation()} - /> - )} - value={filterValue} - /> - ) : ( + }} + inputProps={{ + ...builtinTextFieldProps.inputProps, + ...commonTextFieldProps?.inputProps, + }} + onChange={handleTextFieldChange} + onClick={(e: MouseEvent) => e.stopPropagation()} + /> + )} + value={autocompleteValue} + />; + } + )() : (