import { ComboBox } from "@senvenli/react/selection";
const CUSTOMERS = [
{ id: "ada", label: "Ada Lovelace", description: "ada@example.com" },
{ id: "alan", label: "Alan Turing", description: "alan@example.com" },
{ id: "grace", label: "Grace Hopper", description: "grace@example.com" },
{ id: "katherine", label: "Katherine Johnson", description: "katherine@example.com" },
{ id: "linus", label: "Linus Torvalds", description: "linus@example.com" },
{ id: "margaret", label: "Margaret Hamilton", description: "margaret@example.com" },
];
export default function Example() {
return (
<div className="max-w-80">
<ComboBox label="Customer" placeholder="Name or email" items={CUSTOMERS} defaultSelectedKey="ada" />
</div>
);
}import { ComboBox } from "@senvenli/react/selection";Examples
Several items
Chosen items become removable tags in the field.import { ComboBox } from "@senvenli/react/selection";
const MEMBERS = [
{ id: "ada", label: "Ada Lovelace", description: "Engineering" },
{ id: "alan", label: "Alan Turing", description: "Engineering" },
{ id: "grace", label: "Grace Hopper", description: "Operations" },
{ id: "katherine", label: "Katherine Johnson", description: "Finance" },
{ id: "margaret", label: "Margaret Hamilton", description: "Product", isDisabled: true },
];
export default function Example() {
return (
<div className="max-w-96">
<ComboBox
label="Reviewers"
description="Margaret is on leave this week."
selectionMode="multiple"
menuTrigger="focus"
items={MEMBERS}
defaultSelectedKeys={["ada", "grace"]}
/>
</div>
);
}Create a missing item
A create row appears when nothing matches the text exactly.import { ComboBox, type ComboBoxItem } from "@senvenli/react/selection";
import { useState } from "react";
export default function Example() {
const [labels, setLabels] = useState<ComboBoxItem[]>([
{ id: "bug", label: "Bug" },
{ id: "billing", label: "Billing" },
{ id: "feature", label: "Feature request" },
]);
const [label, setLabel] = useState<string | number | null>(null);
return (
<div className="max-w-80">
<ComboBox
label="Label"
placeholder="Find or create a label"
items={labels}
selectedKey={label ?? undefined}
onSelectionChange={setLabel}
createLabel={(text) => `Create “${text}”`}
onCreate={(text) => {
setLabels((current) => [...current, { id: text, label: text }]);
setLabel(text);
}}
/>
</div>
);
}Loaded from the server
A loadItems function fetches matches as people type and pages by cursor.import { ComboBox, type ComboBoxLoader } from "@senvenli/react/selection";
const PAGE = 5;
const ORDERS = Array.from({ length: 40 }, (_, index) => {
const number = 1040 - index;
return { id: `ORD-${number}`, label: `ORD-${number}`, description: index % 3 === 0 ? "Northwind Traders" : "Acme Supplies" };
});
// Stands in for `fetch("/api/orders?q=…&cursor=…", { signal })`.
const loadOrders: ComboBoxLoader = async (filterText, cursor) => {
await new Promise((resolve) => setTimeout(resolve, 400));
const query = filterText.toLowerCase();
const matches = ORDERS.filter((order) => `${order.label} ${order.description}`.toLowerCase().includes(query));
const start = Number(cursor ?? 0);
const next = start + PAGE;
return { items: matches.slice(start, next), cursor: next < matches.length ? String(next) : undefined };
};
export default function Example() {
return (
<div className="max-w-80">
<ComboBox label="Order" placeholder="Order number or customer" menuTrigger="focus" loadItems={loadOrders} />
</div>
);
}Guidelines
When to use it
- Picking from a long list (customers, products, countries) by typing.
Accessibility
Keyboard
| Keys | What happens |
|---|---|
| Arrow down | Opens the list. |
| Enter | Picks the focused option. |
Built in
- Announces the result count and "No results".
- Matches ignore case and accents.
- Required fields show "Fill in this field." after a blur or a submit; your own
errorMessagereplaces it.
You provide
- A visible label.
API reference
ComboBox
| Prop | Type | Default | Description |
|---|---|---|---|
label | React.ReactNode | – | The label sits above the field. |
items | readonly ComboBoxItem[] | – | The whole list, filtered here as the user types. |
loadItems | ComboBoxLoader | – | Or load matches from the server (with paging when it returns a cursor). |
description | React.ReactNode | – | |
errorMessage | React.ReactNode | – | |
placeholder | string | – | |
isRequired | boolean | – | |
requiredValidation | RequiredValidation | – | Who says an empty required ComboBox is missing: |
isDisabled | boolean | – | |
isInvalid | boolean | – | |
name | string | – | |
menuTrigger | "focus" | "input" | – |
|
emptyState | React.ReactNode | ((query: string) => React.ReactNode) | – | The empty result: a node, or a function of the typed text for a message that quotes it. Defaults to "No results for “{query}”." in the provider locale. |
onCreate | (text: string) => void | – | Adds a create row for the typed text when nothing matches it exactly. |
createLabel | (text: string) => string | – | The create row's text; defaults to "Add “{query}”". |
onInputChange | (text: string) => void | – | The typed text, for consumers that need it (custom values, analytics). |
size | FieldSize | – | |
isLoading | boolean | – | The app is fetching items itself (with |
loadingText | string | – | What is announced and shown while loading; defaults to "Loading…" in the provider locale. |
onSearchChange | (search: string) => void | – | The app searches on the server itself (with The field hides rows found for another text itself: from the moment the text changes until the app's answer arrives, the list shows its loading row, the field its spinner, and the loading is announced politely, whatever the app does while it debounces. The answer has arrived when the rows change (ids, labels, second lines), when It is for a search that answers later. Filtering the rows the app already holds, synchronously, is the local filter's job (pass |
onLoadMore | () => void | – | The next page of the app's own |
id | string | – | The input's id, so a label outside the component ( |
aria-describedby | string | – | Ids of text outside the component that describes the input (an app's hint or error), added to its own. |
aria-invalid | boolean | "true" | "false" | – | Set by a form row that owns the error text (the shared |
selectionMode | "single" | "multiple" | – | |
selectedKey | Key | – | Controlled; may start undefined and become set later (never flips control). |
defaultSelectedKey | Key | – | |
onSelectionChange | (key: Key | null) => void | (keys: Key[]) => void | – | |
selectedItem | ComboBoxItem | – | The chosen item when it is not on the loaded page (server search), so its label still shows. |
allowsCustomValue | boolean | – | Typed text that matches no item is kept as the field's text; the key becomes |
radius | ComponentRadius | – | Corner radius: |
aria-label | string | – | Names the field for assistive technology when no visible label is shown. |
selectedKeys | readonly Key[] | – | |
defaultSelectedKeys | readonly Key[] | – | |
selectedItems | readonly ComboBoxItem[] | – | Chosen items that are not on the loaded page, so their tags show their labels. |
labelReact.ReactNodeThe label sits above the field.
itemsreadonly ComboBoxItem[]The whole list, filtered here as the user types.
loadItemsComboBoxLoaderOr load matches from the server (with paging when it returns a cursor).
descriptionReact.ReactNodeerrorMessageReact.ReactNodeplaceholderstringisRequiredbooleanrequiredValidationRequiredValidationWho says an empty required ComboBox is missing:
field(default) shows "Fill in this field." on blur and on a DS Form submit, which it then blocks;apponly marks the field. Empty is no chosen item (multiple: no tag); withallowsCustomValue, typed text counts as a value. See {@link RequiredValidation}. The app'sisInvalid(truewith itserrorMessage, orfalse) overrides the check, as on TextField. Disabled, empty and required, it still blocks a DS Form's submit and cannot take focus (as TextField).isDisabledbooleanisInvalidbooleannamestringmenuTrigger"focus" | "input"input(default) opens on typing;focusalso on focus. The chevron always opens the full list.emptyStateReact.ReactNode | ((query: string) => React.ReactNode)The empty result: a node, or a function of the typed text for a message that quotes it. Defaults to "No results for “{query}”." in the provider locale.
onCreate(text: string) => voidAdds a create row for the typed text when nothing matches it exactly.
createLabel(text: string) => stringThe create row's text; defaults to "Add “{query}”".
onInputChange(text: string) => voidThe typed text, for consumers that need it (custom values, analytics).
sizeFieldSizeisLoadingbooleanThe app is fetching items itself (with
items, notloadItems): a spinner shows in the field and at the end of the list, and the change is announced. Items already shown stay selectable by keyboard and pointer. Together withloadItems, the field's own loading status is the one announced, so there is a single status region.loadingTextstringWhat is announced and shown while loading; defaults to "Loading…" in the provider locale.
onSearchChange(search: string) => voidThe app searches on the server itself (with
items, notloadItems;): told the text to search for, the typed text, or""when the list opens in full (the chevron, focus withmenuTrigger="focus") or the field just shows the chosen item's label. Debounce it on the app side. With it,itemsare the app's results and are shown as given, not filtered again here; the match in each row is still in bold. Not called on mount.The field hides rows found for another text itself: from the moment the text changes until the app's answer arrives, the list shows its loading row, the field its spinner, and the loading is announced politely, whatever the app does while it debounces. The answer has arrived when the rows change (ids, labels, second lines), when
isLoadingis set and cleared again (also for a server that finds the same rows), or, if neither, once the same rows have stood for a second with no new render. Nothing is asked of the app for it;isLoadingis still the way to say a fetch is running.It is for a search that answers later. Filtering the rows the app already holds, synchronously, is the local filter's job (pass
itemswithoutonSearchChange): here an answer with the very same rows and noisLoadingstays hidden for that second. The field tells answers apart by when they arrive, not by the text they were asked for: an earlier request that answers after the next keystroke counts as the answer to the newer text. Abort superseded requests, or keep each answer under its own text (react-query's query key, withoutplaceholderData).onLoadMore() => voidThe next page of the app's own
items, for an app that pages with its own query (react-query's infinite query): called when the list is scrolled near its end. Pass it only while there is another page (hasNextPage ? fetchNextPage : undefined), and setisLoadingwhile the page loads: the list then ends in a loading row, the field shows its spinner and the loading is announced (politely). It is not called again whileisLoadingis set, nor twice before the app answers (a new page,isLoading, or a new search). The app's error stays the app's (errorMessage,description); after it, a scroll to the end asks again. WithloadItems, which pages by itself, it is not used.idstringThe input's id, so a label outside the component (
<label htmlFor={id}>, an app's form field) names it. With neitherlabelnoraria-label, that external label is the field's name and no label of its own renders.aria-describedbystringIds of text outside the component that describes the input (an app's hint or error), added to its own.
aria-invalidboolean | "true" | "false"Set by a form row that owns the error text (the shared
Fieldhands its controlaria-invalid): the field shows its invalid state as withisInvalid, and without anerrorMessageof its own draws no error row, so the row's message is the only one. It marks the input for assistive tech and draws it invalid only: unlikeisInvalidit never becomes the input's native validity (React Aria's default outside a DS Form), so it never blocks a form's submit.selectionMode"single" | "multiple"selectedKeyKeyControlled; may start undefined and become set later (never flips control).
defaultSelectedKeyKeyonSelectionChange(key: Key | null) => void | (keys: Key[]) => voidselectedItemComboBoxItemThe chosen item when it is not on the loaded page (server search), so its label still shows.
allowsCustomValuebooleanTyped text that matches no item is kept as the field's text; the key becomes
nullandonInputChangereports the text.radiusComponentRadiusCorner radius:
none,small,medium(the design),largeorfull. The provider's default otherwise.aria-labelstringNames the field for assistive technology when no visible label is shown.
selectedKeysreadonly Key[]defaultSelectedKeysreadonly Key[]selectedItemsreadonly ComboBoxItem[]Chosen items that are not on the loaded page, so their tags show their labels.