Combo box with string options
A combo box whose options are an array of strings.
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 }) {
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: mapOption,
});
const [managedFocus, setManagedFocus] = useState(true);
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}
managedFocus={managedFocus}
/>
<label htmlFor="output">Current value</label>
<output
htmlFor="select"
id="output"
>
{JSON.stringify(value, undefined, ' ')}
</output>
<label>
<input
type="checkbox"
onChange={({ target: { checked } }) => setManagedFocus(checked)}
checked={managedFocus}
/>{' '}
Toggle managed focus
</label>
</>
);
}