import { Listbox, Transition } from '@headlessui/react'; import { Fragment } from 'react'; import { Flex } from '@/components'; import { Icon } from '@/components/core/icon'; type DropdownOptionProps = { option: DropdownItem; }; const DropdownOption: React.FC = ({ option, }: DropdownOptionProps) => ( `relative cursor-default select-none py-2 px-3.5 text-slate11 rounded-xl mb-2 text-sm max-w-full ${ active ? 'bg-slate5 text-slate12' : 'bg-transparent' }` } value={option} > {({ selected, active }) => ( {option.label} {selected && ( )} )} ); type DropdownButtonProps = { /** * The selected value of the dropdown. */ selectedValue: DropdownItem | undefined; /** * If it's true, the list of options will be displayed */ open: boolean; /** * Background color of the dropdown. Should be on tailwind palette. */ backgroundColor?: string; /** * Text color of the dropdown. Should be on tailwind palette. */ textColor?: string; }; const DropdownButton: React.FC = ({ selectedValue, backgroundColor, textColor, }: DropdownButtonProps) => { const textColorCss = textColor ? `text-${textColor}` : 'text-slate12'; const borderColor = backgroundColor ? `border-${backgroundColor}` : 'border-slate7'; const backgroundColorClass = backgroundColor ? `bg-${backgroundColor}` : 'bg-transparent'; return ( {selectedValue && selectedValue.label ? selectedValue.label : 'Select'} ); }; export type DropdownItem = { /** * The key of the item. */ value: string; /** * The label to display of the item. */ label: string; }; export type DropdownProps = { /** * List of items to be displayed in the dropdown. */ items: DropdownItem[]; /** * The selected value of the dropdown. */ selectedValue: DropdownItem | undefined; /** * Callback when the selected value changes. */ onChange(option: DropdownItem): void; /** * Background color of the dropdown. Should be on tailwind palette. https://tailwindcss.com/docs/background-color */ backgroundColor?: string; /** * Text color of the dropdown. Should be on tailwind palette. https://tailwindcss.com/docs/text-color */ textColor?: string; /** * Width of the options list. Should be on tailwind width. https://tailwindcss.com/docs/width */ optionsWidth?: string; }; export const Dropdown: React.FC = ({ items, selectedValue, onChange, backgroundColor, textColor, optionsWidth, }: DropdownProps) => { const handleDropdownChange = (option: DropdownItem): void => { onChange(option); }; const width = optionsWidth ? `w-${optionsWidth}` : 'w-full'; return ( {({ open }) => (
{items.map((option: DropdownItem) => ( ))}
)}
); };