From f1ddba461a3316319fb90bf589ef631a7e9dddea Mon Sep 17 00:00:00 2001 From: seaznCode Date: Mon, 19 Jan 2026 21:55:39 +0100 Subject: [PATCH] feat: synchronize selected flag with external value changes in TelephoneInput --- src/app/components/phone/telephoneInput.tsx | 25 +++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/app/components/phone/telephoneInput.tsx b/src/app/components/phone/telephoneInput.tsx index 4c66105..c7a581c 100644 --- a/src/app/components/phone/telephoneInput.tsx +++ b/src/app/components/phone/telephoneInput.tsx @@ -212,6 +212,31 @@ const TelephoneInput = forwardRef( } }, [initialCountry, rest.id, rest.name]) + // Keep selected flag in sync when value is prefilled/changed externally + useEffect(() => { + const inputEl = inputRef.current + const instance = itiRef.current + if (!inputEl || !instance) return + + const raw = (inputEl.value || '').trim() + if (!raw) return + + // normalize "00" prefix to "+" + const normalized = raw.startsWith('00') ? `+${raw.slice(2)}` : raw + if (!normalized.startsWith('+')) return + + const digits = normalized.replace(/\D/g, '') + if (digits.length < 4) return + if (digits === lastSyncDigitsRef.current) return + lastSyncDigitsRef.current = digits + + try { + instance.setNumber?.(`+${digits}`) + } catch { + // ignore + } + }, [rest.value]) + useImperativeHandle(ref, () => ({ getNumber: () => { const raw = inputRef.current?.value || ''