Combobox

A text input that filters a list, searched and picked by a person or an agent.

Source
$npx @dinachi/cli@latest add combobox
placeholder
ComboboxClear
autoHighlight
disabled

Usage

import {
  Combobox,
  ComboboxContent,
  ComboboxEmpty,
  ComboboxInput,
  ComboboxInputGroup,
  ComboboxItem,
  ComboboxList,
  ComboboxTrigger,
} from "@/components/ui/combobox"

const fruits = ["Apple", "Banana", "Cherry", "Mango"]

export function FruitPicker() {
  return (
    <Combobox items={fruits}>
      <label htmlFor="fruit" className="text-sm font-medium">Fruit</label>
      <ComboboxInputGroup className="flex w-64 items-center rounded-md border">
        <ComboboxInput id="fruit" placeholder="Search fruit" />
        <ComboboxTrigger />
      </ComboboxInputGroup>
      <ComboboxContent>
        <ComboboxEmpty>No fruit found.</ComboboxEmpty>
        <ComboboxList>
          {(fruit: string) => (
            <ComboboxItem key={fruit} value={fruit}>
              {fruit}
            </ComboboxItem>
          )}
        </ComboboxList>
      </ComboboxContent>
    </Combobox>
  )
}

Examples

Type to filter a list, then pick one.
import {
  Combobox,
  ComboboxInputGroup,
  ComboboxInput,
  ComboboxTrigger,
  ComboboxContent,
  ComboboxList,
  ComboboxItem,
  ComboboxEmpty,
} from '@/components/ui/combobox';

const frameworks = ["React", "Vue", "Angular", "Svelte", "Solid", "Next.js", "Nuxt", "Remix"];

export function Example() {
  return (
    <Combobox items={frameworks} openOnInputClick>
      <div className="space-y-2">
        <label htmlFor="combobox-framework" className="block text-sm font-medium leading-none">Framework</label>
        <ComboboxInputGroup 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">
          <ComboboxInput id="combobox-framework" placeholder="Search frameworks..." />
          <ComboboxTrigger />
        </ComboboxInputGroup>
      </div>
      <ComboboxContent>
        <ComboboxEmpty>No frameworks found.</ComboboxEmpty>
        <ComboboxList>
          {(fw: string) => (
            <ComboboxItem key={fw} value={fw}>
              {fw}
            </ComboboxItem>
          )}
        </ComboboxList>
      </ComboboxContent>
    </Combobox>
  );
}

Notes

Selecting by ID

createComboboxItems derives each item's value and label, so value and onValueChange use IDs while the list receives the full object. Create the collection at module scope, or memoize it for runtime data:

const userItems = React.useMemo(
  () => createComboboxItems(users, { getValue: (user) => user.id, getLabel: (user) => user.name }),
  [users]
)

Naming the input

ComboboxLabel names the trigger. Name the input with a <label htmlFor> on its id, or aria-label, as in the default example. The agent reads the input's name.

Rendering in place

ComboboxContent portals by default. Pass portal={false} to render inside its parent and inherit its stacking.

API

items
agent
Item[] | Group[] | ItemCollection
The options. Needed for filtering; lets the agent pick while closed.
valueValue | Value[] | null
Controlled value.
defaultValueValue | Value[] | null = null, or [] when multiple
Starting value when uncontrolled.
onValueChange
agent
(value, details) => void
Runs for people and agents. details.cancel() refuses the change.
onInputValueChange
agent
(text, details) => void
Runs as the input text changes, including when an agent searches.
openboolean
Controlled open state.
defaultOpenboolean = false
Starting open state when uncontrolled.
onOpenChange
agent
(open, details) => void
Runs when the list opens or closes, from either side.
multiple
agent
boolean = false
Holds an array of values. Agents also get setValue.
openOnInputClickboolean = true
Opens the list when the input is clicked.
autoHighlightboolean = false
Highlights the first match while filtering.
itemToStringLabel
agent
(value) => string
Turns an object value into its label. The agent matches by it too.
filter(item, query) => boolean | null
Replaces the built-in filter. null turns filtering off.
disabled
agent
boolean = false
Blocks people and agents.
readOnly
agent
boolean = false
Refuses any change, from a person or an agent.
namestring
Submits the value with a form.
requiredboolean = false
Must have a value before the form submits.

Agent

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

Reads

label

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

value

The chosen label, a list of labels when multiple, or null.

open

Whether the list is showing.

items

Every option's label from items, or what the open list shows.

multiple

Present when the combobox holds several values.

disabled

True when the combobox is blocked.

Actions

select("Svelte")

Picks by label, then by value, open or closed. A miss lists the options.

search("sv")

Types into the input and returns the labels that match.

open() / close()

Whichever fits the current state.

setValue(["Vue", "Svelte"])

Replaces the whole list. Only when multiple.

An agent's change runs through onValueChange, onInputValueChange and onOpenChange like anyone's. isAgentChange(details) is true for it, and details.cancel() refuses it; the agent is told the app refused. A disabled combobox refuses on its own, and readOnly refuses select, setValue and search.

await agent.act({
  steps: [{ ref: "Framework", action: "select", args: ["Svelte"] }],
})

Accessibility

  • The input has role="combobox" and controls the list popup.
  • Give the input a name with a <label htmlFor> or aria-label. The agent reads the same name.
  • ComboboxEmpty and ComboboxStatus announce changes politely.
  • The list is a listbox; focus stays in the input as the highlight moves.