"use client"; import React, { useState, useRef, useEffect } from "react"; import { Search, ChevronDown, Check } from "lucide-react"; interface Option { id: string; name: string; } interface ComboboxProps { options: Option[]; value: string; onChange: (value: string) => void; placeholder?: string; id?: string; } export default function Combobox({ options, value, onChange, placeholder = "Search...", id }: ComboboxProps) { const [isOpen, setIsOpen] = useState(false); const [search, setSearch] = useState(""); const containerRef = useRef(null); const filteredOptions = options.filter(option => option.name.toLowerCase().includes(search.toLowerCase()) ); const selectedOption = options.find(o => o.id === value); useEffect(() => { const handleClickOutside = (e: MouseEvent) => { if (containerRef.current && !containerRef.current.contains(e.target as Node)) { setIsOpen(false); } }; document.addEventListener("mousedown", handleClickOutside); return () => document.removeEventListener("mousedown", handleClickOutside); }, []); return (
{isOpen && (
{filteredOptions.length === 0 ? (
No results found
) : ( filteredOptions.map(option => (
{ onChange(option.id); setIsOpen(false); setSearch(""); }} onKeyDown={(e) => { if (e.key === 'Enter' || e.key === ' ') { onChange(option.id); setIsOpen(false); setSearch(""); } }} className={`flex items-center justify-between px-3 py-2 rounded-md cursor-pointer transition-colors outline-none focus:bg-blue-600 focus:text-white ${value === option.id ? "bg-blue-600 text-white" : "hover:bg-white/5 text-gray-300" }`} > {option.name} {value === option.id &&
)) )}
)}
); }