import React, { useState, useRef, useEffect } from 'react'; interface DropdownProps { options: { value: string; label: string }[]; value: string; onChange: (e: { target: { name: string; value: string } }) => void; name?: string; } const Dropdown: React.FC = ({ options, value, onChange, name, ...rest }) => { const [isOpen, setIsOpen] = useState(false); const dropdownRef = useRef(null); const selectedOptionLabel = options.find(option => option.value === value)?.label || ''; useEffect(() => { const handleClickOutside = (event: MouseEvent) => { if (dropdownRef.current && !dropdownRef.current.contains(event.target as Node)) { setIsOpen(false); } }; document.addEventListener('mousedown', handleClickOutside); return () => { document.removeEventListener('mousedown', handleClickOutside); }; }, []); const handleOptionClick = (optionValue: string) => { const syntheticEvent = { target: { name: name || '', value: optionValue, }, }; onChange(syntheticEvent); setIsOpen(false); }; return (
{isOpen && (
    {options.map((option) => (
  • handleOptionClick(option.value)} className={`h-10 px-3 text-white cursor-pointer transition-all duration-150 ease-in-out flex items-center ${option.value === value ? 'bg-cyan-500/20 text-cyan-300' : 'hover:bg-white/20 hover:text-white hover:shadow-lg' }`} role="option" aria-selected={option.value === value} > {option.label}
  • ))}
)} {/* Hidden input to mimic native select behavior for forms */} {name && }
); }; export default Dropdown;