- Add the registry
- Add autocomplete
Usage
Pass the suggestions as items and render each one inside AutocompleteList. The list opens as
people type, or on the down arrow, and shows the suggestions that contain what's typed. Picking one
fills in the input, but the value is always the text, so people can type something that isn't
suggested. The list closes when nothing matches, unless it has an AutocompleteEmpty to say so.
Label the input with aria-label or a FieldLabel.
When the value has to be one of the options, use a Combobox. For several values, use a Tag Input, which takes the same suggestions.
Composition
Examples
With a label
Put it in a Field with a FieldLabel, which labels the input.
Async
To load suggestions from somewhere else, such as a search endpoint, pass loadItems in place of
items. It's called with the typed text once people stop typing for loadDelay milliseconds, 300
by default, and with an AbortSignal that aborts when they type again, so pass it on to fetch.
Its results are shown as given. An AutocompleteStatus shows a spinner while they load and a
message if loading fails, and screen readers announce both, then how many suggestions arrived. A
failed search runs again once the text changes and comes back to it. Until new results arrive, the
last ones stay in the list, dimmed. Here a stand-in for the endpoint answers after 600 milliseconds
with the libraries that start with what's typed.
With autoHighlight, which highlights the first suggestion as people type, Enter fills in a
suggestion only when it's among the results for what's typed, never one left from an earlier search.
When loadItems reads something besides the text, such as a selected category, list it in
loadDeps, and suggestions load again when it changes, the way an effect's dependencies work:
Groups
Pass groups as items, each with its own items, and render them with AutocompleteGroup and
AutocompleteCollection. A group with no matches is left out. End each group with an
AutocompleteSeparator, which hides itself in the last group shown.
Search
Put an InputGroupAddon in AutocompleteInput for an icon, and set showClear for a button that
clears the text.
Objects
For object items, itemToStringValue returns the text to match and fill in. showTrigger adds a
button that opens the suggestions: all of them when nothing is typed, or the ones that match.
Your own suggestions
useSuggestions, the hook behind loadItems here and loadSuggestions in Tag Input, loads suggestions for any
control. It takes the query and a load function, with deps, delay, and onLoad and onError
callbacks, and returns the items for the query, loading and error. seen returns every
suggestion loaded so far, one for each text when you pass itemToStringValue. Pass
onItemHighlighted to a Base UI root's prop of the same name, and highlight tells why a
suggestion is highlighted: people chosen it, it was highlighted for them (auto), or that
happened among results being replaced (stale).
To add the hook on its own:
Props
AutocompleteInput is an Input Group with optional clear and open
buttons. AutocompleteContent takes side, sideOffset, align, alignOffset and anchor.
The tables leave out className, style and render. Parts built on Base UI take them as its styling and composition guides describe.