Autocomplete
A text field that suggests options while the user types and shows the chosen one as a removable chip.
Focus the field and type part of a name. Choose a suggestion with the mouse or the arrow keys and Enter. Select the chip to remove it and search again.
When to use
- To find one entity by typing, such as a reviewer, a person or an organization, when the user usually knows part of the name.
- When the field must stay a text input until a value is chosen.
- When the options are already filtered by the caller, locally or from a server.
When not to use
- A short, fixed list. Use Select.
- A long list where the user also browses. Use Combobox, which opens a list with a search field inside.
- A value that may not be in the list. Use Creatable combobox.
- Running commands or navigation. Use Command.
Which one to use
| Component | The user | Options | Value | Typical use |
|---|---|---|---|---|
| Select | Picks from a list | Fixed, short | One of the options | Status, type, role |
| Combobox | Searches, then picks | Long or loaded from an API | One of the options | Customer, template, model |
| Creatable combobox | Searches, picks or types | Suggestions | An option or free text | Field name, tag |
| Autocomplete | Types and gets suggestions | Filtered as they type | One option, shown as a chip | Search for a person or entity |
| Command | Types to find an action | Actions and pages | Runs an action, holds no value | Global search, shortcuts |
States
Empty result
When options is empty and the field is not loading, the list shows emptyMessage. The component does not filter for you in that case: pass already filtered options.
Loading
While isLoading is true the list shows a skeleton row and typing is not forwarded.
With a chosen value
The chosen option is shown as a chip. Selecting the chip clears it and returns to the text field.
Selecione o revisor para trocá-lo.
Disabled
Content
- Put a label above the field: “Revisor responsável”. The placeholder says how to search: “Buscar revisor…”.
emptyMessageis required. Write it short and in the user’s language: “Nenhum resultado”.- Option labels are what users read in the chip, so use the full name, not an id.
Accessibility
- Built on cmdk: the input is
role="combobox"witharia-controlsandaria-activedescendant, the list isrole="listbox"and options arerole="option". - Keyboard: typing opens the list;
ArrowUpandArrowDownmove the highlight;Enterchooses the highlighted option, or the option whose label equals the text;Escapeblurs the field and closes the list. - Pressing a key in the field reopens the list if it was closed.
- The chip is a button; its text is its name. The text field has no
idoraria-labelprop, so aLabelcannot be linked to it. See Known issues.
Code
import { AutoComplete } from "@repo/ui/autocomplete";
<AutoComplete
emptyMessage="Nenhum resultado"
inputValue={inputValue}
onInputValueChange={setInputValue}
onValueChange={setReviewer}
options={results}
placeholder="Buscar revisor…"
value={reviewer}
/>;
Props
AutoComplete| Prop | Type | Default | Description |
|---|---|---|---|
disabled | boolean | — | Disables the field and the chip |
emptyMessage | string | — | Shown when `options` is empty and not loading. Required. Filter `options` yourself so it can appear |
inputValue | string | — | Controlled text of the field |
isLoading | boolean | false | Shows a skeleton row instead of the options |
onInputValueChange | (value: string) => void | — | Called when the text changes, and with the label on select |
onValueChange | (value: Option) => void | — | Called with the chosen option, or with `undefined` when the user clears it |
options | { value: string; label: string }[] | — | Options to suggest. Each needs a `value` and a `label` |
placeholder | string | — | Placeholder of the empty field |
selectedClassName | string | — | Extra class for the chip that shows the chosen option |
value | Option | — | Controlled chosen option |
Known issues
| Issue | Effect |
|---|---|
The list uses bg-white, ring-slate-200, text-gray-400 and hover:bg-disabled. |
Hardcoded colors do not follow the theme: the list stays white in Dark and Darker, and the clear icon uses a fixed gray. |
The selected option uses bg-primary text-primary-foreground, and the preset has no primary-foreground color (only old-primary). |
The chosen option gets no highlight text color and its fill equals the page background, so it looks the same as other rows. |
The chip uses bg-disabled/40 (translucent) and the field has no focus style: the border is on a wrapper div and CommandInput removes its outline. |
Keyboard users get no visible focus ring on the field, and the chip depends on transparency. |
The clear Button (size="smIcon") has only an icon and no aria-label. |
Screen readers announce an unnamed button. |
emptyMessage shows only when options is empty. When options has items that cmdk then filters out, nothing is shown. |
Typing a term with no match leaves an empty box unless the caller filters options itself. |
Clearing calls onValueChange(undefined) while the prop type says Option. |
Callers must handle undefined despite the type. |
The input accepts no id, name or aria-label. |
Labels cannot be associated and the field cannot take part in a native form submit. |