Command palette

@yasinsensoy/web

Web · Components

Autocomplete

A text box that filters suggestions as you type.

5 exports

Live example

Preview

Code

import { Label, Autocomplete } from "@yasinsensoy/web";

<Label htmlFor="search">Ara</Label>
<Autocomplete id="search" name="search" placeholder="Start typing…" icon="search"
  items={iller}
  onValueChange={(value) => …} />

Events

  1. Events appear here when you interact with the preview.

AutocompleteField: tag input and required field

react

Pick from the list or type and press Enter.

Import

import { Autocomplete } from "@yasinsensoy/web";

Autocomplete

componentClient component

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; loadItems searches 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 FormData with name, required works and it resets with the form. Use AutocompleteField for label, description and error.
Autocomplete(props: AutocompleteProps): JSX.Element

Examples

<Autocomplete name="city" placeholder="Search cities" items={cities} />

<Autocomplete name="person" icon="search" placeholder="Search people"
  loadItems={({ query, page, signal }) => searchPeople(query, page, signal)} />

<Autocomplete multiple allowCustomValue name="tags" placeholder="Add tags"
  items={["react", "nextjs", "tailwind"]} />

Classes that can be given to the inner parts of Autocomplete.

NameTypeDescription
control?stringThe box itself (the frame around the text field, tags and buttons).
input?stringText field.
chip?stringEach tag in multiple selection.
content?stringThe open panel.
item?stringEach option.

Single-select Autocomplete.

NameTypeDefaultDescription
multiple?falsefalseWhen true multiple values are selected; selections appear as tags in the box.
value?stringSelected value in controlled use; "" for none.
defaultValue?stringInitial value in uncontrolled use.
onValueChange?(value: string, item: SelectItem | undefined) => voidCalled 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?EffectPropEffect 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?SelectItemsSourceOptions: 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?SelectLoaderFunction 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?booleanfalseCan 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?booleanfalseOpen 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?stringText in the text field in controlled use.
onInputChange?(text: string) => voidCalled when the text in the field changes (e.g. to run your own search).
placeholder?stringText shown while the box is empty.
emptyMessage?ReactNodeContent shown when the list is empty or the search has no results.
icon?IconPropIcon at the start of the box (e.g. "search"). In single selection the selected item's icon is shown if it has one.
clearable?booleantrue× button that clears the selection and text.
name?stringForm field name; the selected value(s) are submitted under this name.
form?stringId of the form the field belongs to (when outside the form).
required?booleanThe form can't be submitted without a selection.
disabled?booleanDisabled.
id?stringId of the text field; <Label htmlFor> binds to it.
autoFocus?booleanFocused when the page opens.
open?booleanWhether the list is open in controlled use.
onOpenChange?(open: boolean) => voidCalled when the list opens or closes.
tooltip?TooltipPropTooltip shown on hovering the box. See TooltipProp.
className?stringClasses added to the container (e.g. w-72 for width).
classNames?AutocompleteClassNamesClasses added to the inner parts.
aria-label?stringScreen reader name when there is no visible label.
aria-labelledby?stringId of the element naming the box.
aria-describedby?stringId of the element describing the box. AutocompleteField provides it.
aria-invalid?boolean | "true" | "false"Show the field as invalid (red border). AutocompleteField provides it.

Multi-select Autocomplete.

NameTypeDefaultDescription
multipletrueMultiple 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[]) => voidCalled 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?EffectPropEffect 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?SelectItemsSourceOptions: 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?SelectLoaderFunction 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?booleanfalseCan 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?booleanfalseOpen 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?stringText in the text field in controlled use.
onInputChange?(text: string) => voidCalled when the text in the field changes (e.g. to run your own search).
placeholder?stringText shown while the box is empty.
emptyMessage?ReactNodeContent shown when the list is empty or the search has no results.
icon?IconPropIcon at the start of the box (e.g. "search"). In single selection the selected item's icon is shown if it has one.
clearable?booleantrue× button that clears the selection and text.
name?stringForm field name; the selected value(s) are submitted under this name.
form?stringId of the form the field belongs to (when outside the form).
required?booleanThe form can't be submitted without a selection.
disabled?booleanDisabled.
id?stringId of the text field; <Label htmlFor> binds to it.
autoFocus?booleanFocused when the page opens.
open?booleanWhether the list is open in controlled use.
onOpenChange?(open: boolean) => voidCalled when the list opens or closes.
tooltip?TooltipPropTooltip shown on hovering the box. See TooltipProp.
className?stringClasses added to the container (e.g. w-72 for width).
classNames?AutocompleteClassNamesClasses added to the inner parts.
aria-label?stringScreen reader name when there is no visible label.
aria-labelledby?stringId of the element naming the box.
aria-describedby?stringId 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.

type AutocompleteProps = SingleAutocompleteProps | MultiAutocompleteProps
NameTypeDefaultDescription
multiple?false | truefalseWhen 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[]) => voidCalled 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?EffectPropEffect 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?SelectItemsSourceOptions: 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?SelectLoaderFunction 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?booleanfalseCan 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?booleanfalseOpen 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?stringText in the text field in controlled use.
onInputChange?(text: string) => voidCalled when the text in the field changes (e.g. to run your own search).
placeholder?stringText shown while the box is empty.
emptyMessage?ReactNodeContent shown when the list is empty or the search has no results.
icon?IconPropIcon at the start of the box (e.g. "search"). In single selection the selected item's icon is shown if it has one.
clearable?booleantrue× button that clears the selection and text.
name?stringForm field name; the selected value(s) are submitted under this name.
form?stringId of the form the field belongs to (when outside the form).
required?booleanThe form can't be submitted without a selection.
disabled?booleanDisabled.
id?stringId of the text field; <Label htmlFor> binds to it.
autoFocus?booleanFocused when the page opens.
open?booleanWhether the list is open in controlled use.
onOpenChange?(open: boolean) => voidCalled when the list opens or closes.
tooltip?TooltipPropTooltip shown on hovering the box. See TooltipProp.
className?stringClasses added to the container (e.g. w-72 for width).
classNames?AutocompleteClassNamesClasses added to the inner parts.
aria-label?stringScreen reader name when there is no visible label.
aria-labelledby?stringId of the element naming the box.
aria-describedby?stringId of the element describing the box. AutocompleteField provides it.
aria-invalid?boolean | "true" | "false"Show the field as invalid (red border). AutocompleteField provides it.