import React, { FC } from 'react'; import { Listbox, ListboxButton, ListboxOption, ListboxOptions, Transition, } from '@headlessui/react'; import { AnchorPropsWithSelection } from '@headlessui/react/dist/internal/floating'; import { cn } from '~/utils/'; type OptionType = { value: string; display?: string; }; interface DropdownProps { value: string | OptionType; label?: string; onChange: (value: string) => void | ((value: OptionType) => void); options: (string | OptionType)[]; className?: string; anchor?: AnchorPropsWithSelection; sizeClasses?: string; testId?: string; } const Dropdown: FC = ({ value, label = '', onChange, options, className = '', anchor, sizeClasses, testId = 'dropdown-menu', }) => { const getValue = (option: string | OptionType): string => typeof option === 'string' ? option : option.value; const getDisplay = (option: string | OptionType): string => typeof option === 'string' ? option : (option.display ?? '') || option.value; const selectedOption = options.find((option) => getValue(option) === getValue(value)); const displayValue = selectedOption != null ? getDisplay(selectedOption) : getDisplay(value); return (
{label} {displayValue} {options.map((item, index) => (
{getDisplay(item)}
))}
); }; export default Dropdown;