Primitives

Combobox

A combobox pairs a Popover with a Command menu to build an autocomplete input, letting users search and select from a list of suggestions. Supports single select, multi-select, and form-field usage.


Combobox Examples

Searchable Combobox

Multi-select Combobox

Next.js

Form Field

This is used to tailor the docs shown to you.

Installation

# The combobox is composed from existing primitives, already installed at:
# src/components/ui/shadcn/popover.tsx
# src/components/ui/shadcn/command.tsx
# src/components/ui/shadcn/button.tsx

Usage

'use client'

import * as React from 'react'
import { CheckIcon, ChevronsUpDownIcon } from 'lucide-react'
import { Button } from '@/components/ui/shadcn/button'
import {
  Command,
  CommandEmpty,
  CommandGroup,
  CommandInput,
  CommandItem,
  CommandList,
} from '@/components/ui/shadcn/command'
import {
  Popover,
  PopoverContent,
  PopoverTrigger,
} from '@/components/ui/shadcn/popover'
import { cn } from '@/lib/utils'

const frameworks = [
  { value: 'next.js', label: 'Next.js' },
  { value: 'sveltekit', label: 'SvelteKit' },
]

export function ComboboxDemo() {
  const [open, setOpen] = React.useState(false)
  const [value, setValue] = React.useState('')

  return (
    <Popover open={open} onOpenChange={setOpen}>
      <PopoverTrigger asChild>
        <Button variant="outline" role="combobox" aria-expanded={open}>
          {value || 'Select framework...'}
          <ChevronsUpDownIcon className="opacity-50" />
        </Button>
      </PopoverTrigger>
      <PopoverContent className="p-0">
        <Command>
          <CommandInput placeholder="Search framework..." />
          <CommandList>
            <CommandEmpty>No framework found.</CommandEmpty>
            <CommandGroup>
              {frameworks.map((framework) => (
                <CommandItem
                  key={framework.value}
                  value={framework.value}
                  onSelect={(currentValue) => {
                    setValue(currentValue)
                    setOpen(false)
                  }}
                >
                  <CheckIcon
                    className={cn(value === framework.value ? 'opacity-100' : 'opacity-0')}
                  />
                  {framework.label}
                </CommandItem>
              ))}
            </CommandGroup>
          </CommandList>
        </Command>
      </PopoverContent>
    </Popover>
  )
}

Components

ComponentDescription
PopoverPositions the dropdown relative to the trigger button
PopoverTriggerThe button that opens the combobox
PopoverContentWraps the command menu
CommandThe searchable list container
CommandInputThe search input
CommandListScrollable list of results
CommandEmptyShown when no items match
CommandGroupGroups related items
CommandItemAn individual selectable option
Previous
Collapsible