import { ListboxArrow, ListboxButton, ListboxInput, ListboxList, ListboxOption, ListboxPopover, } from '@reach/listbox'; import VisuallyHidden from '@reach/visually-hidden'; import { FunctionComponent } from 'preact'; import { IconType, Icon } from './Icon'; import { useState } from 'preact/hooks'; export type DropdownItem = { icon?: IconType; label: string; value: string; }; type DropdownProps = { id: string; srLabel: string; items: DropdownItem[]; defaultValue: string; onChange: (value: string) => void; }; type ListboxButtonProps = { icon?: IconType; value: string | null; label: string; isExpanded: boolean; }; const customDropdownButton: FunctionComponent = ({ label, isExpanded, icon, }) => ( <>
{icon ? (
) : null}
{label}
); export const Dropdown: FunctionComponent = ({ id, srLabel, items, defaultValue, onChange, }) => { const [value, setValue] = useState(defaultValue); const labelId = `${id}-label`; const handleChange = (value: string) => { setValue(value); onChange(value); }; return ( <> {srLabel} { const current = items.find((item) => item.value === value); const icon = current ? current?.icon : null; return customDropdownButton({ value, label, isExpanded, ...(icon ? { icon } : null), }); }} />
{items.map((item) => ( {item.icon ? (
) : null}
{item.label}
))}
); };