Autocomplete
Autocomplete allows users to quickly filter through a list of options and pick one or more values for a field.
Page navigation navigation
React
readyNot reviewed for a11y
Rails
readyNot reviewed for a11y
Figma
React examples
Default
Multi-select
Multi-select with tokens for selected items
Adding new items from input
When the user isn't limited to a pre-defined list, an additional menu item can be added to select the value they typed.
Loading
A loading indicator should be displayed while the data for the list of options is being populated. The loading indicator helps the user understand that the list is not empty, it's just not done loading.
More code examples
See the Autocomplete Storybook stories.
Props
Autocomplete
Name | Required | Description |
---|---|---|
children | React.ReactNode | |
id | string May be used to customize how the ID is set on the text input to be used by ARIA attributes on related elements. |
Autocomplete.Input
Name | Required | Description |
---|---|---|
as | React.ElementType | |
openOnFocus Deprecated | boolean Whether the associated autocomplete menu should open on an input focus event |
Autocomplete.Overlay
Name | Required | Description |
---|---|---|
menuAnchorRef | React.RefObject<HTMLElement> | |
children | React.ReactNode | |
overlayProps Deprecated | Partial<OverlayProps> Props to be spread on the internal `Overlay` component. |
Autocomplete.Menu
Name | Required | Description |
---|---|---|
items | Required | T[] The options for field values that are displayed in the dropdown menu. One or more may be selected depending on the value of the `selectionVariant` prop. |
selectedItemIds | Required | string[] The IDs of the selected items |
aria-labelledby | Required | string |
addNewItem | Omit<T, 'id' | 'leadingVisual' | 'onAction'> & { handleAddItem: (item: Omit<T, 'leadingVisual' | 'onAction'>) => void; } A menu item that is used to allow users make a selection that is not available in the array passed to the `items` prop. This menu item gets appended to the end of the list of options. | |
emptyStateText | React.ReactNode | false The text that appears in the menu when there are no options in the array passed to the `items` prop. | |
filterFn | (item: T, i: number) => boolean A custom function used to filter the options in the array passed to the `items` prop. By default, we filter out items that don't match the value of the autocomplete text input. The default filter is not case-sensitive. | |
loading | boolean Whether the data is loading for the menu items | |
sortOnCloseFn | (itemIdA: string | number, itemIdB: string | number) => number The sort function that is applied to the options in the array passed to the `items` prop after the user closes the menu. By default, selected items are sorted to the top after the user closes the menu. | |
selectionVariant | 'single' | 'multiple' Whether there can be one item selected from the menu or multiple items selected from the menu | |
onOpenChange | (open: boolean) => void Function that gets called when the menu is opened or closed | |
onSelectedChange | (item: T | T[]) => void The function that is called when an item in the list is selected or deselected | |
customScrollContainerRef | React.MutableRefObject<HTMLElement | null> If the menu is rendered in a scrolling element other than the `Autocomplete.Overlay` component, pass the ref of that element to `customScrollContainerRef` to ensure the container automatically scrolls when the user highlights an item in the menu that is outside the scroll container |