import React, { FC } from 'react'; import { Listbox, ListboxButton, ListboxOption, ListboxOptions, Transition, } from '@headlessui/react'; import { AnchorPropsWithSelection } from '@headlessui/react/dist/internal/floating'; import type { Option } from '~/common'; import { cn } from '~/utils/'; interface DropdownProps { value?: string | Option; label?: string; onChange: (value: string | Option) => void; options: (string | Option)[]; className?: string; anchor?: AnchorPropsWithSelection; sizeClasses?: string; testId?: string; } /* * Mainly used for the Speech Voice Selection Dropdown */ const Dropdown: FC = ({ value, label = '', onChange, options, className = '', anchor, sizeClasses, testId = 'dropdown-menu', }) => { const getValue = (option?: string | Option) => typeof option === 'string' ? option : option?.value; const getDisplay = (option?: string | Option) => typeof option === 'string' ? option : option?.label ?? option?.value; const isEqual = (a: string | Option, b: string | Option): boolean => getValue(a) === getValue(b); const selectedOption = options.find((option) => isEqual(option, value ?? '')) ?? value; const handleChange = (newValue: string | Option) => { onChange(newValue); }; return (
{label} {getDisplay(selectedOption)} {options.map((item, index) => ( {({ selected }) => (
{getDisplay(item)} {selected && ( )}
)}
))}
); }; export default Dropdown;