fix/issue-84-country-code-search #112
|
|
@ -5,7 +5,7 @@ import {
|
|||
} from 'lucide-react';
|
||||
import type { UserProfile, ModuleItem, Currency } from '../../types';
|
||||
import { getCountryRuleByName } from '../../constants/countries';
|
||||
import { cleanPhoneInput, parsePhoneNumber, formatE164Phone, SPECIAL_CHAR_REGEX } from '../../utils/validation';
|
||||
import { parsePhoneNumber, formatE164Phone, SPECIAL_CHAR_REGEX } from '../../utils/validation';
|
||||
import { PhoneInput } from '../common/PhoneInput';
|
||||
import { CountrySelect } from '../common/CountrySelect';
|
||||
|
||||
|
|
@ -90,7 +90,7 @@ export const ProfileDrawer: React.FC<ProfileDrawerProps> = ({
|
|||
setDialCode(p.dialCode);
|
||||
setLocalPhone(p.localDigits);
|
||||
}
|
||||
}, [profilePhone, profileCountry, isOpen]);
|
||||
}, [isOpen]);
|
||||
|
||||
const handleLocalPhoneChange = (newLocal: string) => {
|
||||
setLocalPhone(newLocal);
|
||||
|
|
@ -99,22 +99,30 @@ export const ProfileDrawer: React.FC<ProfileDrawerProps> = ({
|
|||
|
||||
const handleDialCodeChange = (newCode: string, newCountry: string) => {
|
||||
setDialCode(newCode);
|
||||
setProfileCountry(newCountry);
|
||||
const rule = getCountryRuleByName(newCountry);
|
||||
setCurrency(rule.currency);
|
||||
const clamped = cleanPhoneInput(localPhone, rule.maxLength, false);
|
||||
setLocalPhone(clamped);
|
||||
setProfilePhone(formatE164Phone(newCode, clamped, newCountry));
|
||||
if (newCountry !== profileCountry) {
|
||||
setProfileCountry(newCountry);
|
||||
const rule = getCountryRuleByName(newCountry);
|
||||
if (rule) {
|
||||
setCurrency(rule.currency);
|
||||
}
|
||||
setLocalPhone('');
|
||||
setProfilePhone('');
|
||||
} else {
|
||||
setProfilePhone(localPhone ? formatE164Phone(newCode, localPhone, profileCountry) : '');
|
||||
}
|
||||
};
|
||||
|
||||
const handleCountrySelectChange = (newCountry: string) => {
|
||||
setProfileCountry(newCountry);
|
||||
const rule = getCountryRuleByName(newCountry);
|
||||
setDialCode(rule.code);
|
||||
setCurrency(rule.currency);
|
||||
const clamped = cleanPhoneInput(localPhone, rule.maxLength, false);
|
||||
setLocalPhone(clamped);
|
||||
setProfilePhone(formatE164Phone(rule.code, clamped, newCountry));
|
||||
if (newCountry !== profileCountry) {
|
||||
setProfileCountry(newCountry);
|
||||
const rule = getCountryRuleByName(newCountry);
|
||||
if (rule) {
|
||||
setDialCode(rule.code);
|
||||
setCurrency(rule.currency);
|
||||
}
|
||||
setLocalPhone('');
|
||||
setProfilePhone('');
|
||||
}
|
||||
};
|
||||
|
||||
if (!isOpen || !loggedInUser) return null;
|
||||
|
|
@ -190,6 +198,7 @@ export const ProfileDrawer: React.FC<ProfileDrawerProps> = ({
|
|||
dialCode={dialCode}
|
||||
phone={localPhone}
|
||||
country={profileCountry}
|
||||
clearOnDialCodeChange={true}
|
||||
onDialCodeChange={handleDialCodeChange}
|
||||
onPhoneChange={handleLocalPhoneChange}
|
||||
theme="light"
|
||||
|
|
|
|||
|
|
@ -366,6 +366,10 @@ export const CartDrawer: React.FC<CartDrawerProps> = ({
|
|||
<span>Email:</span>
|
||||
<span className="text-slate-800 font-bold truncate max-w-[180px]">{loggedInUser.email}</span>
|
||||
</div>
|
||||
<div className="flex justify-between text-xs font-semibold text-slate-600">
|
||||
<span>Billing Country:</span>
|
||||
<span className="text-slate-800 font-bold truncate max-w-[180px]">{loggedInUser.country || 'India'}</span>
|
||||
</div>
|
||||
<div className="flex justify-between text-xs font-semibold text-slate-600">
|
||||
<span>Phone:</span>
|
||||
<span className="text-slate-800 font-bold">
|
||||
|
|
|
|||
|
|
@ -1,6 +1,8 @@
|
|||
import React, { useState, useEffect } from 'react';
|
||||
import { X, Phone, ShieldCheck, RefreshCw } from 'lucide-react';
|
||||
import { X, Phone, ShieldCheck, RefreshCw, Globe } from 'lucide-react';
|
||||
import { PhoneInput } from '../common/PhoneInput';
|
||||
import { CountrySelect } from '../common/CountrySelect';
|
||||
import { getCountryRuleByName } from '../../constants/countries';
|
||||
import { parsePhoneNumber, validatePhoneNumber, formatE164Phone } from '../../utils/validation';
|
||||
|
||||
interface BillingPhoneModalProps {
|
||||
|
|
@ -37,6 +39,27 @@ export const BillingPhoneModal: React.FC<BillingPhoneModalProps> = ({
|
|||
|
||||
if (!isOpen) return null;
|
||||
|
||||
const handleCountryChange = (newCountry: string) => {
|
||||
if (newCountry !== country) {
|
||||
setCountry(newCountry);
|
||||
const rule = getCountryRuleByName(newCountry);
|
||||
if (rule) {
|
||||
setDialCode(rule.code);
|
||||
}
|
||||
setPhone('');
|
||||
setError(null);
|
||||
}
|
||||
};
|
||||
|
||||
const handleDialCodeChange = (newCode: string, newCountry: string) => {
|
||||
setDialCode(newCode);
|
||||
if (newCountry !== country) {
|
||||
setCountry(newCountry);
|
||||
setPhone('');
|
||||
}
|
||||
setError(null);
|
||||
};
|
||||
|
||||
const handleSubmit = async (e: React.FormEvent) => {
|
||||
e.preventDefault();
|
||||
setError(null);
|
||||
|
|
@ -99,11 +122,8 @@ export const BillingPhoneModal: React.FC<BillingPhoneModalProps> = ({
|
|||
dialCode={dialCode}
|
||||
phone={phone}
|
||||
country={country}
|
||||
onDialCodeChange={(newCode, newCountry) => {
|
||||
setDialCode(newCode);
|
||||
setCountry(newCountry);
|
||||
setError(null);
|
||||
}}
|
||||
clearOnDialCodeChange={true}
|
||||
onDialCodeChange={handleDialCodeChange}
|
||||
onPhoneChange={(newPhone) => {
|
||||
setPhone(newPhone);
|
||||
setError(null);
|
||||
|
|
@ -114,9 +134,24 @@ export const BillingPhoneModal: React.FC<BillingPhoneModalProps> = ({
|
|||
/>
|
||||
</div>
|
||||
|
||||
<div className="space-y-1.5">
|
||||
<label className="text-xs font-bold text-slate-600 flex items-center gap-1">
|
||||
<Globe className="h-3.5 w-3.5 text-indigo-500" /> Billing Country
|
||||
</label>
|
||||
<CountrySelect
|
||||
value={country}
|
||||
onChange={handleCountryChange}
|
||||
theme="light"
|
||||
size="md"
|
||||
showDialCode={true}
|
||||
showCurrency={false}
|
||||
disabled={saving}
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div className="flex items-center gap-2 text-[11px] text-slate-500 bg-slate-50 p-3 rounded-xl border border-slate-200/60">
|
||||
<ShieldCheck className="h-4 w-4 text-emerald-500 flex-shrink-0" />
|
||||
<span>This contact number will appear on your compliance tax invoice and payment receipts.</span>
|
||||
<span>This contact number and billing country will appear on your compliance tax invoice and payment receipts.</span>
|
||||
</div>
|
||||
|
||||
<div className="flex gap-3 pt-2">
|
||||
|
|
@ -141,4 +176,3 @@ export const BillingPhoneModal: React.FC<BillingPhoneModalProps> = ({
|
|||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -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<PhoneInputProps> = ({
|
|||
id = 'phone-input',
|
||||
clearOnDialCodeChange = false,
|
||||
}) => {
|
||||
const [isDropdownOpen, setIsDropdownOpen] = useState(false);
|
||||
const [searchQuery, setSearchQuery] = useState('');
|
||||
const dropdownRef = useRef<HTMLDivElement>(null);
|
||||
const searchInputRef = useRef<HTMLInputElement>(null);
|
||||
|
||||
// Prioritize country name if available, fallback to dial code
|
||||
let activeRule = country ? getCountryRuleByName(country) : undefined;
|
||||
if (!activeRule && dialCode) {
|
||||
|
|
@ -36,17 +48,79 @@ export const PhoneInput: React.FC<PhoneInputProps> = ({
|
|||
activeRule = getCountryRuleByDialCode('+91')!;
|
||||
}
|
||||
|
||||
const handleCountrySelect = (e: React.ChangeEvent<HTMLSelectElement>) => {
|
||||
const selectedCountryName = e.target.value;
|
||||
const rule = getCountryRuleByName(selectedCountryName);
|
||||
// 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(rule.code, rule.country);
|
||||
onDialCodeChange(cc.code, cc.country);
|
||||
}
|
||||
if (clearOnDialCodeChange) {
|
||||
onPhoneChange('');
|
||||
} else {
|
||||
onPhoneChange(cleanPhoneInput(phone, rule.maxLength, false));
|
||||
onPhoneChange(cleanPhoneInput(phone, cc.maxLength, false));
|
||||
}
|
||||
setIsDropdownOpen(false);
|
||||
setSearchQuery('');
|
||||
};
|
||||
|
||||
const handlePhoneInputChange = (e: React.ChangeEvent<HTMLInputElement>) => {
|
||||
|
|
@ -108,27 +182,123 @@ export const PhoneInput: React.FC<PhoneInputProps> = ({
|
|||
return (
|
||||
<div className="space-y-1.5 w-full">
|
||||
<div className="flex gap-2 w-full">
|
||||
{/* Country Code Dropdown: keyed by unique country name to avoid +1 (US vs CA) collisions */}
|
||||
<div className="relative shrink-0 w-[110px]">
|
||||
<select
|
||||
value={activeRule.country}
|
||||
onChange={handleCountrySelect}
|
||||
{/* Searchable Country Code Dropdown */}
|
||||
<div ref={dropdownRef} className="relative shrink-0">
|
||||
<button
|
||||
type="button"
|
||||
disabled={disabled}
|
||||
className={`w-full rounded-xl px-2.5 py-2.5 text-xs font-semibold appearance-none pr-6 cursor-pointer focus:outline-none transition-colors ${
|
||||
onClick={() => !disabled && setIsDropdownOpen(!isDropdownOpen)}
|
||||
className={`w-[110px] flex items-center justify-between rounded-xl px-2.5 py-2.5 text-xs font-semibold cursor-pointer focus:outline-none transition-colors ${
|
||||
isDark
|
||||
? 'bg-slate-950 border border-slate-800 text-white focus:border-indigo-500'
|
||||
: 'bg-white border border-slate-200 text-slate-800 focus:border-indigo-500 focus:ring-1 focus:ring-indigo-500'
|
||||
} ${disabled ? 'opacity-50 cursor-not-allowed' : ''}`}
|
||||
>
|
||||
{COUNTRY_CODES.map((cc) => (
|
||||
<option key={`${cc.country}-${cc.code}`} value={cc.country}>
|
||||
{cc.label} ({cc.country})
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
<div className="absolute right-2.5 top-1/2 -translate-y-1/2 pointer-events-none text-slate-400 text-[10px]">
|
||||
▼
|
||||
</div>
|
||||
<div className="flex items-center gap-1.5 truncate">
|
||||
<span className="text-sm shrink-0 leading-none">{getFlag(activeRule)}</span>
|
||||
<span className="font-mono text-xs font-bold">{activeRule.code}</span>
|
||||
</div>
|
||||
<ChevronDown
|
||||
className={`h-3.5 w-3.5 shrink-0 text-slate-400 transition-transform duration-200 ${
|
||||
isDropdownOpen ? 'rotate-180' : ''
|
||||
}`}
|
||||
/>
|
||||
</button>
|
||||
|
||||
{/* Searchable Dropdown Popover */}
|
||||
{isDropdownOpen && (
|
||||
<div
|
||||
className={`absolute left-0 top-full mt-1.5 z-50 w-72 sm:w-80 rounded-2xl shadow-2xl border overflow-hidden flex flex-col animate-in fade-in zoom-in-95 duration-150 ${
|
||||
isDark
|
||||
? 'bg-slate-900 border-slate-800 text-white'
|
||||
: 'bg-white border-slate-200 text-slate-800'
|
||||
}`}
|
||||
>
|
||||
{/* Search Box */}
|
||||
<div
|
||||
className={`p-2.5 border-b shrink-0 ${
|
||||
isDark ? 'border-slate-800 bg-slate-950/60' : 'border-slate-100 bg-slate-50/70'
|
||||
}`}
|
||||
>
|
||||
<div className="relative">
|
||||
<Search
|
||||
className={`absolute left-3 top-1/2 -translate-y-1/2 h-3.5 w-3.5 ${
|
||||
isDark ? 'text-slate-500' : 'text-slate-400'
|
||||
}`}
|
||||
/>
|
||||
<input
|
||||
ref={searchInputRef}
|
||||
type="text"
|
||||
value={searchQuery}
|
||||
onChange={(e) => 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 && (
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => setSearchQuery('')}
|
||||
className={`absolute right-2.5 top-1/2 -translate-y-1/2 p-0.5 rounded-full hover:bg-slate-200/50 ${
|
||||
isDark ? 'text-slate-400 hover:text-white' : 'text-slate-400 hover:text-slate-600'
|
||||
}`}
|
||||
>
|
||||
<X className="h-3 w-3" />
|
||||
</button>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Alphabetically Sorted Options List */}
|
||||
<div className="max-h-60 overflow-y-auto overscroll-contain py-1">
|
||||
{filteredCountryCodes.length === 0 ? (
|
||||
<div
|
||||
className={`p-4 text-center text-xs ${
|
||||
isDark ? 'text-slate-500' : 'text-slate-400'
|
||||
}`}
|
||||
>
|
||||
No country codes found matching “{searchQuery}”
|
||||
</div>
|
||||
) : (
|
||||
filteredCountryCodes.map((cc) => {
|
||||
const selected = isSelected(cc);
|
||||
return (
|
||||
<button
|
||||
key={`${cc.country}-${cc.code}`}
|
||||
type="button"
|
||||
onClick={() => handleSelectCountry(cc)}
|
||||
className={`w-full flex items-center justify-between px-3.5 py-2 text-left text-xs transition-colors cursor-pointer ${
|
||||
selected
|
||||
? isDark
|
||||
? 'bg-indigo-600/20 text-indigo-400 font-bold'
|
||||
: 'bg-indigo-50 text-indigo-700 font-bold'
|
||||
: isDark
|
||||
? 'hover:bg-slate-800/80 text-slate-200'
|
||||
: 'hover:bg-slate-50 text-slate-700'
|
||||
}`}
|
||||
>
|
||||
<div className="flex items-center gap-2.5 min-w-0 pr-2">
|
||||
<span className="text-base shrink-0 leading-none">{getFlag(cc)}</span>
|
||||
<span className="truncate">{cc.country}</span>
|
||||
<span
|
||||
className={`text-[11px] font-mono shrink-0 ${
|
||||
isDark ? 'text-slate-400' : 'text-slate-500'
|
||||
}`}
|
||||
>
|
||||
({cc.code})
|
||||
</span>
|
||||
</div>
|
||||
{selected && <Check className="h-3.5 w-3.5 shrink-0 text-indigo-500" />}
|
||||
</button>
|
||||
);
|
||||
})
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{/* Numeric-Only Local Phone Input */}
|
||||
|
|
|
|||
|
|
@ -55,7 +55,7 @@ export const validatePhoneNumber = (
|
|||
|
||||
// Resolve country rule: prioritize explicit country name, fallback to dialCode
|
||||
let rule = country ? getCountryRuleByName(country) : undefined;
|
||||
if (!rule && dialCode) {
|
||||
if ((!rule || rule.country === 'Other') && dialCode) {
|
||||
rule = getCountryRuleByDialCode(dialCode, country);
|
||||
}
|
||||
if (!rule) {
|
||||
|
|
@ -181,6 +181,18 @@ export const parsePhoneNumber = (
|
|||
|
||||
// Fallback regex match from longest dial code
|
||||
const sorted = [...COUNTRY_CODES].sort((a, b) => b.code.length - a.code.length);
|
||||
|
||||
// If fallbackCountry is specified and matches the prefix, prioritize it
|
||||
if (fallbackCountry && trimmed.startsWith(defaultRule.code)) {
|
||||
const rawLocal = trimmed.slice(defaultRule.code.length).replace(/\D/g, '');
|
||||
return {
|
||||
country: defaultRule.country,
|
||||
dialCode: defaultRule.code,
|
||||
localDigits: rawLocal.slice(0, defaultRule.maxLength),
|
||||
iso2: defaultRule.iso2,
|
||||
};
|
||||
}
|
||||
|
||||
const matched = sorted.find((c) => trimmed.startsWith(c.code));
|
||||
if (matched) {
|
||||
const rawLocal = trimmed.slice(matched.code.length).replace(/\D/g, '');
|
||||
|
|
|
|||
Loading…
Reference in New Issue