import React from 'react'; import Arrow2 from '../assets/dropdown-arrow.svg'; import Edit from '../assets/edit.svg'; import Trash from '../assets/trash.svg'; import { DropdownOption, DropdownProps } from './types/Dropdown.types'; function Dropdown({ options, selectedValue, onSelect, size = 'w-32', rounded = 'xl', buttonClassName = 'border-silver bg-white dark:bg-transparent dark:border-dim-gray', optionsClassName = 'border-silver bg-white dark:border-dim-gray dark:bg-dark-charcoal', border = 'border-2', showEdit, onEdit, showDelete, onDelete, placeholder, placeholderClassName = 'text-gray-500 dark:text-gray-400', contentSize = 'text-base', }: DropdownProps) { const dropdownRef = React.useRef(null); const [isOpen, setIsOpen] = React.useState(false); const borderRadius = rounded === 'xl' ? 'rounded-xl' : 'rounded-3xl'; const borderTopRadius = rounded === 'xl' ? 'rounded-t-xl' : 'rounded-t-3xl'; const handleClickOutside = (event: MouseEvent) => { if ( dropdownRef.current && !dropdownRef.current.contains(event.target as Node) ) { setIsOpen(false); } }; React.useEffect(() => { document.addEventListener('mousedown', handleClickOutside); return () => { document.removeEventListener('mousedown', handleClickOutside); }; }, []); return (
{isOpen && (
{options.map((option: any, index) => (
{ onSelect(option); setIsOpen(false); }} className={`dark:text-light-gray ml-5 flex-1 overflow-hidden py-3 text-ellipsis whitespace-nowrap ${contentSize}`} > {typeof option === 'string' ? option : option.name ? option.name : option.label ? option.label : `${ option.value < 1e9 ? option.value + ` (${option.description})` : option.description }`} {showEdit && onEdit && option.type !== 'public' && ( Edit { onEdit({ id: option.id, name: option.name, type: option.type, }); setIsOpen(false); }} /> )} {showDelete && onDelete && ( )}
))}
)}
); } export default Dropdown;