import { useState, useRef, useEffect, useCallback } from 'react' import { cn } from '@/lib/utils' import { Check, ChevronDown, X } from 'lucide-react' export interface MultiSelectOption { value: string label: string description?: string } interface MultiSelectProps { value: string[] onChange: (values: string[]) => void options: MultiSelectOption[] placeholder?: string disabled?: boolean className?: string } export function MultiSelect({ value, onChange, options, placeholder = 'Select...', disabled = false, className, }: MultiSelectProps) { const [isOpen, setIsOpen] = useState(false) const [search, setSearch] = useState('') const [highlightedIndex, setHighlightedIndex] = useState(0) const containerRef = useRef(null) const inputRef = useRef(null) const triggerRef = useRef(null) const filteredOptions = options.filter(option => option.label.toLowerCase().includes(search.toLowerCase()) || option.value.toLowerCase().includes(search.toLowerCase()) || (option.description?.toLowerCase().includes(search.toLowerCase())) ) useEffect(() => { if (!isOpen) return const handleClickOutside = (e: MouseEvent) => { if (containerRef.current && !containerRef.current.contains(e.target as Node)) { setIsOpen(false) setSearch('') } } document.addEventListener('mousedown', handleClickOutside) return () => document.removeEventListener('mousedown', handleClickOutside) }, [isOpen]) useEffect(() => { setHighlightedIndex(0) }, [search]) const handleToggle = useCallback((optionValue: string) => { if (value.includes(optionValue)) { onChange(value.filter(v => v !== optionValue)) } else { onChange([...value, optionValue]) } }, [value, onChange]) const handleRemove = useCallback((optionValue: string, e: React.MouseEvent) => { e.stopPropagation() onChange(value.filter(v => v !== optionValue)) }, [value, onChange]) const handleOpen = () => { if (disabled) return setIsOpen(true) setTimeout(() => inputRef.current?.focus(), 0) } const handleTriggerKeyDown = useCallback((e: React.KeyboardEvent) => { if (e.key === 'Enter' || e.key === ' ' || e.key === 'ArrowDown') { e.preventDefault() if (!isOpen) { setIsOpen(true) setTimeout(() => inputRef.current?.focus(), 0) } } else if (e.key === 'Escape') { setIsOpen(false) setSearch('') } }, [isOpen]) const handleInputKeyDown = useCallback((e: React.KeyboardEvent) => { if (e.key === 'Escape') { e.preventDefault() setIsOpen(false) setSearch('') triggerRef.current?.focus() } else if (e.key === 'ArrowDown') { e.preventDefault() setHighlightedIndex(prev => Math.min(prev + 1, filteredOptions.length - 1)) } else if (e.key === 'ArrowUp') { e.preventDefault() setHighlightedIndex(prev => Math.max(prev - 1, 0)) } else if (e.key === 'Enter') { e.preventDefault() if (filteredOptions[highlightedIndex]) { handleToggle(filteredOptions[highlightedIndex].value) } } else if (e.key === 'Tab') { setIsOpen(false) setSearch('') } }, [highlightedIndex, filteredOptions, handleToggle]) return (
{value.length === 0 && ( {placeholder} )} {value.map(v => { const option = options.find(o => o.value === v) return ( {option?.label ?? v} ) })}
{isOpen && (
setSearch(e.target.value)} onKeyDown={handleInputKeyDown} placeholder="Search skills..." className={cn( 'flex h-8 w-full rounded-md border border-input bg-transparent px-3 py-1 text-sm shadow-sm transition-colors', 'placeholder:text-muted-foreground', 'focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring' )} />
{filteredOptions.length === 0 ? (
No skills found
) : ( filteredOptions.map((option, index) => { const isSelected = value.includes(option.value) const isHighlighted = index === highlightedIndex return ( ) }) )}
)}
) }