placeholdermodeAutocompleteCleardisabledimport {
Autocomplete,
AutocompleteContent,
AutocompleteEmpty,
AutocompleteInput,
AutocompleteInputGroup,
AutocompleteItem,
AutocompleteList,
AutocompleteTrigger,
} from "@/components/ui/autocomplete"
const cities = ["New York", "San Francisco", "London", "Tokyo", "Toronto", "Paris", "Berlin", "Sydney"]
export function CitySearch() {
return (
<Autocomplete items={cities} openOnInputClick>
<AutocompleteInputGroup className="flex w-[280px] max-w-full items-center gap-1 rounded-md border border-input focus-within:ring-2 focus-within:ring-ring focus-within:ring-offset-2">
<AutocompleteInput aria-label="Search cities" placeholder="Search cities..." />
<AutocompleteTrigger />
</AutocompleteInputGroup>
<AutocompleteContent>
<AutocompleteEmpty>No cities found.</AutocompleteEmpty>
<AutocompleteList>
{(city: string) => (
<AutocompleteItem key={city} value={city}>
{city}
</AutocompleteItem>
)}
</AutocompleteList>
</AutocompleteContent>
</Autocomplete>
)
}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
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>
);
}import {
Autocomplete,
AutocompleteInput,
AutocompleteTrigger,
AutocompleteContent,
AutocompleteList,
AutocompleteItem,
AutocompleteEmpty,
AutocompleteGroup,
AutocompleteGroupLabel,
AutocompleteCollection,
} from '@/components/ui/autocomplete';
const foods = [
{ value: "Fruits", items: ["Apple", "Banana", "Mango"] },
{ value: "Vegetables", items: ["Carrot", "Broccoli", "Spinach"] },
];
export function Example() {
return (
<Autocomplete items={foods} 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 foods..." />
<AutocompleteTrigger />
</div>
<AutocompleteContent>
<AutocompleteEmpty>No results found.</AutocompleteEmpty>
<AutocompleteList>
{(group: { value: string; items: string[] }) => (
<AutocompleteGroup key={group.value}>
<AutocompleteGroupLabel>{group.value}</AutocompleteGroupLabel>
{group.items.map((item) => (
<AutocompleteItem key={item} value={item}>
{item}
</AutocompleteItem>
))}
</AutocompleteGroup>
)}
</AutocompleteList>
</AutocompleteContent>
</Autocomplete>
);
}import {
Autocomplete,
AutocompleteInput,
AutocompleteTrigger,
AutocompleteClear,
AutocompleteContent,
AutocompleteList,
AutocompleteItem,
AutocompleteEmpty,
} from '@/components/ui/autocomplete';
const colors = ["Red", "Blue", "Green", "Purple", "Orange", "Yellow"];
export function Example() {
return (
<Autocomplete items={colors} 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 colors..." />
<AutocompleteClear />
<AutocompleteTrigger />
</div>
<AutocompleteContent>
<AutocompleteEmpty>No colors found.</AutocompleteEmpty>
<AutocompleteList>
{(color: string) => (
<AutocompleteItem key={color} value={color}>
{color}
</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
details.cancel() refuses the change.{ value, label } item defaults to its label.Takes the props of the element it renders.
AutocompleteCollection inside it.Takes the props of the element it renders.
Takes the props of the element it renders.
Takes the props of the element it renders.
Takes the props of the element it renders.
Takes the props of the element it renders.
Takes the props of the element it renders.
Agent
Inside an AgentProvider, it appears in agent.snapshot() as combobox.
Reads
labelThe input's aria-label or label, then its placeholder.
valueThe text in the input.
itemsThe suggestions showing while open, or every item passed as items.
disabledTrue 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.
AutocompleteStatusis hidden and announced, for counts such as matching results.