import { useState, useRef, useEffect } from "react"; const Dropdown = ({ label, selectedValue, onSelect, options = [], placeholder = "Select an option...", disabled = false, loading = false, icon = null, className = "", dropdownClassName = "", optionClassName = "", showSearch = false, searchPlaceholder = "Search...", emptyMessage = "No options available", loadingMessage = "Loading...", }) => { const [isOpen, setIsOpen] = useState(false); const [searchTerm, setSearchTerm] = useState(""); const dropdownRef = useRef(null); useEffect(() => { const handleClickOutside = (event) => { if (dropdownRef.current && !dropdownRef.current.contains(event.target)) { setIsOpen(false); } }; document.addEventListener("mousedown", handleClickOutside); return () => document.removeEventListener("mousedown", handleClickOutside); }, []); const filteredOptions = options.filter((option) => option.label ? option.label.toLowerCase().includes(searchTerm.toLowerCase()) : option.toLowerCase().includes(searchTerm.toLowerCase()) ); const displayValue = selectedValue?.label || selectedValue || placeholder; const isSelected = selectedValue && selectedValue !== placeholder; return (
{label && ( )} {isOpen && !disabled && (
{showSearch && (
setSearchTerm(e.target.value)} className="w-full pl-9 pr-3 py-2 border border-secondary-200 rounded-lg focus:outline-none focus:ring-2 focus:ring-primary-500 focus:border-transparent hover:border-primary-300 transition-colors text-sm" onClick={(e) => e.stopPropagation()} />
)}
{loading ? (
{loadingMessage}
) : filteredOptions.length === 0 ? (
{emptyMessage}
) : ( filteredOptions.map((option) => { const optionValue = option.value || option; const optionLabel = option.label || option; const optionIcon = option.icon; const optionDescription = option.description; const isOptionSelected = selectedValue === optionValue || (selectedValue?.value && selectedValue.value === optionValue); return ( ); }) )}
)}
); }; export default Dropdown;