Skip to content

Commit 99ff761

Browse files
dmitry-simple-itDmitrii Zolotukhin
andauthored
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 <dmitrii.zolotukhin@sportion.net>
1 parent b248acf commit 99ff761

File tree

1 file changed

+35
-30
lines changed

1 file changed

+35
-30
lines changed

packages/material-react-table/src/components/inputs/MRT_FilterTextField.tsx

Lines changed: 35 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import {
3030
type TimePickerProps,
3131
} from '@mui/x-date-pickers/TimePicker';
3232
import {
33+
type DropdownOption,
3334
type MRT_Header,
3435
type MRT_RowData,
3536
type MRT_TableInstance,
@@ -434,37 +435,41 @@ export const MRT_FilterTextField = <TData extends MRT_RowData>({
434435
},
435436
}}
436437
/>
437-
) : isAutocompleteFilter ? (
438-
<Autocomplete
439-
freeSolo
440-
getOptionLabel={(option) => getValueAndLabel(option).label}
441-
onChange={(_e, newValue) =>
442-
handleChange(getValueAndLabel(newValue).value)
443-
}
444-
options={
445-
dropdownOptions?.map((option) => getValueAndLabel(option)) ?? []
446-
}
447-
{...autocompleteProps}
448-
renderInput={(builtinTextFieldProps) => (
449-
<TextField
450-
{...builtinTextFieldProps}
451-
{...commonTextFieldProps}
452-
InputProps={{
453-
...builtinTextFieldProps.InputProps,
454-
startAdornment:
438+
) : isAutocompleteFilter ? (() => {
439+
const [autocompleteValue, setAutocompleteValue] = useState<DropdownOption | null>(null);
440+
const handleAutocompleteChange = (newValue: DropdownOption) => {
441+
setAutocompleteValue(newValue);
442+
handleChange(getValueAndLabel(newValue).value);
443+
};
444+
return <Autocomplete
445+
freeSolo
446+
getOptionLabel={(option) => getValueAndLabel(option).label}
447+
onChange={(_e, newValue) => handleAutocompleteChange(newValue)}
448+
options={
449+
dropdownOptions?.map((option) => getValueAndLabel(option)) ?? []
450+
}
451+
{...autocompleteProps}
452+
renderInput={(builtinTextFieldProps) => (
453+
<TextField
454+
{...builtinTextFieldProps}
455+
{...commonTextFieldProps}
456+
InputProps={{
457+
...builtinTextFieldProps.InputProps,
458+
startAdornment:
455459
commonTextFieldProps?.InputProps?.startAdornment,
456-
}}
457-
inputProps={{
458-
...builtinTextFieldProps.inputProps,
459-
...commonTextFieldProps?.inputProps,
460-
}}
461-
onChange={handleTextFieldChange}
462-
onClick={(e: MouseEvent<HTMLInputElement>) => e.stopPropagation()}
463-
/>
464-
)}
465-
value={filterValue}
466-
/>
467-
) : (
460+
}}
461+
inputProps={{
462+
...builtinTextFieldProps.inputProps,
463+
...commonTextFieldProps?.inputProps,
464+
}}
465+
onChange={handleTextFieldChange}
466+
onClick={(e: MouseEvent<HTMLInputElement>) => e.stopPropagation()}
467+
/>
468+
)}
469+
value={autocompleteValue}
470+
/>;
471+
}
472+
)() : (
468473
<TextField
469474
select={isSelectFilter || isMultiSelectFilter}
470475
{...commonTextFieldProps}

0 commit comments

Comments
 (0)