Demo
Listbox basics
Documented in Listbox.
Listbox basics
A single-selection Listbox where selection follows focus, and a multiple-selection Listbox with checkboxes, a tri-state select all, a five-item limit and a disabled item.
Favourite fruit
Apple
Apricot
Banana
Blueberry
Cherry
Date
Elderberry
Fig
Grape
Kiwi
Lemon
Mango
Value:
Basket (at most 5)
Apple
Apricot
Banana
Blueberry
Cherry
Date
Elderberry
Fig
Grape
Kiwi
Lemon
Mango
Value:
Demo.tsx
'use client';
import { useState } from 'react';
import { Listbox, type ListboxKey } from '@salmonui/dropdowns/listbox';
interface Fruit {
value: string;
label: string;
disabled?: boolean;
}
// Objects with `value` and `label` need no accessors; `disabled` items are
// skipped by the arrow keys and type-ahead.
const fruits: Fruit[] = [
{ value: 'apple', label: 'Apple' },
{ value: 'apricot', label: 'Apricot' },
{ value: 'banana', label: 'Banana' },
{ value: 'blueberry', label: 'Blueberry' },
{ value: 'cherry', label: 'Cherry', disabled: true },
{ value: 'date', label: 'Date' },
{ value: 'elderberry', label: 'Elderberry' },
{ value: 'fig', label: 'Fig' },
{ value: 'grape', label: 'Grape' },
{ value: 'kiwi', label: 'Kiwi' },
{ value: 'lemon', label: 'Lemon' },
{ value: 'mango', label: 'Mango' },
];
export default function ListboxBasicDemo() {
// Single selection, controlled: selection follows focus on the arrow keys.
const [fruit, setFruit] = useState<ListboxKey | null>('banana');
// Multiple selection, controlled, with checkboxes, select all and a limit.
const [basket, setBasket] = useState<readonly ListboxKey[]>(['apple', 'kiwi']);
return (
<div style={{ display: 'flex', flexWrap: 'wrap', gap: 24, alignItems: 'start' }}>
<div style={{ minWidth: 200 }}>
<p id="listbox-basic-single">Favourite fruit</p>
<Listbox data={fruits} aria-labelledby="listbox-basic-single" value={fruit} onValueChange={(value) => setFruit(value)} maxVisibleItems={6} />
<p>
Value: <output>{String(fruit)}</output>
</p>
</div>
<div style={{ minWidth: 220 }}>
<p id="listbox-basic-multiple">Basket (at most 5)</p>
<Listbox
data={fruits}
multiple
indicator="checkbox"
showSelectAll
maxSelected={5}
aria-labelledby="listbox-basic-multiple"
value={basket}
onValueChange={(value) => setBasket(value)}
maxVisibleItems={6}
/>
<p>
Value: <output>{basket.join(', ') || 'none'}</output>
</p>
</div>
</div>
);
}
Both lists are controlled (value plus onValueChange). Try the arrow keys,
Space, Shift+arrows, Ctrl/Cmd+A and type-ahead in the right-hand list; the
Cherry option is disabled and skipped.