Combobox

A searchable dropdown component that combines an input with a list of options.

Installation

pnpm dlx bna-ui add combobox

Usage

When managing state for the combobox, you will work with an OptionType object ({ value: string; label: string; }) or an array of these objects for multiple selections.

import {
  Combobox,
  ComboboxContent,
  ComboboxEmpty,
  ComboboxGroup,
  ComboboxInput,
  ComboboxItem,
  ComboboxList,
  ComboboxTrigger,
  ComboboxValue,
  OptionType, // Import the type
} from '@/components/ui/combobox';
import { useState } from 'react';
 
// ...
 
const [value, setValue] = useState<OptionType | null>(null);
 
// ...
 
<Combobox value={value} onValueChange={setValue}>
  <ComboboxTrigger>
    <ComboboxValue placeholder='Select framework...' />
  </ComboboxTrigger>
  <ComboboxContent>
    <ComboboxInput placeholder='Search frameworks...' />
    <ComboboxList>
      <ComboboxEmpty>No framework found.</ComboboxEmpty>
      <ComboboxItem value='react'>React</ComboboxItem>
      <ComboboxItem value='vue'>Vue</ComboboxItem>
      <ComboboxItem value='angular'>Angular</ComboboxItem>
    </ComboboxList>
  </ComboboxContent>
</Combobox>;

Examples

Default

With Groups

Multiple Selection

Disabled

Form Integration

Large Dataset

API Reference

The Combobox component operates on an OptionType object for its state, which has the shape { value: string; label: string; }. The value prop of ComboboxItem should still be a unique string.

Combobox

The root component that manages the state and context for all child components.

PropTypeDefaultDescription
childrenReactNode-The combobox components.
valueOptionType | nullnullThe selected option object (for single selection).
onValueChange(option: OptionType | null) => void-Callback when a single option object changes.
valuesOptionType[][]An array of selected option objects (multiple selection).
onValuesChange(options: OptionType[]) => void-Callback when the array of selected options changes.
disabledbooleanfalseIf true, the combobox is disabled.
multiplebooleanfalseIf true, allows multiple selections.

ComboboxTrigger

The button that triggers the combobox dropdown.

PropTypeDefaultDescription
childrenReactNode-The trigger content (usually ComboboxValue).
styleViewStyle-Additional styles for the trigger.
errorbooleanfalseIf true, shows error styling on the border.

ComboboxValue

Displays the selected value(s) or placeholder text.

PropTypeDefaultDescription
placeholderstring"Select..."Placeholder text when no value is set.
styleTextStyle-Additional styles for the text.

ComboboxContent

The modal container for the dropdown content.

PropTypeDefaultDescription
childrenReactNode-The dropdown content.
maxHeightnumber400Maximum height of the dropdown.

ComboboxInput

The search input field within the dropdown.

PropTypeDefaultDescription
placeholderstring"Search..."Placeholder text for the input.
styleViewStyle-Additional styles for the container.
autoFocusbooleantrueIf true, auto-focuses the input.

ComboboxList

A scrollable container for the list of options with filtering capability.

PropTypeDefaultDescription
childrenReactNode-The list items and groups.
styleViewStyle-Additional styles for the list.

ComboboxEmpty

Displays when no items match the search query.

PropTypeDefaultDescription
childrenReactNode-The empty state content.
styleViewStyle-Additional styles for the container.

ComboboxGroup

Groups related options with an optional heading.

PropTypeDefaultDescription
childrenReactNode-The group items.
headingstring-Optional heading for the group.

ComboboxItem

An individual selectable option within the combobox.

PropTypeDefaultDescription
childrenReactNode-The item content. This is used as the label for the option.
valuestring-The unique value of the item.
onSelect(value: OptionType) => void-Callback when item is selected, receiving the full option object.
disabledbooleanfalseIf true, the item cannot be selected.
searchValuestring-A custom string to use for search filtering instead of the label.
styleViewStyle-Additional styles for the item.