Drop down table with a header row
A drop down whose options are laid out in a table with a header row.
import { useRef, useState } from 'react';
import { DropDownTable, layoutMaxWidth, layoutMaxHeight } 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 }) {
return { label: breed };
}
const onLayoutListBox = [layoutMaxWidth, layoutMaxHeight];
export function Example() {
const [value, setValue] = useState(null);
const ref = useRef();
return (
<>
<div
className="label"
onClick={() => ref.current.focus()}
id="drop-down-label"
>
Drop down
</div>
<DropDownTable
id="drop-down"
aria-labelledby="drop-down-label"
ref={ref}
value={value}
onValue={setValue}
onLayoutListBox={onLayoutListBox}
options={cats}
columns={columns}
mapOption={mapOption}
/>
<label htmlFor="output">Current value</label>
<output
htmlFor="drop-down"
id="output"
>
{JSON.stringify(value, undefined, ' ')}
</output>
</>
);
}