Demo
ListView as Listbox
Documented in List.
ListView as Listbox
A grouped, checkable contact list with item templates, tooltips and a disabled item, written with Listbox.
Ada Lovelace
Alan Turing
Edsger Dijkstra
Grace Hopper
Barbara Liskov
Checked: 2
Demo.tsx
'use client';
import { useState } from 'react';
import { Avatar } from '@salmonui/layout/avatar';
import { Listbox, type ListboxKey } from '@salmonui/dropdowns/listbox';
interface Contact {
id: number;
name: string;
team: string;
email: string;
away?: boolean;
}
// A Listbox with groups, an item template, tooltips and checkboxes:
// accessors read the item fields, `renderItem` renders each item.
const contacts: Contact[] = [
{ id: 1, name: 'Ada Lovelace', team: 'Engineering', email: '[email protected]' },
{ id: 2, name: 'Alan Turing', team: 'Engineering', email: '[email protected]' },
{ id: 3, name: 'Edsger Dijkstra', team: 'Engineering', email: '[email protected]', away: true },
{ id: 4, name: 'Grace Hopper', team: 'Operations', email: '[email protected]' },
{ id: 5, name: 'Barbara Liskov', team: 'Operations', email: '[email protected]' },
];
export default function ListViewDemo() {
const [checked, setChecked] = useState<readonly ListboxKey[]>([1, 4]);
return (
<div style={{ display: 'grid', gap: 12, maxInlineSize: 360 }}>
<Listbox
aria-label="Contacts"
data={contacts}
getItemValue={(c) => c.id}
getItemLabel={(c) => c.name}
getItemGroup={(c) => c.team}
isItemDisabled={(c) => c.away === true}
getItemProps={(c) => ({ title: c.email })}
renderItem={({ label }) => (
<span style={{ display: 'inline-flex', alignItems: 'center', gap: 8 }}>
<Avatar name={label} size="xs" shape="circle" />
{label}
</span>
)}
multiple
indicator="checkbox"
showSelectAll
value={checked}
onValueChange={setChecked}
/>
<p style={{ margin: 0 }}>Checked: {checked.length}</p>
</div>
);
}
Accessors replace fields, renderItem the item template and
getItemProps the tooltip; checked items are the controlled value.