From 1567a96c54fdcd93c57561d470c0d4dfdf888949 Mon Sep 17 00:00:00 2001 From: ShivaniKhaire Date: Tue, 17 Nov 2026 13:12:57 +0530 Subject: [PATCH] feat(store): add search bar, alphabetical sorting, and country code display to phone input (issue #84) - Replace native HTML select in PhoneInput with a custom searchable popover dropdown - Sort country options alphabetically using SORTED_COUNTRY_CODES - Display flag, country name, and dial code clearly for each option - Support search filtering by country name, formatted dial code (+XX), and raw numeric code - Apply seamlessly to Registration (AuthModal) and User Profile (ProfileDrawer) --- src/components/common/PhoneInput.tsx | 218 ++++++++++++++++++++++++--- 1 file changed, 193 insertions(+), 25 deletions(-) diff --git a/src/components/common/PhoneInput.tsx b/src/components/common/PhoneInput.tsx index 311cddb..7f7e60e 100644 --- a/src/components/common/PhoneInput.tsx +++ b/src/components/common/PhoneInput.tsx @@ -1,5 +1,12 @@ -import React from 'react'; -import { COUNTRY_CODES, getCountryRuleByDialCode, getCountryRuleByName } from '../../constants/countries'; +import React, { useState, useRef, useEffect, useMemo } from 'react'; +import { Search, ChevronDown, Check, X } from 'lucide-react'; +import { + COUNTRY_CODES, + SORTED_COUNTRY_CODES, + getCountryRuleByDialCode, + getCountryRuleByName, + type CountryCode, +} from '../../constants/countries'; import { cleanPhoneInput } from '../../utils/validation'; export interface PhoneInputProps { @@ -27,6 +34,11 @@ export const PhoneInput: React.FC = ({ id = 'phone-input', clearOnDialCodeChange = false, }) => { + const [isDropdownOpen, setIsDropdownOpen] = useState(false); + const [searchQuery, setSearchQuery] = useState(''); + const dropdownRef = useRef(null); + const searchInputRef = useRef(null); + let activeRule = getCountryRuleByDialCode(dialCode); if (!activeRule && country) { activeRule = getCountryRuleByName(country); @@ -35,18 +47,79 @@ export const PhoneInput: React.FC = ({ activeRule = getCountryRuleByDialCode('+91')!; } - const handleDialCodeSelect = (e: React.ChangeEvent) => { - const newCode = e.target.value; - const rule = getCountryRuleByDialCode(newCode); - const newCountry = rule ? rule.country : (country || 'India'); + // Focus search input when dropdown opens + useEffect(() => { + if (isDropdownOpen) { + setTimeout(() => { + searchInputRef.current?.focus(); + }, 50); + } else { + setSearchQuery(''); + } + }, [isDropdownOpen]); + + // Handle click outside to close dropdown + useEffect(() => { + const handleClickOutside = (event: MouseEvent) => { + if (dropdownRef.current && !dropdownRef.current.contains(event.target as Node)) { + setIsDropdownOpen(false); + } + }; + + const handleKeyDown = (event: KeyboardEvent) => { + if (event.key === 'Escape') { + setIsDropdownOpen(false); + } + }; + + if (isDropdownOpen) { + document.addEventListener('mousedown', handleClickOutside); + document.addEventListener('keydown', handleKeyDown); + } + + return () => { + document.removeEventListener('mousedown', handleClickOutside); + document.removeEventListener('keydown', handleKeyDown); + }; + }, [isDropdownOpen]); + + // Filter countries alphabetically by country name or dial code + const filteredCountryCodes = useMemo(() => { + if (!searchQuery.trim()) { + return SORTED_COUNTRY_CODES; + } + const q = searchQuery.toLowerCase().trim(); + return SORTED_COUNTRY_CODES.filter((c: CountryCode) => { + const matchCountry = c.country.toLowerCase().includes(q); + const matchCode = c.code.toLowerCase().includes(q); + const rawCode = c.code.replace('+', ''); + const matchRaw = rawCode.includes(q); + return matchCountry || matchCode || matchRaw; + }); + }, [searchQuery]); + + const getFlag = (cc: CountryCode) => { + return cc.label ? cc.label.split(' ')[0] : '🌐'; + }; + + const isSelected = (cc: CountryCode) => { + if (country) { + return cc.country.toLowerCase() === country.toLowerCase() && cc.code === dialCode; + } + return cc.code === dialCode; + }; + + const handleSelectCountry = (cc: CountryCode) => { if (onDialCodeChange) { - onDialCodeChange(newCode, newCountry); + onDialCodeChange(cc.code, cc.country); } if (clearOnDialCodeChange) { onPhoneChange(''); - } else if (rule) { - onPhoneChange(cleanPhoneInput(phone, rule.maxLength, false)); + } else { + onPhoneChange(cleanPhoneInput(phone, cc.maxLength, false)); } + setIsDropdownOpen(false); + setSearchQuery(''); }; const handlePhoneInputChange = (e: React.ChangeEvent) => { @@ -108,27 +181,123 @@ export const PhoneInput: React.FC = ({ return (
- {/* Country Code Dropdown */} -
- -
- ▼ -
+
+ {getFlag(activeRule)} + {activeRule.code} +
+ + + + {/* Searchable Dropdown Popover */} + {isDropdownOpen && ( +
+ {/* Search Box */} +
+
+ + setSearchQuery(e.target.value)} + placeholder="Search country 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 && ( + + )} +
+
+ + {/* Alphabetically Sorted Options List */} +
+ {filteredCountryCodes.length === 0 ? ( +
+ No country codes found matching “{searchQuery}” +
+ ) : ( + filteredCountryCodes.map((cc) => { + const selected = isSelected(cc); + return ( + + ); + }) + )} +
+
+ )}
{/* Numeric-Only Local Phone Input */} @@ -170,4 +339,3 @@ export const PhoneInput: React.FC = ({
); }; -