import React from 'react'; import Arrow2 from '../assets/dropdown-arrow.svg'; import Edit from '../assets/edit.svg'; import Trash from '../assets/trash.svg'; function Dropdown({ options, selectedValue, onSelect, size = 'w-32', rounded = 'xl', border = 'border-2', borderColor = 'silver', showEdit, onEdit, showDelete, onDelete, placeholder, contentSize = 'text-base', }: { options: | string[] | { name: string; id: string; type: string }[] | { label: string; value: string }[] | { value: number; description: string }[]; selectedValue: | string | { label: string; value: string } | { value: number; description: string } | { name: string; id: string; type: string } | null; onSelect: | ((value: string) => void) | ((value: { name: string; id: string; type: string }) => void) | ((value: { label: string; value: string }) => void) | ((value: { value: number; description: string }) => void); size?: string; rounded?: 'xl' | '3xl'; border?: 'border' | 'border-2'; borderColor?: string; showEdit?: boolean; onEdit?: (value: { name: string; id: string; type: string }) => void; showDelete?: boolean; onDelete?: (value: string) => void; placeholder?: string; contentSize?: string; }) { const dropdownRef = React.useRef(null); const [isOpen, setIsOpen] = React.useState(false); const [dropdownPosition, setDropdownPosition] = React.useState({ top: 0}); 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); }; }, []); const handleToggleDropdown = () => { setIsOpen(prev => !prev); if (!isOpen) { adjustDropdownPosition(); } }; const adjustDropdownPosition = () => { if (dropdownRef.current) { const rect = dropdownRef.current.getBoundingClientRect(); const dropdownMenuHeight = Math.min(200, options.length * 40); // Adjust height based on options const viewportHeight = window.innerHeight; // Check if dropdown overflows the bottom of the viewport const newPosition = { top: rect.bottom + dropdownMenuHeight > viewportHeight ? -dropdownMenuHeight : 0, left: 0, }; setDropdownPosition(newPosition); } }; return (
{isOpen && (
{options.map((option: any, index) => (
{ onSelect(option); setIsOpen(false); }} className={`ml-5 flex-1 overflow-hidden overflow-ellipsis whitespace-nowrap py-3 dark:text-light-gray ${contentSize}`} > {typeof option === 'string' ? option : option.name || option.label || option.description} {showEdit && onEdit && ( Edit { onEdit(option); setIsOpen(false); }} /> )} {showDelete && onDelete && ( )}
))}
)}
); } export default Dropdown;