import React from 'react'; import { COUNTRY_CODES, getCountryRuleByDialCode, getCountryRuleByName } from '../../constants/countries'; import { cleanPhoneInput } from '../../utils/validation'; export interface PhoneInputProps { dialCode: string; phone: string; // local digits only country?: string; onDialCodeChange?: (newCode: string, newCountry: string) => void; onPhoneChange: (newPhone: string) => void; error?: string | null; disabled?: boolean; theme?: 'dark' | 'light'; id?: string; clearOnDialCodeChange?: boolean; } export const PhoneInput: React.FC = ({ dialCode, phone, country, onDialCodeChange, onPhoneChange, error, disabled = false, theme = 'dark', id = 'phone-input', clearOnDialCodeChange = false, }) => { let activeRule = getCountryRuleByDialCode(dialCode); if (!activeRule && country) { activeRule = getCountryRuleByName(country); } if (!activeRule) { activeRule = getCountryRuleByDialCode('+91')!; } const handleDialCodeSelect = (e: React.ChangeEvent) => { const newCode = e.target.value; const rule = getCountryRuleByDialCode(newCode); const newCountry = rule ? rule.country : (country || 'India'); if (onDialCodeChange) { onDialCodeChange(newCode, newCountry); } if (clearOnDialCodeChange) { onPhoneChange(''); } else if (rule) { onPhoneChange(cleanPhoneInput(phone, rule.maxLength, false)); } }; const handlePhoneInputChange = (e: React.ChangeEvent) => { const inputVal = e.target.value; const trimmed = inputVal.trim(); // Check if input contains an explicit international prefix '+' if (trimmed.startsWith('+')) { const sorted = [...COUNTRY_CODES].sort((a, b) => b.code.length - a.code.length); const matched = sorted.find((c) => trimmed.startsWith(c.code)); if (matched) { if (onDialCodeChange && matched.code !== dialCode) { onDialCodeChange(matched.code, matched.country); } const rest = trimmed.slice(matched.code.length).replace(/\D/g, ''); onPhoneChange(rest.slice(0, matched.maxLength)); return; } } // Check if input was pasted with dial code digits without '+' (e.g. 919876543210) const dialDigits = activeRule.code.replace(/\D/g, ''); const rawDigits = trimmed.replace(/\D/g, ''); if ( dialDigits && rawDigits.startsWith(dialDigits) && rawDigits.length > activeRule.maxLength ) { const rest = rawDigits.slice(dialDigits.length); onPhoneChange(rest.slice(0, activeRule.maxLength)); return; } // Strictly numeric local digits, capped at active country's maxLength const cleaned = cleanPhoneInput(inputVal, activeRule.maxLength, false); onPhoneChange(cleaned); }; const handleKeyDown = (e: React.KeyboardEvent) => { // Allow navigation keys, backspace, delete, tab, copy/paste shortcuts if ( ['Backspace', 'Delete', 'ArrowLeft', 'ArrowRight', 'Tab', 'Enter', 'Escape'].includes(e.key) || (e.ctrlKey || e.metaKey) ) { return; } // Allow '+' as leading character to support international number paste/typing if (e.key === '+' && (e.currentTarget.selectionStart === 0 || !phone)) { return; } // Block any non-digit if (!/^\d$/.test(e.key)) { e.preventDefault(); } }; const isDark = theme === 'dark'; return (
{/* Country Code Dropdown */}
▼
{/* Numeric-Only Local Phone Input */}
{/* Helper text or Error message */}
{error ? (

{error}

) : (

{activeRule.label || activeRule.country}:{' '} {activeRule.minLength === activeRule.maxLength ? `Requires exactly ${activeRule.maxLength} digits` : `${activeRule.minLength} to ${activeRule.maxLength} digits`}

)} {phone.length}/{activeRule.maxLength}
); };