"use client"; import { useEffect, useRef, useState } from "react"; import { COUNTRIES } from "@/lib/countries"; interface CountrySelectProps { value: string; onChange: (code: string) => void; className?: string; } export function CountrySelect({ value, onChange, className }: CountrySelectProps) { const [open, setOpen] = useState(false); const [search, setSearch] = useState(""); const ref = useRef(null); const inputRef = useRef(null); const selected = COUNTRIES.find((c) => c.code === value); const filtered = search ? COUNTRIES.filter((c) => c.label.toLowerCase().includes(search.toLowerCase()), ) : COUNTRIES; // Close on outside click useEffect(() => { function handle(e: MouseEvent) { if (ref.current && !ref.current.contains(e.target as Node)) { setOpen(false); setSearch(""); } } document.addEventListener("mousedown", handle); return () => document.removeEventListener("mousedown", handle); }, []); // Focus search input when opened useEffect(() => { if (open) inputRef.current?.focus(); }, [open]); return (
{/* Trigger button */} {/* Dropdown */} {open && (
{/* Search */}
setSearch(e.target.value)} className="w-full rounded-lg bg-transparent px-3 py-2 text-sm outline-none placeholder:text-lightsec dark:placeholder:text-darksec" />
{/* Options */}
    {filtered.length === 0 && (
  • No results
  • )} {filtered.map((c) => (
  • ))}
)}
); }