8.4k

Autocomplete

An input that suggests options as you type.

API Reference

Installation

pnpm dlx shadcn@latest add https://coss.com/ui/r/autocomplete.json

Usage

import {
  Autocomplete,
  AutocompleteEmpty,
  AutocompleteInput,
  AutocompleteItem,
  AutocompleteList,
  AutocompletePopup,
} from "@/components/ui/autocomplete"
const items = [
  { value: "apple", label: "Apple" },
  { value: "banana", label: "Banana" },
  { value: "orange", label: "Orange" },
  { value: "grape", label: "Grape" },
]
 
<Autocomplete items={items}>
  <AutocompleteInput placeholder="Search..." />
  <AutocompletePopup>
    <AutocompleteEmpty>No results found.</AutocompleteEmpty>
    <AutocompleteList>
      {(item) => <AutocompleteItem key={item.value} value={item}>{item.label}</AutocompleteItem>}
    </AutocompleteList>
  </AutocompletePopup>
</Autocomplete>

API Reference

The AutocompleteInput component extends the original Base UI component with a few extra props:

PropTypeDefaultDescription
size"sm" | "default" | "lg""default"The size variant of the input field.
showTriggerbooleanfalseWhether to display a trigger button (chevron icon) on the right side of the input.
showClearbooleanfalseWhether to display a clear button (X icon) on the right side of the input when there is a value.

Examples

Disabled

Small Size

Large Size

With Label

Inline Autocomplete

Autofill the input with the highlighted item while navigating with arrow keys.

Auto Highlight

Automatically highlight the first matching option.

With Clear Button

With Trigger and Clear Buttons

With Groups

With Limit Results

Form Integration

coss.com ui

Built by and for the team of Cal.com, Inc. — the leading commercial open source company (“coss”).