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 && (
)}
{loading ? (
) : 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;