import { ListboxArrow, ListboxButton, ListboxInput, ListboxList, ListboxOption, ListboxPopover } from '@reach/listbox' import VisuallyHidden from '@reach/visually-hidden' import { FunctionComponent } from 'react' import Icon from '@/Components/Icon/Icon' import { DropdownItem } from './DropdownItem' type DropdownProps = { id: string label: string items: DropdownItem[] value: string onChange: (value: string, item: DropdownItem) => void disabled?: boolean } type ListboxButtonProps = DropdownItem & { isExpanded: boolean } const CustomDropdownButton: FunctionComponent = ({ label, isExpanded, icon, iconClassName = '', }) => ( <>
{icon ? (
) : null}
{label}
) const Dropdown: FunctionComponent = ({ id, label, items, value, onChange, disabled }) => { const labelId = `${id}-label` const handleChange = (value: string) => { const selectedItem = items.find((item) => item.value === value) as DropdownItem onChange(value, selectedItem) } return ( <> {label} { const current = items.find((item) => item.value === value) const icon = current ? current?.icon : null const iconClassName = current ? current?.iconClassName : null return CustomDropdownButton({ value: value ? value : label.toLowerCase(), label, isExpanded, ...(icon ? { icon } : null), ...(iconClassName ? { iconClassName } : null), }) }} />
{items.map((item) => ( {item.icon ? (
) : null}
{item.label}
))}
) } export default Dropdown