Combo box table with headers and grouped
A combo box with both headers and group labels.
import { useState } from 'react';
import {
ComboBoxTable,
useTokenSearch,
TokenHighlight,
layoutMaxWidth,
layoutMaxHeight,
layoutColumnsAlignLeft,
} from '@citizensadvice/react-combo-boxes';
import cats from '../../data/cats.json';
const columns = [
{ name: 'breed', label: 'Breed' },
{ name: 'country', label: 'Country' },
{ name: 'origin', label: 'Origin' },
{ name: 'bodyType', label: 'Body type' },
{ name: 'coatLength', label: 'Coat length' },
{ name: 'pattern', label: 'Pattern' },
];
function mapOption({ breed, bodyType }) {
return { label: breed, group: bodyType };
}
function index({ breed }) {
return breed;
}
function renderValue({ children }, { search, column: { name } }) {
if (name === 'breed') {
return (
<TokenHighlight
search={search}
value={children}
/>
);
}
return children;
}
const onLayoutListBox = [
layoutMaxWidth,
layoutMaxHeight,
layoutColumnsAlignLeft,
];
export function Example() {
const [value, setValue] = useState(null);
const [search, setSearch] = useState(null);
const filteredOptions = useTokenSearch(search, { options: cats, index });
return (
<>
<label
id="select-label"
htmlFor="select"
>
Select
</label>
<ComboBoxTable
id="select"
aria-labelledby="select-label"
value={value}
onValue={setValue}
onSearch={setSearch}
options={filteredOptions}
columns={columns}
renderColumnValue={renderValue}
mapOption={mapOption}
onLayoutListBox={onLayoutListBox}
/>
<label htmlFor="output">Current value</label>
<output
htmlFor="select"
id="output"
>
{JSON.stringify(value, undefined, ' ')}
</output>
</>
);
}