import { useState, useRef, useEffect } from 'react' import { ChevronDownIcon, CheckIcon } from '@heroicons/react/24/outline' interface MultiSelectProps { label: string options: { value: string; label: string; count?: number }[] selected: string[] onChange: (selected: string[]) => void placeholder?: string className?: string } export default function MultiSelect({ label, options, selected, onChange, placeholder = 'Select...', className = '' }: MultiSelectProps) { const [isOpen, setIsOpen] = useState(false) const dropdownRef = useRef(null) // Close dropdown when clicking outside 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 handleToggle = (value: string) => { if (selected.includes(value)) { onChange(selected.filter(v => v !== value)) } else { onChange([...selected, value]) } } const handleSelectAll = () => { onChange(options.map(opt => opt.value)) } const handleDeselectAll = () => { onChange([]) } const getDisplayText = () => { if (selected.length === 0) return placeholder if (selected.length === options.length) return 'All Selected' if (selected.length === 1) { const option = options.find(opt => opt.value === selected[0]) return option?.label || selected[0] } return `${selected.length} selected` } return (
{/* Dropdown Button */} {/* Dropdown Menu */} {isOpen && (
{/* Select All / Deselect All */}
{/* Options */}
{options.map((option) => { const isSelected = selected.includes(option.value) return ( ) })}
)}
) }