Demo
Listbox search and grouping
Documented in Listbox search, grouping and remote data.
Listbox search and grouping
10,000 grouped items in a virtual list with a sticky group header, an accent-insensitive contains search with highlighted matches and a search-change callback.
Places (10,000, grouped by region)
Ångström 0
Café 1
Zürich 2
Øresund 3
Crème 4
Naïve 5
Señor 6
Straße 7
Łódź 8
São Paulo 9
Kraków 10
Málaga 11
Ångström 12
Café 13
Zürich 14
Øresund 15
Crème 16
Naïve 17
Señor 18
Search: · Selected:
Demo.tsx
'use client';
import { useState } from 'react';
import { Listbox, type ListboxKey } from '@salmonui/dropdowns/listbox';
interface Place {
value: number;
label: string;
group: string;
}
const REGIONS = ['Africa', 'Americas', 'Asia', 'Europe', 'Oceania'];
const NAMES = ['Ångström', 'Café', 'Zürich', 'Øresund', 'Crème', 'Naïve', 'Señor', 'Straße', 'Łódź', 'São Paulo', 'Kraków', 'Málaga'];
// 10,000 items in five contiguous groups (items of one group must be adjacent).
const places: Place[] = Array.from({ length: 10_000 }, (_, i) => ({
value: i,
label: `${NAMES[i % NAMES.length]} ${i}`,
group: REGIONS[Math.floor(i / 2_000)] as string,
}));
export default function ListboxSearchDemo() {
const [value, setValue] = useState<readonly ListboxKey[]>([]);
const [text, setText] = useState('');
return (
<div style={{ maxWidth: 360 }}>
<p id="listbox-search-label">Places (10,000, grouped by region)</p>
<Listbox
data={places}
multiple
searchable
searchOperator="contains"
ignoreAccents
highlightMatches
searchPlaceholder="Search places"
onSearchChange={(event) => setText(event.text)}
aria-labelledby="listbox-search-label"
value={value}
onValueChange={(next) => setValue(next)}
maxVisibleItems={8}
/>
<p>
Search: <output>{text === '' ? '(empty)' : text}</output> · Selected: <output>{value.length}</output>
</p>
</div>
);
}
Type cafe to match Café (ignoreAccents). The list is virtual (more
than 100 items), so only the options in view are in the DOM; the group
header at the top follows the scroll position.