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 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
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.
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.
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 andEnterpicks one. Esccloses the list.- Screen readers hear how many options match as you type.
- The label, description and error are linked to the input.
API
ComboBox
| Prop | Type | Default | Description |
|---|---|---|---|
placeholder | string | — | Shown while the field is empty. |
emptyMessage | ReactNode | "No results" | Shown in the list when nothing matches. |
className | string | — | Extra classes for the outer wrapper. |
label | ReactNode | — | Visible label, linked to the field. |
description | ReactNode | — | Help text under the field. |
error | ReactNode | — | Error message. Marks the field invalid and shakes it once. Pass true to mark it without a message. |
value | Key | null | — | Controlled value: the selected option's id, or null. |
defaultValue | Key | null | — | Selected option on first render, when uncontrolled. |
onChange | (value: Key | null) => void | — | Called with the new option's id. |
inputValue | string | — | Controlled text in the field. |
onInputChange | (value: string) => void | — | Called as the text changes. |
defaultItems | Iterable<T> | — | Options as data, with a render function as children. The combo box filters them as you type. |
allowsCustomValue | boolean | — | 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). |
name | string | — | Field name for form submission. |
isRequired | boolean | — | Marks the field required, with an asterisk in the label. |
isDisabled | boolean | — | Disables the field. |
ComboBoxItem
| Prop | Type | Default | Description |
|---|---|---|---|
className | string | — | Extra classes for the row. |
icon | ReactNode | — | Icon before the label. Hidden from screen readers. |
description | ReactNode | — | Smaller text under the label, read with it. |
id | Key | — | Unique key, used for selection and in callbacks. |
isDisabled | boolean | — | Can't be focused or chosen. |
textValue | string | — | Plain text for type-to-search. Set it when the label isn't a string. |