Autocomplete
The Autocomplete component dynamically suggests options as users type into an input field. This enhances user experience by making data entry faster and more accurate, particularly when dealing with large data sets. With multiple selection and customization capabilities, it offers high versatility for a wide range of applications.
This component is built upon fantastic @floating-ui/react library.
Listening to input value changes
Sometimes you need to get to the actual value passed to the input. For example, you might want to use it to make an external API call. In this case, you can use the onInputChange prop to listen to the input value changes.
Caveat When is cleared by default clearable callback the event will be synthetic and will not contain the actual value.
const clearableCallback = () => {
onChange?.(null)
props.onInputChange?.({ target: { value: '' } } as any)
clearSearch()
}Autocompleting multiple options
With the multiple prop, the Autocomplete component allows for the selection of multiple options, providing a dynamic and versatile user experience.
This example also makes use of the onCreate callback, which is triggered when the user selects an option that does not exist in the options list. This callback can be used to create new options on the fly.
In the multiple mode, the value and onChange props adjust their signature as follows:
| Property | !multiple | multiple |
|---|---|---|
| value | T | T[] |
| onChange | T => void | T[] => void |
For more details, please refer to the properties table.
Component API
The main Autocomplete component. Extends props of InputBase component.
| Prop | Default | Description |
options | { label:string, disabled?: boolean }[] | T[]List available options |
value | — | TCurrent value of input If multiple is set, the type of |
onChange | — | (value: T | T[] | null) => voidChange event callback If multiple is set, the type of |
onCreate | — | (title: string) => voidCallback for creating new option |
multiple | false | booleanAllow multiple values |
autoHighlight | false | booleanHighlight search text fragment in options. Works only with default getOptionLabel |
renderOption | _renderOption | (props: AutocompleteOptionProps, option: T) => ReactNodeFunction rendering option in dropdown. Docs |
renderSelection | _renderSelection | (option: T) => ReactNodeFunction rendering single selection. Docs |
renderTags | _renderTags | (selected: T[]) => ReactNodeFunction rendering option in dropdown. Docs |
getLimitTagsText | more => `+${more}` | (more: number) => stringText to display when the tags are truncated. Also see |
limit | 3 | numberLimit of multiple selected to be displayed in input |
defaultTagProps: ChipProps | — | ChipPropsProps of the default tags component (Chip). Chip component docs. |
isOptionEqualToValue | _isOptionEqualToValue | (option: T, value?: T | null) => booleanFunction to compare option and value. |
getOptionDisabled | option => option.disabled | (option: T) => booleanGetter for option disabled state. |
getOptionLabel | option => option.label ?? option | (option: T) => stringGetter for option label. |
filterOptions | _filterOptions | (
options: T[],
filterOptions: AutocompleteFilterOptions
) => T[]Filter options function. Docs |
textEmpty | No options | stringText to display for empty list. |
textNotFound | No results found | stringText to display when no options match search. |
textCreate | Create | stringText to display when no options matches search and |
textLoading | Loading... | stringText to display for loading state input. Replaces |
textClear | Clear | stringText to display as HTML "title" of clear button. |
maxHeight | 500 | number | string | "available"How heigh should dropdown list be? Either provide a number of pixels or a string like |
zIndex | — | { list?: number}Escape hatch for z-index of dropdown list. Helpful when using with modals and drawers that might have their own, higher z-index. |
filterSelectedOptions | false | booleanShould filter out selected options from options list |
Default option shape. You can provide your own option shape by passing it to component.
| Prop | Default | Description |
label | option.label ?? option | React.ReactNodeLabel of option |
disabled | — | booleanOption disabled state, used by |