feat(ControlledAutocomplete): filter by value too
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
import * as React from 'react';
|
import * as React from 'react';
|
||||||
import { Controller, useFormContext } from 'react-hook-form';
|
import { Controller, useFormContext } from 'react-hook-form';
|
||||||
import FormHelperText from '@mui/material/FormHelperText';
|
import FormHelperText from '@mui/material/FormHelperText';
|
||||||
import Autocomplete, { AutocompleteProps } from '@mui/material/Autocomplete';
|
import Autocomplete, { AutocompleteProps, createFilterOptions } from '@mui/material/Autocomplete';
|
||||||
import Typography from '@mui/material/Typography';
|
import Typography from '@mui/material/Typography';
|
||||||
import type { IFieldDropdownOption } from '@automatisch/types';
|
import type { IFieldDropdownOption } from '@automatisch/types';
|
||||||
|
|
||||||
@@ -18,6 +18,14 @@ interface ControlledAutocompleteProps
|
|||||||
const getOption = (options: readonly IFieldDropdownOption[], value: string) =>
|
const getOption = (options: readonly IFieldDropdownOption[], value: string) =>
|
||||||
options.find((option) => option.value === value) || null;
|
options.find((option) => option.value === value) || null;
|
||||||
|
|
||||||
|
// Enables filtering by value in autocomplete dropdown
|
||||||
|
const filterOptions = createFilterOptions<IFieldDropdownOption>({
|
||||||
|
stringify: ({ label, value }) => `
|
||||||
|
${label}
|
||||||
|
${value}
|
||||||
|
`
|
||||||
|
})
|
||||||
|
|
||||||
function ControlledAutocomplete(
|
function ControlledAutocomplete(
|
||||||
props: ControlledAutocompleteProps
|
props: ControlledAutocompleteProps
|
||||||
): React.ReactElement {
|
): React.ReactElement {
|
||||||
@@ -75,6 +83,7 @@ function ControlledAutocomplete(
|
|||||||
{...autocompleteProps}
|
{...autocompleteProps}
|
||||||
{...field}
|
{...field}
|
||||||
options={options}
|
options={options}
|
||||||
|
filterOptions={filterOptions}
|
||||||
value={getOption(options, field.value)}
|
value={getOption(options, field.value)}
|
||||||
onChange={(event, selectedOption, reason, details) => {
|
onChange={(event, selectedOption, reason, details) => {
|
||||||
const typedSelectedOption =
|
const typedSelectedOption =
|
||||||
|
Reference in New Issue
Block a user