import { ListboxArrow, ListboxButton, ListboxInput, ListboxList, ListboxOption, ListboxPopover, } from '@reach/listbox'; import VisuallyHidden from '@reach/visually-hidden'; import { FunctionComponent } from 'preact'; import { Icon } from './Icon'; import { IconType } from '@standardnotes/snjs'; export type DropdownItem = { icon?: IconType; iconClassName?: string; label: string; value: string; disabled?: boolean; }; 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}
); export 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}
))}
); };