import { useState, useEffect, useRef } from 'react'; import { IconChevronDown } from './icons'; import styles from './Select.module.scss'; export interface SelectOption { value: string; label: string; } interface SelectProps { value: string; options: ReadonlyArray; onChange: (value: string) => void; placeholder?: string; className?: string; disabled?: boolean; ariaLabel?: string; fullWidth?: boolean; } export function Select({ value, options, onChange, placeholder, className, disabled = false, ariaLabel, fullWidth = true }: SelectProps) { const [open, setOpen] = useState(false); const wrapRef = useRef(null); useEffect(() => { if (!open || disabled) return; const handleClickOutside = (event: MouseEvent) => { if (!wrapRef.current?.contains(event.target as Node)) setOpen(false); }; document.addEventListener('mousedown', handleClickOutside); return () => document.removeEventListener('mousedown', handleClickOutside); }, [disabled, open]); const isOpen = open && !disabled; const selected = options.find((o) => o.value === value); const displayText = selected?.label ?? placeholder ?? ''; const isPlaceholder = !selected && placeholder; return (
{isOpen && (
{options.map((opt) => { const active = opt.value === value; return ( ); })}
)}
); }