Autocomplete

Free text with suggestions, filled in by a person or an agent.

Source
$npx @dinachi/cli@latest add autocomplete
placeholder
mode
AutocompleteClear
disabled

Usage

import {
  Autocomplete,
  AutocompleteContent,
  AutocompleteEmpty,
  AutocompleteInput,
  AutocompleteItem,
  AutocompleteList,
} from "@/components/ui/autocomplete"

const cities = ["London", "Paris", "Tokyo"]

export function CitySearch() {
  return (
    <Autocomplete items={cities}>
      <AutocompleteInput aria-label="City" placeholder="Search cities..." />
      <AutocompleteContent>
        <AutocompleteEmpty>No cities found.</AutocompleteEmpty>
        <AutocompleteList>
          {(city: string) => (
            <AutocompleteItem key={city} value={city}>
              {city}
            </AutocompleteItem>
          )}
        </AutocompleteList>
      </AutocompleteContent>
    </Autocomplete>
  )
}

Examples

Suggestions filter as you type.
import {
  Autocomplete,
  AutocompleteInput,
  AutocompleteTrigger,
  AutocompleteContent,
  AutocompleteList,
  AutocompleteItem,
  AutocompleteEmpty,
} from '@/components/ui/autocomplete';

const cities = ["New York", "San Francisco", "London", "Tokyo", "Paris", "Berlin", "Sydney"];

export function Example() {
  return (
    <Autocomplete items={cities} openOnInputClick>
      <div className="flex w-[280px] items-center gap-1 rounded-md border border-input focus-within:ring-2 focus-within:ring-ring focus-within:ring-offset-2">
        <AutocompleteInput placeholder="Search cities..." />
        <AutocompleteTrigger />
      </div>
      <AutocompleteContent>
        <AutocompleteEmpty>No cities found.</AutocompleteEmpty>
        <AutocompleteList>
          {(city: string) => (
            <AutocompleteItem key={city} value={city}>
              {city}
            </AutocompleteItem>
          )}
        </AutocompleteList>
      </AutocompleteContent>
    </Autocomplete>
  );
}

Notes

Autocomplete or Combobox

Autocomplete holds free text. When the value must be one of the options, use Combobox.

Inside a Dialog or Drawer

AutocompleteContent portals by default. Pass portal={false} inside a Dialog or Drawer to keep their stacking.

API

items
agent
T[] | { value, items: T[] }[]
The suggestions, flat or grouped. The agent reads them while closed.
valuestring
Controlled input text.
defaultValuestring = ""
Starting text when uncontrolled.
onValueChange
agent
(value, details) => void
Runs for people and agents. details.cancel() refuses the change.
openboolean
Controlled open state.
onOpenChange
agent
(open, details) => void
Runs when the list opens or closes, from either side. An agent's search opens it first.
mode"list" | "both" | "inline" | "none" = "list"
list filters the items; both also completes inline; inline completes without filtering; none does neither.
openOnInputClickboolean = false
Opens the list when the input is clicked.
autoHighlightboolean | "always" = false
Highlights the first match while typing, or always.
keepHighlightboolean = false
Keeps the highlight when the pointer leaves the list.
itemToStringValue(item) => string
Text for an object item. A { value, label } item defaults to its label.
disabled
agent
boolean = false
Blocks people and agents.
readOnly
agent
boolean = false
Blocks typing, and refuses the agent's text.
namestring
Submits the text with a form.

Agent

Inside an AgentProvider, it appears in agent.snapshot() as combobox.

Reads

label

The input's aria-label or label, then its placeholder.

value

The text in the input.

items

The suggestions showing while open, or every item passed as items.

disabled

True when the autocomplete is blocked.

Actions

search("to")

Opens the list, types the text, returns the shown suggestions.

setValue("Lisbon")

Sets the text outright, even when no suggestion matches.

An agent's text runs through onValueChange, and search's open through onOpenChange. isAgentChange(details) is true for both; details.cancel() refuses either, and a refused search leaves text and list unchanged. disabled and readOnly refuse on their own.

await agent.act({
  steps: [{ ref: "Search cities", action: "search", args: ["to"] }],
})

Accessibility

  • The input has role combobox, with aria-expanded and aria-controls on the list.
  • The list is a listbox of options; aria-activedescendant keeps focus in the input.
  • Name the input with a label or aria-label; a placeholder disappears on typing.
  • AutocompleteStatus is hidden and announced, for counts such as matching results.