"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; } export default function Combobox({ options, value, onChange, placeholder = "Search..." }: 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 (
setIsOpen(!isOpen)} className="flex items-center justify-between w-full bg-black/40 border border-white/10 rounded-lg px-4 py-2.5 cursor-pointer hover:border-white/20 transition-all focus-within:ring-2 focus-within:ring-blue-500/50" > {selectedOption ? selectedOption.name : placeholder}
{isOpen && (
setSearch(e.target.value)} />
{filteredOptions.length === 0 ? (
No results found
) : ( filteredOptions.map(option => (
{ onChange(option.id); setIsOpen(false); setSearch(""); }} className={`flex items-center justify-between px-3 py-2 rounded-md cursor-pointer transition-colors ${value === option.id ? "bg-blue-600 text-white" : "hover:bg-white/5 text-gray-300" }`} > {option.name} {value === option.id && }
)) )}
)}
); }