Autocomplete

A field whose text finds an option in a list: a role="combobox" input with a role="listbox" below it (in a Portal, placed under the field and flipped for want of room), aria-activedescendant on the...

Demos

For usage examples, visit the component demo page: Autocomplete

Import

import { Autocomplete } from '@soroush.tech/design-system/Autocomplete'

Props

PropTypeDefaultDescription
optionsreadonly T[]-What can be chosen - strings, or anything getOptionLabel can say.
value / defaultValueT | null, or T[] with multiplenull / []The choice. Controlled with onChange, or at first.
onChange(event, value, reason) => void-reason: 'selectOption', 'removeOption', 'createOption' or 'clear'.
inputValue / onInputChangestring / (event, value, reason) => void-The text, controlled. reason: 'input', 'reset' or 'clear'.
multiplebooleanfalseChooses several: each shows as a Chip, and Backspace in the empty field removes one.
shouldAllowFreeTextbooleanfalseTakes a typed text that is no option as the value - a string - on Enter or on leaving.
getOptionLabel(option) => stringthe string, else labelWhat an option says, and what is typed to find it.
isOptionEqualToValue(option, value) => boolean===Whether an option is the value.
filterOptions(options, { inputValue }) => T[]label holds the textThe options that match what is typed.
groupBy(option) => string-The group of an option - options of one group are put next to one another.
renderOption(option, { isSelected, isHighlighted }) => ReactNodethe labelWhat an option shows in its row.
isLoading / loadingTextboolean / stringfalse / -While there is nothing to list, the list says loadingText.
noOptionsTextstring-What the list says when nothing matches. Without it, the list stays closed.
clearLabelstring-Names the clear control. Without it, there is none.
openLabel / closeLabelstring-Name the control that opens and closes the list.
getRemoveLabel(label) => string-Names the control that removes a chip, by its label. Without it, a chip has none.
variant / size / colorits FormControl'sThe frame, as TextInput's; theme.sizes; theme.palette.
disabled / error / required / fullWidthbooleanits FormControl'sNative state.
textColorkeyof Theme['text']the focus colorText color.
borderRadiuskeyof Theme['radii']field square; list theme.defaults.surfaceRadiusCorner radius of the field (default and outlined frames) and of the list.
bgkeyof Theme['background']'modal'Background of the list.
layerkeyof Theme['zOrder']'modal'Stacking layer of the list.
placeholder, name, id, className, aria-*, data-testidOn the field.

Customization

theme.components.Autocomplete styles the root, tags, input, clearIndicator, popupIndicator, popup, listbox, group, groupLabel, groupItems, option and status. Its defaultProps set variant, size, color, textColor, borderRadius, bg, shouldAllowFreeText, layer and the labels - so a theme gives every Autocomplete its language.

Server rendering

The list of options is drawn in a Portal, which renders nothing on the server: it is absent from server-rendered and prerendered HTML, and mounts on the client when it opens (on a click, hover, focus or key, or when the app sets it open). One that starts open, by its open prop, is still closed in the static HTML and opens on hydration. Its trigger renders on the server as usual.

Source code

If this page does not answer your question, have a look at the implementation of the component for more detail.