feat: synchronize selected flag with external value changes in TelephoneInput

This commit is contained in:
seaznCode 2026-01-19 21:55:39 +01:00
parent 860e88c3be
commit f1ddba461a

View File

@ -212,6 +212,31 @@ const TelephoneInput = forwardRef<TelephoneInputHandle, TelephoneInputProps>(
} }
}, [initialCountry, rest.id, rest.name]) }, [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, () => ({ useImperativeHandle(ref, () => ({
getNumber: () => { getNumber: () => {
const raw = inputRef.current?.value || '' const raw = inputRef.current?.value || ''