Combobox
A text box with a list under it: the person types a few letters and the list keeps only what matches. For long lists where scrolling would take too long.
import { Combobox } from '@sypnia/ui/react'
Type to filter
Type «ord» or «log»: the list narrows to the tables that contain it.
Every row of orders goes into one CSV file.
Code
import { useState } from 'react';
import { Combobox, Field, FormGrid, type Option } from '@sypnia/ui/react';
const tables: Option[] = [
{ value: 'customers', label: 'customers', description: '12,480 rows', icon: 'table' },
{ value: 'orders', label: 'orders', description: '98,204 rows', icon: 'table' },
{ value: 'order_items', label: 'order_items', description: '311,930 rows', icon: 'table' },
{ value: 'products', label: 'products', description: '642 rows', icon: 'table' },
{ value: 'invoices', label: 'invoices', description: '40,112 rows', icon: 'table' },
{ value: 'audit_log', label: 'audit_log', description: '2,904,551 rows', icon: 'table' },
{ value: 'sessions', label: 'sessions', description: '77,310 rows', icon: 'table' },
];
export default function Basic() {
const [table, setTable] = useState<string | null>('orders');
return (
<FormGrid>
<Field label="Table to export" hint={table ? `Every row of ${table} goes into one CSV file.` : 'Type a few letters of its name.'}>
<Combobox options={tables} value={table} onChange={setTable} placeholder="Find a table" />
</Field>
</FormGrid>
);
} Nothing matches
Type «zebra». The list says there is nothing, and what to do about it.
Code
import { Combobox, Field, FormGrid, type Option } from '@sypnia/ui/react';
const members: Option[] = [
{ value: 'laura', label: 'Laura Méndez', description: 'laura@northwind.dev' },
{ value: 'diego', label: 'Diego Ruiz', description: 'diego@northwind.dev' },
{ value: 'ana', label: 'Ana Torres', description: 'ana@northwind.dev' },
];
export default function Empty() {
return (
<FormGrid>
<Field label="Assign to">
<Combobox options={members} placeholder="Find a member" emptyText="Nobody in this project has that name. Invite them from the team page first." />
</Field>
</FormGrid>
);
} Options from a server
Each letter asks a (pretend) server. While the answer travels the list shows rows of skeleton, never a spinner.
Reports and backups use this time.
Code
import { useEffect, useState } from 'react';
import { Combobox, Field, FormGrid, type Option } from '@sypnia/ui/react';
const zones = ['America/Bogota', 'America/Lima', 'America/Mexico_City', 'America/New_York', 'America/Sao_Paulo', 'Europe/Dublin', 'Europe/Madrid', 'Europe/Berlin', 'Asia/Singapore', 'Asia/Tokyo'];
// Stands in for a request: answers after a moment with the zones that contain the text.
const search = (text: string) => new Promise<Option[]>((resolve) => {
setTimeout(() => resolve(zones.filter((z) => z.toLowerCase().includes(text.toLowerCase())).map((z) => ({ value: z, label: z.replace('_', ' ') }))), 900);
});
export default function Loading() {
const [text, setText] = useState('');
const [options, setOptions] = useState<Option[]>([]);
const [loading, setLoading] = useState(true);
useEffect(() => {
let current = true;
setLoading(true);
void search(text).then((found) => { if (current) { setOptions(found); setLoading(false); } });
return () => { current = false; };
}, [text]);
return (
<FormGrid>
<Field label="Time zone" hint="Reports and backups use this time.">
<Combobox options={options} loading={loading} filter={false} onInputChange={setText} placeholder="Find a city" />
</Field>
</FormGrid>
);
} States
With an error that says how to fix it, and disabled.
Choose a table from the list: «ordrs» does not exist in this database.
This project has one database.
Code
import { Combobox, Field, FormGrid, type Option } from '@sypnia/ui/react';
const tables: Option[] = [{ value: 'orders', label: 'orders' }, { value: 'customers', label: 'customers' }, { value: 'invoices', label: 'invoices' }];
export default function States() {
return (
<FormGrid>
<Field label="Table to export" error="Choose a table from the list: «ordrs» does not exist in this database.">
<Combobox options={tables} placeholder="Find a table" />
</Field>
<Field label="Database" hint="This project has one database.">
<Combobox disabled options={[{ value: 'main', label: 'main' }]} defaultValue="main" />
</Field>
</FormGrid>
);
} When to use it
Use it when
- One choice from a long list the person can name: a table, a time zone, a member, a country.
- The options come from a server as the person types: pass
onInputChange,loadingandfilter={false}. - The list may come back empty: it always says so, in words.
Do not use it when
- There are fewer than about ten options: a Select shows them all at a glance.
- The person may write anything, not only what is in the list: that is an Input (with suggestions, an Autocomplete).
- Several can be chosen: MultiSelect.
For everyone
- Typing filters; the down arrow opens the list and moves through it; Enter chooses; Escape closes the list, and a second Escape clears what was typed.
- It is announced as a
combobox; the number of options that match is announced as it changes. - While options load, the list holds rows of skeleton and is marked busy. Sypnia never shows a spinner.
- Inside a Field it takes the label, the hint and the error by itself; alone, give it an
aria-label.
Props
| Combobox | Type | Default |
|---|---|---|
optionsrequired | Option[] | |
aria-describedby | string | |
aria-labelThe name for screen readers when there is no Field around it. | string | |
aria-labelledby | string | |
className | string | |
defaultValue | string | |
disabled | boolean | |
emptyTextWhat the list says when nothing matches. | ReactNode | |
filterSet false when the options already come filtered (from a server). By default the list keeps the options that contain what was typed. | boolean | true |
idInside a Field it comes from the Field. | string | |
invalidMarks the value as wrong. Inside a `<Field error>` it is set for you. | boolean | |
loadingOptions are on their way: the list shows rows of skeleton. | boolean | |
name | string | |
onChangeThe chosen value, or null when the person clears the text. | ((value: string | null) => void) | |
onInputChangeCalled with what the person types: use it to ask a server for options. | ((text: string) => void) | |
placeholder | string | |
size | "sm" | "md" | "lg" | |
value | string | null |