import React, { useState, useRef, useEffect, useMemo } from 'react'; import { Search, ChevronDown, Check, X } from 'lucide-react'; import { SORTED_COUNTRY_CODES, getCountryRuleByName, type CountryCode } from '../../constants/countries'; export interface CountrySelectProps { value: string; onChange: (country: string) => void; theme?: 'dark' | 'light'; size?: 'sm' | 'md'; showCurrency?: boolean; showDialCode?: boolean; disabled?: boolean; className?: string; placeholder?: string; } export const CountrySelect: React.FC = ({ value, onChange, theme = 'light', size = 'md', showCurrency = false, showDialCode = true, disabled = false, className = '', placeholder = 'Select country', }) => { const [isOpen, setIsOpen] = useState(false); const [searchQuery, setSearchQuery] = useState(''); const containerRef = useRef(null); const searchInputRef = useRef(null); const selectedRule = useMemo(() => { return getCountryRuleByName(value); }, [value]); // Focus search input when dropdown opens useEffect(() => { if (isOpen) { setTimeout(() => { searchInputRef.current?.focus(); }, 50); } else { setSearchQuery(''); } }, [isOpen]); // Handle click outside to close dropdown useEffect(() => { const handleClickOutside = (event: MouseEvent) => { if (containerRef.current && !containerRef.current.contains(event.target as Node)) { setIsOpen(false); } }; const handleKeyDown = (event: KeyboardEvent) => { if (event.key === 'Escape') { setIsOpen(false); } }; if (isOpen) { document.addEventListener('mousedown', handleClickOutside); document.addEventListener('keydown', handleKeyDown); } return () => { document.removeEventListener('mousedown', handleClickOutside); document.removeEventListener('keydown', handleKeyDown); }; }, [isOpen]); // Filter countries alphabetically by country name, dial code, or currency const filteredCountries = useMemo(() => { if (!searchQuery.trim()) { return SORTED_COUNTRY_CODES; } const q = searchQuery.toLowerCase().trim(); return SORTED_COUNTRY_CODES.filter((c: CountryCode) => { const matchName = c.country.toLowerCase().includes(q); const matchCode = c.code.toLowerCase().includes(q); const matchCurrency = c.currency ? c.currency.toLowerCase().includes(q) : false; return matchName || matchCode || matchCurrency; }); }, [searchQuery]); const getCurrencySymbol = (curr: string) => { if (curr === 'INR') return '₹'; if (curr === 'EUR') return '€'; return '$'; }; const getFlag = (cc: CountryCode) => { return cc.label ? cc.label.split(' ')[0] : '🌐'; }; const isDark = theme === 'dark'; return (
{/* Trigger Button */} {/* Dropdown Menu */} {isOpen && (
{/* Search Box */}
setSearchQuery(e.target.value)} placeholder="Search country name or code..." className={`w-full rounded-xl pl-8 pr-8 py-1.5 text-xs font-semibold focus:outline-none transition-colors ${ isDark ? 'bg-slate-950 border border-slate-800 text-white placeholder:text-slate-600 focus:border-indigo-500' : 'bg-white border border-slate-200 text-slate-800 placeholder:text-slate-400 focus:border-indigo-500' }`} /> {searchQuery && ( )}
{/* Options List */}
{filteredCountries.length === 0 ? (
No countries found matching “{searchQuery}”
) : ( filteredCountries.map((c) => { const isSelected = c.country.toLowerCase() === value.toLowerCase(); return ( ); }) )}
)}
); };