A text field that suggests as you type, where any text is still a valid answer.
$ pnpm dlx shadcn@latest add @dashral/autocompleteInstallation
$ pnpm dlx shadcn@latest add @dashral/autocompleteAdds the component, the components it builds on, its npm packages and the theme tokens it uses. Or by its full URL: https://dashral.com/r/autocomplete.json
Usage
import { Autocomplete } from "@/components/dashral/autocomplete"
<Autocomplete label="City" suggestions={cities} value={city} onValueChange={setCity} />Using the npm package? Import from @dashral/ui/components/autocomplete instead, here and in the examples; the rest is the same.
Examples
API reference
Autocomplete
Also takes every prop of Input(opens in a new tab).
| Prop | Type | Default |
|---|---|---|
label | ReactNodeShown above, linked to the control. | — |
description | ReactNodeA hint under the control. | — |
error | boolean | ReactNodetrue shows the error style; a message also shows under the control. | — |
labelAction | ReactNodeAt the end of the label's row, e.g. a “Forgot password?” link or the current value. | — |
withAsterisk | booleanMarks the label with an asterisk, e.g. alongside required. | false |
value / defaultValue | stringThe text, controlled or initial. Any text is valid. | "" |
onValueChange | (value: string) => voidCalled with the new value. | — |
suggestions | (string | { value, description?, icon? })[]What it suggests. Required. | — |
onSelect | (value: string) => voidCalled when a suggestion is picked, not on every keystroke. | — |
filter | booleanNarrows the suggestions to those containing the text (ignoring case and accents). false shows them as given, for your own search. | true |
loading | booleanA spinner in the list while your suggestions load. | false |
limit | numberAt most this many in the list. | 8 |
prefix / suffix | ReactNodeInside the box, as on Input. | — |
classNames | { root, label, wrapper, input, description, error, content, option }Classes for each part. | — |
className | stringExtra classes, merged with cn() so yours win. | — |
Keyboard
| Keys | Action |
|---|---|
| ↓or↑ | Move through the suggestions (the focus stays in the field) |
| Enter | Pick the highlighted suggestion |
| Escape | Close the list, keeping the text |
Accessibility
- With label, description or error, the id, aria-describedby and aria-invalid are wired up for you.
- The combobox pattern: role="combobox" with aria-expanded and aria-activedescendant pointing at the highlighted option of a listbox.
- For a choice from a fixed set, use Combobox: it doesn't accept free text.