'use client' import { useState, useRef } from 'react' import { X, ChevronDown, Check } from 'lucide-react' import { FieldWrapper } from './shared' import useClickOutside from '../../hooks/useClickOutside' type Option = { label: string value: string } export type MultiSelectProps = { label?: string name: string options: Option[] value?: string[] onChange?: (value: string[]) => void placeholder?: string error?: string className?: string disabled?: boolean required?: boolean info?: string description?: string textSize?: 'sm' | 'md' } export default function MultiSelect({ label, name, options, value = [], onChange, placeholder = 'Select…', error, className, disabled, required, info, description, textSize = 'sm', }: MultiSelectProps) { const [open, setOpen] = useState(false) const containerRef = useRef(null) useClickOutside(containerRef, () => setOpen(false)) function toggleOption(optionValue: string) { if (!onChange) return if (value.includes(optionValue)) { onChange(value.filter((v) => v !== optionValue)) } else { onChange([...value, optionValue]) } } function removeOption(optionValue: string) { if (!onChange) return onChange(value.filter((v) => v !== optionValue)) } return (
!disabled && setOpen((o) => !o)} className={` flex min-h-10 cursor-pointer items-center justify-between gap-2 rounded-md bg-login-500/50 border border-login-500 px-3 py-1.5 transition-all duration-150 select-none ${open ? 'border-login/60 ring-1 ring-login/30' : ''} ${error ? 'border-red-500/70' : ''} ${disabled ? 'opacity-40 cursor-not-allowed' : ''} `} >
{value.length === 0 && ( {placeholder} )} {value.map((val) => { const option = options.find((o) => o.value === val) if (!option) return null return ( {option.label} {!disabled && ( )} ) })}
{open && options.length > 0 && (
{options.map((option) => { const selected = value.includes(option.value) return (
toggleOption(option.value)} className={` flex cursor-pointer items-center justify-between px-3 py-2 text-sm hover:bg-login-700 transition-colors select-none ${selected ? 'text-login-50' : 'text-login-100'} `} > {option.label} {selected && }
) })}
)}
) }