import React, { FC, useState } 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; label?: string; onChange: (value: string) => void; options: string[] | Option[]; className?: string; anchor?: AnchorPropsWithSelection; sizeClasses?: string; testId?: string; } const Dropdown: FC = ({ value: initialValue, label = '', onChange, options, className = '', anchor, sizeClasses, testId = 'dropdown-menu', }) => { const [selectedValue, setSelectedValue] = useState(initialValue); return (
{ setSelectedValue(newValue); onChange(newValue); }} >
{label} {options .map((o) => (typeof o === 'string' ? { value: o, label: o } : o)) .find((o) => o.value === selectedValue)?.label ?? selectedValue} {options.map((item, index) => (
{typeof item === 'string' ? item : (item as Option).label} {selectedValue === (typeof item === 'string' ? item : item.value) && ( )}
))}
); }; export default Dropdown;