Skip to content

Combobox

Download .md

A combobox is a text field with a list: people type to narrow the list, then pick one value with the mouse or the keyboard.

  • Use it for more than 15 options, or whenever people know what they look for, such as an artist, a song or a company.
  • Use a Select for 4 to 15 options, and a Radio Group for 2 or 3.
  • Write the empty text for the list, such as “Artis tidak ditemukan.”, so people know the search worked.

Searching on the server. When the list lives in the backend, search as people type: wait about 300ms after the last key, keep the previous results while the next ones load, and show a Spinner in the list. The backend decides what matches.

React
Vue
1234
  1. 1Input to type in
  2. 2Typed text and caret
  3. 3Matching option
  4. 4List
  1. Input: a text field on radius-md with an input border, h-9 tall.
  2. List: a popup on popover with a border, radius-lg and shadow-lg, as wide as the input.
  3. Item: radius-md; the highlighted item is accent and the chosen one has a check icon.
  4. Empty text: shown when nothing matches.

There is one style. React also has chips (ComboboxChips) for picking several values.

React Vue Flutter Type Default Use
items on Combobox ComboboxItem children options on ShadSelect.withSearch list none The options.
value / defaultValue v-model initialValue value none The chosen value.
onValueChange @update:model-value onChanged function none Runs when a value is picked.
placeholder on ComboboxInput placeholder on ComboboxInput placeholder text none Shown before a choice.
ComboboxEmpty ComboboxEmpty an empty row in options text none Says nothing matched.
onInputValueChange @update:model-value on ComboboxInput onSearchChanged function none Runs as people type, for server search.
import { useId } from "react"
import {
Combobox,
ComboboxContent,
ComboboxEmpty,
ComboboxInput,
ComboboxItem,
ComboboxList,
} from "@/components/ui/combobox"
import { Field, FieldLabel } from "@/components/ui/field"
const artists = ["Rara Senandika", "Bima Laras", "Nada Kirana", "Arka Wening", "Sinta Maharani", "Raka Pratama"]
export default function ComboboxDemo() {
const id = useId()
return (
<Field className="w-full max-w-xs">
<FieldLabel htmlFor={`${id}-artist`}>Artis</FieldLabel>
<Combobox items={artists}>
<ComboboxInput id={`${id}-artist`} placeholder="Pilih artis" />
<ComboboxContent>
<ComboboxEmpty>Artis tidak ditemukan.</ComboboxEmpty>
<ComboboxList>
{(artist: string) => (
<ComboboxItem key={artist} value={artist}>
{artist}
</ComboboxItem>
)}
</ComboboxList>
</ComboboxContent>
</Combobox>
</Field>
)
}
Terminal window
npx shadcn@latest add https://design.sosialoka.tech/r/react/combobox.json
  • The input has role="combobox" and a visible label; the list has role="listbox" and its items role="option".
  • Typing filters the list. Arrow keys move through the items, Enter picks one, and Escape closes the list without changing the value.
  • The empty text is read when nothing matches.