The Autocomplete component is comprised of an Autocomplete.Input component that a user types into, and a Autocomplete.Menu component that displays the list of selectable values.
The text input is used to filter the options in the dropdown menu. It is also used to show the selected value (or values).
The default input rendered is the TextInput component. A different text input component may be rendered by passing a different component to the as prop.
The Autocomplete.Input should not be rendered without a <label> who's htmlFor prop matches the Autocomplete.Input's id prop
The Autocomplete.Overlay wraps the Autocomplete.Menu to display it in an Overlay component.
This component takes the same props as the Overlay component.
Most Autocomplete implementations will use the Autocomplete.Overlay component, but there could be special cases where the Autocomplete.Menu should be rendered directly after the Autocomplete.Input (for example: an Autocomplete that is already being rendered in an Overlay).
The Autocomplete.Menu component renders a list of selectable options in a non-modal dialog. The list is filtered and sorted to make it as easy as possible to find the option/s that a user is looking for.
The Autocomplete.Menu component should be passed an aria-labelledby prop that matches the id prop of the <label> associated with the Autocomplete.Input
By default, menu items are just rendered as a single line of text. The list in the menu is rendered using the Action List component, so menu items can be rendered with all of the same options as Action List items.
However, the renderGroup, groupMetadata, and renderItem props have not been implemented yet.
Items can be displayed in any order that makes sense, but the Autocomplete.Menu component comes with a default sort behavior to make it easy to find selected items. The default behavior is to sort selected items to the top of the list after the menu has been closed.
A function may be passed to the sortOnCloseFn prop if this default sorting logic is not helpful for your use case. The sort function will be only be called after the menu is closed so that items don't shift while users are trying to make a selection.
By default, menu items are filtered based on whether or not they match the value of the text input. The default filter is case-insensitive.
A function may be passed to the filterFn prop if this default filtering behavior does not make sense for your use case.
In this example, we're passing a TextInputWithTokens component
Autocomplete.OverlayActionList.Item propsIn this example, selected items get sorted to the end. By default, they are sorted to the beginning.
In this example, we show any items whose text contains the input value. By default, we only show items that start with the input value.
Autocomplete.Overlay with a customScrollContainerRefIf a Autocomplete.Menu is rendered without an Autocomplete.Overlay inside of a scrollable container, the ref of the scrollable container must be passed to the customScrollContainerRef to ensure that highlighted items are always scrolled into view.
Autocomplete.ContextThe Autocomplete.Context can be used to control the menu open/closed state and customize certain Autocomplete behaviors
DialogIf you attempt to render an Autocomplete inside of a Dialog (or any other component that overlays the page), the menu will be rendered below the parent overlay.
There are two ways to get around this:
Portal root inside the Dialog, then pass the name of that Portal root to Autocomplete.Overlay: <Autocomplete.Overlay portalContainerName="portal-inside-dialog">Autocomplete.Overlay until after the Dialog has opened| Name | Type | Default | Description |
|---|---|---|---|
| children | React.ReactNode |
| Name | Type | Default | Description |
|---|---|---|---|
| as | React.ElementType | TextInput | The underlying element to render — either a HTML element name or a React component. |
Additional props are passed to the <TextInput> element. See TextInput docs for a list of props accepted by the <TextInput> element. | |||
| Name | Type | Default | Description |
|---|---|---|---|
| menuAnchorRef | React.RefObject<HTMLElement> | ||
| children | React.ReactNode | ||
| overlayProps Deprecated | Partial<OverlayProps> | Props to be spread on the internal | |
Additional props are passed to the <Overlay> element. See Overlay docs for a list of props accepted by the <Overlay> element. | |||
| Name | Type | Default | 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 | |
| selectedItemIds Required | (string | number)[] | 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 | |
| emptyStateText | React.ReactNode | false | The text that appears in the menu when there are no options in the array passed to the | |
| filterFn | (item: T, i: number) => boolean | A custom function used to filter the options in the array passed to the | |
| 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 | |
| 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 |