Components / Selection

Combo Box07

A text field with a list that filters as you type. The matching letters light up in every option, and Turkish capitals match the way they should.

ComboBox · PlaygroundLive
JSX
<ComboBox label="City" placeholder="Start typing…">
  <ComboBoxItem id="istanbul">Istanbul</ComboBoxItem>
  <ComboBoxItem id="izmir">Izmir</ComboBoxItem>
  <ComboBoxItem id="lisbon">Lisbon</ComboBoxItem>
  <ComboBoxItem id="tokyo">Tokyo</ComboBoxItem>
</ComboBox>

Usage

TSX
import { ComboBox, ComboBoxItem } from "plonk-ui";

export function City() {
  return (
    <ComboBox label="City" name="city">
      <ComboBoxItem id="istanbul">Istanbul</ComboBoxItem>
      <ComboBoxItem id="lisbon">Lisbon</ComboBoxItem>
    </ComboBox>
  );
}

Use a combo box when the list is long enough that typing is faster than scrolling; for short lists, a Select is simpler.

Options from data

defaultItems and a render function build the list from an array, and the combo box filters it for you. value and onChange work with the option's id. When nothing matches, the list says so with emptyMessage.

Combo Box · From dataLive
Value: tr
import { useState } from "react";
import { ComboBox, ComboBoxItem } from "plonk-ui";

const LANGUAGES = [
  { id: "en", name: "English", native: "English" },
  { id: "tr", name: "Turkish", native: "Türkçe" },
  { id: "de", name: "German", native: "Deutsch" },
  { id: "ja", name: "Japanese", native: "日本語" },
  { id: "pt", name: "Portuguese", native: "Português" },
  { id: "es", name: "Spanish", native: "Español" },
];

export default function Items() {
  const [language, setLanguage] = useState<string | null>("tr");

  return (
    <div className="flex w-full max-w-xs flex-col gap-3">
      <ComboBox
        label="Language"
        defaultItems={LANGUAGES}
        value={language}
        onChange={(key) => setLanguage(key == null ? null : String(key))}
        emptyMessage="No language by that name"
      >
        {(item) => (
          <ComboBoxItem description={item.native}>{item.name}</ComboBoxItem>
        )}
      </ComboBox>
      <span className="font-bold">Value: {language ?? "none"}</span>
    </div>
  );
}

Filtering matches the option's text, not its description. Matching and the highlight follow the provider's locale, so in Turkish "iz" finds "İzmir".

Custom values

allowsCustomValue keeps whatever was typed, even if no option matches. Read it with inputValue and onInputChange.

Combo Box · Custom valuesLive
Pick one or type a new one.
Tag: none
TSX
import { useState } from "react";
import { ComboBox, ComboBoxItem } from "plonk-ui";

const TAGS = ["design", "frontend", "research", "urgent"];

export default function Custom() {
  const [tag, setTag] = useState("");

  return (
    <div className="flex w-full max-w-xs flex-col gap-3">
      <ComboBox
        label="Tag"
        description="Pick one or type a new one."
        allowsCustomValue
        inputValue={tag}
        onInputChange={setTag}
      >
        {TAGS.map((t) => (
          <ComboBoxItem key={t} id={t}>
            {t}
          </ComboBoxItem>
        ))}
      </ComboBox>
      <span className="font-bold">Tag: {tag || "none"}</span>
    </div>
  );
}

Personalities

The field lifts on focus like Input, and the list opens like a Menu. Matched letters are marked with a highlighter stroke. The highlight hooks work here too.

Accessibility

  • The input keeps focus while the list is open; ↑ and ↓ move through the options and Enter picks one.
  • Esc closes the list.
  • Screen readers hear how many options match as you type.
  • The label, description and error are linked to the input.

API

ComboBox

PropTypeDefaultDescription
placeholderstring—Shown while the field is empty.
emptyMessageReactNode"No results"Shown in the list when nothing matches.
classNamestring—Extra classes for the outer wrapper.
labelReactNode—Visible label, linked to the field.
descriptionReactNode—Help text under the field.
errorReactNode—Error message. Marks the field invalid and shakes it once. Pass true to mark it without a message.
valueKey | null—Controlled value: the selected option's id, or null.
defaultValueKey | null—Selected option on first render, when uncontrolled.
onChange(value: Key | null) => void—Called with the new option's id.
inputValuestring—Controlled text in the field.
onInputChange(value: string) => void—Called as the text changes.
defaultItemsIterable<T>—Options as data, with a render function as children. The combo box filters them as you type.
allowsCustomValueboolean—Keeps text that matches no option instead of resetting it.
menuTrigger"input" | "manual" | "focus""input"When the list opens: input (as you type), focus, or manual (arrow keys and the button only).
namestring—Field name for form submission.
isRequiredboolean—Marks the field required, with an asterisk in the label.
isDisabledboolean—Disables the field.

ComboBoxItem

PropTypeDefaultDescription
classNamestring—Extra classes for the row.
iconReactNode—Icon before the label. Hidden from screen readers.
descriptionReactNode—Smaller text under the label, read with it.
idKey—Unique key, used for selection and in callbacks.
isDisabledboolean—Can't be focused or chosen.
textValuestring—Plain text for type-to-search. Set it when the label isn't a string.