import React from 'react'; import { Listbox, Transition } from '@headlessui/react'; import type { Option } from '~/common'; import CheckMark from '../svg/CheckMark'; import { useLocalize } from '~/hooks'; import { cn } from '~/utils/'; type SelectDropDownProps = { id?: string; title?: string; value: string | null | Option; disabled?: boolean; setValue: (value: string) => void; availableValues: string[] | Option[]; emptyTitle?: boolean; showAbove?: boolean; showLabel?: boolean; iconSide?: 'left' | 'right'; renderOption?: () => React.ReactNode; containerClassName?: string; currentValueClass?: string; optionsListClass?: string; optionsClass?: string; subContainerClassName?: string; className?: string; }; function SelectDropDown({ title: _title, value, disabled, setValue, availableValues, showAbove = false, showLabel = true, emptyTitle = false, iconSide = 'right', containerClassName, optionsListClass, optionsClass, currentValueClass, subContainerClassName, className, renderOption, }: SelectDropDownProps) { const localize = useLocalize(); const transitionProps = { className: 'top-full mt-3' }; if (showAbove) { transitionProps.className = 'bottom-full mb-3'; } let title = _title; if (emptyTitle) { title = ''; } else if (!title) { title = localize('com_ui_model'); } return (
{({ open }) => ( <> {' '} {showLabel && ( {title} )} {!showLabel && !emptyTitle && ( {title}: )} {typeof value !== 'string' && value ? value?.label ?? '' : value ?? ''} {renderOption && ( {renderOption()} )} {availableValues.map((option: string | Option, i: number) => { if (!option) { return null; } const currentLabel = typeof option === 'string' ? option : option?.label ?? ''; const currentValue = typeof option === 'string' ? option : option?.value ?? ''; let activeValue: string | number | null | Option = value; if (typeof activeValue !== 'string') { activeValue = activeValue?.value ?? ''; } return ( {currentLabel} {currentValue === activeValue && ( )} ); })} )}
); } export default SelectDropDown;