'use client'; import { useState, useRef, useEffect } from 'react'; import { Globe, Search, ChevronDown, Check } from 'lucide-react'; export interface Country { code: string; name: string; } export const COUNTRIES: Country[] = [ { code: 'US', name: 'United States' }, { code: 'GB', name: 'United Kingdom' }, { code: 'DE', name: 'Germany' }, { code: 'NL', name: 'Netherlands' }, { code: 'FR', name: 'France' }, { code: 'IT', name: 'Italy' }, { code: 'ES', name: 'Spain' }, { code: 'AU', name: 'Australia' }, { code: 'CA', name: 'Canada' }, { code: 'BR', name: 'Brazil' }, { code: 'JP', name: 'Japan' }, { code: 'BE', name: 'Belgium' }, { code: 'AT', name: 'Austria' }, { code: 'CH', name: 'Switzerland' }, { code: 'SE', name: 'Sweden' }, { code: 'NO', name: 'Norway' }, { code: 'DK', name: 'Denmark' }, { code: 'FI', name: 'Finland' }, { code: 'PL', name: 'Poland' }, { code: 'PT', name: 'Portugal' }, { code: 'CZ', name: 'Czech Republic' }, { code: 'HU', name: 'Hungary' }, { code: 'RU', name: 'Russia' }, { code: 'MX', name: 'Mexico' }, { code: 'AR', name: 'Argentina' }, { code: 'CL', name: 'Chile' }, { code: 'NZ', name: 'New Zealand' }, { code: 'ZA', name: 'South Africa' }, { code: 'IN', name: 'India' }, { code: 'KR', name: 'South Korea' }, { code: 'SG', name: 'Singapore' }, { code: 'MY', name: 'Malaysia' }, { code: 'TH', name: 'Thailand' }, { code: 'AE', name: 'United Arab Emirates' }, { code: 'SA', name: 'Saudi Arabia' }, { code: 'IE', name: 'Ireland' }, { code: 'GR', name: 'Greece' }, { code: 'TR', name: 'Turkey' }, { code: 'RO', name: 'Romania' }, { code: 'UA', name: 'Ukraine' }, ]; function getCountryFlag(countryCode: string): string { const code = countryCode.toUpperCase(); if (code.length === 2) { const codePoints = [...code].map(char => 127397 + char.charCodeAt(0)); return String.fromCodePoint(...codePoints); } return '🏁'; } interface CountrySelectProps { value: string; onChange: (value: string) => void; error?: boolean; errorMessage?: string; disabled?: boolean; placeholder?: string; } export default function CountrySelect({ value, onChange, error, errorMessage, disabled, placeholder = 'Select country', }: CountrySelectProps) { const [isOpen, setIsOpen] = useState(false); const [search, setSearch] = useState(''); const containerRef = useRef(null); const inputRef = useRef(null); const selectedCountry = COUNTRIES.find(c => c.code === value); const filteredCountries = COUNTRIES.filter(country => country.name.toLowerCase().includes(search.toLowerCase()) || country.code.toLowerCase().includes(search.toLowerCase()) ); useEffect(() => { function handleClickOutside(event: MouseEvent) { if (containerRef.current && !containerRef.current.contains(event.target as Node)) { setIsOpen(false); setSearch(''); } } document.addEventListener('mousedown', handleClickOutside); return () => document.removeEventListener('mousedown', handleClickOutside); }, []); useEffect(() => { if (isOpen && inputRef.current) { inputRef.current.focus(); } }, [isOpen]); const handleSelect = (code: string) => { onChange(code); setIsOpen(false); setSearch(''); }; return (
{/* Trigger Button */} {/* Dropdown */} {isOpen && (
{/* Search Input */}
setSearch(e.target.value)} placeholder="Search countries..." className="w-full rounded-md border-0 px-4 py-2 pl-9 bg-deep-graphite text-white text-sm placeholder:text-gray-500 focus:outline-none focus:ring-1 focus:ring-primary-blue" />
{/* Country List */}
{filteredCountries.length > 0 ? ( filteredCountries.map((country) => ( )) ) : (
No countries found
)}
)} {/* Error Message */} {error && errorMessage && (

{errorMessage}

)}
); }