Web · Components
Autocomplete
A text box that filters suggestions as you type.
5 exports
Live example
Preview
Code
Events
- Events appear here when you interact with the preview.
AutocompleteField: tag input and required field
Import
A text box that filters suggestions as you type. Uses the same options, the same list and the same loading modes as Select; the only difference is a box you type into directly instead of a trigger button.
- Suggestions:
items(array, Promise, function) searches in the browser;loadItemssearches on the server and loads pages while scrolling. Turkish character and accent differences are ignored, the matching part is highlighted; large lists are virtualized. - Free text: with
allowCustomValue, text not in the list also becomes a value (a tag, an email recipient, etc.). - Multiple selection: with
multiple, selections become tags in the box; removed with ✕ or Backspace in an empty box. - Keyboard: ↓/↑ move through suggestions (focus stays in the box), Enter selects, Esc closes the list, a second Esc reverts the typed text.
- Mobile: since the keyboard stays open, the list opens below the box instead of as a bottom sheet and doesn't exceed 40% of the screen; rows are thumb-sized.
- Form: enters
FormDatawithname,requiredworks and it resets with the form. UseAutocompleteFieldfor label, description and error.
Examples
AutocompleteClassNames
interfaceClasses that can be given to the inner parts of Autocomplete.
| Name | Type | Description |
|---|---|---|
control? | string | The box itself (the frame around the text field, tags and buttons). |
input? | string | Text field. |
chip? | string | Each tag in multiple selection. |
content? | string | The open panel. |
item? | string | Each option. |
SingleAutocompleteProps
interfaceSingle-select Autocomplete.
| Name | Type | Default | Description |
|---|---|---|---|
multiple? | false | false | When true multiple values are selected; selections appear as tags in the box. |
value? | string | Selected value in controlled use; "" for none. | |
defaultValue? | string | Initial value in uncontrolled use. | |
onValueChange? | (value: string, item: SelectItem | undefined) => void | Called with the new value and the selected item when the selection changes ("" when cleared). | |
variant? | FieldVariant | CustomVariant<"field"> | Appearance: outline (bordered), filled, underline or ghost (plain). When omitted, the nearest data-field-variant container's setting is used, otherwise outline. See FieldVariant. | |
effect? | EffectProp | Effect or effects of the field box (e.g. "glow", ["lift", "beam"]). The accent color is the focus color (--ui-field-focus). Defaults to UIProvider appearance.defaults.field.effect. See BuiltinEffect. | |
items? | SelectItemsSource | Options: an array, a Promise or a function called once. Typed text is searched among these items in the browser. Same shape as Select (a string, { value, label, icon, description, disabled }, { group, items }). | |
loadItems? | SelectLoader | Function that searches the typed text on the server and fetches page by page. Requested 250 ms after typing stops; old requests are cancelled with signal. See Select loadItems. | |
allowCustomValue? | boolean | false | Can text not in the list also be a value? (tag input, email recipients, an "other" option, etc.). When on, Enter or leaving the box makes the typed text the value; in multiple selection it is added as a tag. When off, only list items can be chosen; unmatched text reverts to the selected value on leave. |
openOnFocus? | boolean | false | Open the list when the box is focused (without typing)? When off, the list opens on typing, the ↓ key or a click on the box. |
inputValue? | string | Text in the text field in controlled use. | |
onInputChange? | (text: string) => void | Called when the text in the field changes (e.g. to run your own search). | |
placeholder? | string | Text shown while the box is empty. | |
emptyMessage? | ReactNode | Content shown when the list is empty or the search has no results. | |
icon? | IconProp | Icon at the start of the box (e.g. "search"). In single selection the selected item's icon is shown if it has one. | |
clearable? | boolean | true | × button that clears the selection and text. |
name? | string | Form field name; the selected value(s) are submitted under this name. | |
form? | string | Id of the form the field belongs to (when outside the form). | |
required? | boolean | The form can't be submitted without a selection. | |
disabled? | boolean | Disabled. | |
id? | string | Id of the text field; <Label htmlFor> binds to it. | |
autoFocus? | boolean | Focused when the page opens. | |
open? | boolean | Whether the list is open in controlled use. | |
onOpenChange? | (open: boolean) => void | Called when the list opens or closes. | |
tooltip? | TooltipProp | Tooltip shown on hovering the box. See TooltipProp. | |
className? | string | Classes added to the container (e.g. w-72 for width). | |
classNames? | AutocompleteClassNames | Classes added to the inner parts. | |
aria-label? | string | Screen reader name when there is no visible label. | |
aria-labelledby? | string | Id of the element naming the box. | |
aria-describedby? | string | Id of the element describing the box. AutocompleteField provides it. | |
aria-invalid? | boolean | "true" | "false" | Show the field as invalid (red border). AutocompleteField provides it. |
MultiAutocompleteProps
interfaceMulti-select Autocomplete.
| Name | Type | Default | Description |
|---|---|---|---|
multiple | true | Multiple values are selected; selections appear as tags in the box and the list doesn't close on select. | |
value? | readonly string[] | Selected values in controlled use. | |
defaultValue? | readonly string[] | Initial values in uncontrolled use. | |
onValueChange? | (value: string[], items: SelectItem[]) => void | Called with the new values and their items when the selection changes. | |
variant? | FieldVariant | CustomVariant<"field"> | Appearance: outline (bordered), filled, underline or ghost (plain). When omitted, the nearest data-field-variant container's setting is used, otherwise outline. See FieldVariant. | |
effect? | EffectProp | Effect or effects of the field box (e.g. "glow", ["lift", "beam"]). The accent color is the focus color (--ui-field-focus). Defaults to UIProvider appearance.defaults.field.effect. See BuiltinEffect. | |
items? | SelectItemsSource | Options: an array, a Promise or a function called once. Typed text is searched among these items in the browser. Same shape as Select (a string, { value, label, icon, description, disabled }, { group, items }). | |
loadItems? | SelectLoader | Function that searches the typed text on the server and fetches page by page. Requested 250 ms after typing stops; old requests are cancelled with signal. See Select loadItems. | |
allowCustomValue? | boolean | false | Can text not in the list also be a value? (tag input, email recipients, an "other" option, etc.). When on, Enter or leaving the box makes the typed text the value; in multiple selection it is added as a tag. When off, only list items can be chosen; unmatched text reverts to the selected value on leave. |
openOnFocus? | boolean | false | Open the list when the box is focused (without typing)? When off, the list opens on typing, the ↓ key or a click on the box. |
inputValue? | string | Text in the text field in controlled use. | |
onInputChange? | (text: string) => void | Called when the text in the field changes (e.g. to run your own search). | |
placeholder? | string | Text shown while the box is empty. | |
emptyMessage? | ReactNode | Content shown when the list is empty or the search has no results. | |
icon? | IconProp | Icon at the start of the box (e.g. "search"). In single selection the selected item's icon is shown if it has one. | |
clearable? | boolean | true | × button that clears the selection and text. |
name? | string | Form field name; the selected value(s) are submitted under this name. | |
form? | string | Id of the form the field belongs to (when outside the form). | |
required? | boolean | The form can't be submitted without a selection. | |
disabled? | boolean | Disabled. | |
id? | string | Id of the text field; <Label htmlFor> binds to it. | |
autoFocus? | boolean | Focused when the page opens. | |
open? | boolean | Whether the list is open in controlled use. | |
onOpenChange? | (open: boolean) => void | Called when the list opens or closes. | |
tooltip? | TooltipProp | Tooltip shown on hovering the box. See TooltipProp. | |
className? | string | Classes added to the container (e.g. w-72 for width). | |
classNames? | AutocompleteClassNames | Classes added to the inner parts. | |
aria-label? | string | Screen reader name when there is no visible label. | |
aria-labelledby? | string | Id of the element naming the box. | |
aria-describedby? | string | Id of the element describing the box. AutocompleteField provides it. | |
aria-invalid? | boolean | "true" | "false" | Show the field as invalid (red border). AutocompleteField provides it. |
Props of the Autocomplete component.
| Name | Type | Default | Description |
|---|---|---|---|
multiple? | false | true | false | When true multiple values are selected; selections appear as tags in the box. |
value? | string | readonly string[] | Selected value in controlled use; "" for none. | |
defaultValue? | string | readonly string[] | Initial value in uncontrolled use. | |
onValueChange? | (value: string, item: SelectItem | undefined) => void | (value: string[], items: SelectItem[]) => void | Called with the new value and the selected item when the selection changes ("" when cleared). | |
variant? | FieldVariant | CustomVariant<"field"> | Appearance: outline (bordered), filled, underline or ghost (plain). When omitted, the nearest data-field-variant container's setting is used, otherwise outline. See FieldVariant. | |
effect? | EffectProp | Effect or effects of the field box (e.g. "glow", ["lift", "beam"]). The accent color is the focus color (--ui-field-focus). Defaults to UIProvider appearance.defaults.field.effect. See BuiltinEffect. | |
items? | SelectItemsSource | Options: an array, a Promise or a function called once. Typed text is searched among these items in the browser. Same shape as Select (a string, { value, label, icon, description, disabled }, { group, items }). | |
loadItems? | SelectLoader | Function that searches the typed text on the server and fetches page by page. Requested 250 ms after typing stops; old requests are cancelled with signal. See Select loadItems. | |
allowCustomValue? | boolean | false | Can text not in the list also be a value? (tag input, email recipients, an "other" option, etc.). When on, Enter or leaving the box makes the typed text the value; in multiple selection it is added as a tag. When off, only list items can be chosen; unmatched text reverts to the selected value on leave. |
openOnFocus? | boolean | false | Open the list when the box is focused (without typing)? When off, the list opens on typing, the ↓ key or a click on the box. |
inputValue? | string | Text in the text field in controlled use. | |
onInputChange? | (text: string) => void | Called when the text in the field changes (e.g. to run your own search). | |
placeholder? | string | Text shown while the box is empty. | |
emptyMessage? | ReactNode | Content shown when the list is empty or the search has no results. | |
icon? | IconProp | Icon at the start of the box (e.g. "search"). In single selection the selected item's icon is shown if it has one. | |
clearable? | boolean | true | × button that clears the selection and text. |
name? | string | Form field name; the selected value(s) are submitted under this name. | |
form? | string | Id of the form the field belongs to (when outside the form). | |
required? | boolean | The form can't be submitted without a selection. | |
disabled? | boolean | Disabled. | |
id? | string | Id of the text field; <Label htmlFor> binds to it. | |
autoFocus? | boolean | Focused when the page opens. | |
open? | boolean | Whether the list is open in controlled use. | |
onOpenChange? | (open: boolean) => void | Called when the list opens or closes. | |
tooltip? | TooltipProp | Tooltip shown on hovering the box. See TooltipProp. | |
className? | string | Classes added to the container (e.g. w-72 for width). | |
classNames? | AutocompleteClassNames | Classes added to the inner parts. | |
aria-label? | string | Screen reader name when there is no visible label. | |
aria-labelledby? | string | Id of the element naming the box. | |
aria-describedby? | string | Id of the element describing the box. AutocompleteField provides it. | |
aria-invalid? | boolean | "true" | "false" | Show the field as invalid (red border). AutocompleteField provides it. |