Type to search, such as “re”.
- Add the registry
- Add use-async-options
Usage
load runs once the query has stayed the same for delay milliseconds. It receives the trimmed
query and a signal that aborts when users type again, so a slow response never replaces a newer
one. An empty query loads nothing.
While new options load, items keeps the last ones so the list doesn't flash empty. loading
and error report the state of the latest load. deps lists what load reads besides the query,
such as a selected category, and options load again when any of them changes.
This is the hook behind loadItems in Autocomplete and
loadSuggestions in Tag Input.
Examples
Reloading when a filter changes
load reads the selected category here, so the category goes in deps. Switching it loads the
options for the same query again, from the other source.
Type to search, such as “a”.
In an Autocomplete or Combobox
When you build on Base UI's Autocomplete or Combobox, pass onItemHighlighted to the root's prop
of the same name. getHighlight() then reports why the highlighted option is highlighted:
users chose it with the keyboard or pointer (chosen), it was highlighted automatically (auto),
or it was highlighted automatically among results that are being replaced (stale). Pressing Enter
on a stale option should submit the typed text rather than pick the option.
getSeen() returns every option loaded so far for the current deps, such as to accept one
that users typed out in full. When you pass itemToStringValue, it returns one option for
each text.
This example builds on Combobox to pick an assignee. It passes
filter={null}, since the endpoint already filters. A combobox has no typed text to submit, so
Enter does nothing while the highlighted option is stale.
API
useAsyncOptions
useAsyncOptions(query, options?) takes the typed text and these options.
It returns:
The file also exports LoadOptions<Item>, the type of load, and OptionHighlight, the
type getHighlight returns.