phone input: fix set country with empty input

This commit is contained in:
Elijah Duffy
2025-07-10 17:35:19 -07:00
parent 5b37bda207
commit db6c2cf4de

View File

@@ -187,9 +187,11 @@
if (!e.target) return; if (!e.target) return;
const input = e.target as HTMLInputElement; const input = e.target as HTMLInputElement;
// Format the input string
const formatter = new AsYouType(formatterCountryCode); const formatter = new AsYouType(formatterCountryCode);
const formatted = formatter.input(input.value); const formatted = formatter.input(input.value);
// If the input length is greater than the last value, use formatted text
if (input.value.length >= lastRawValue.length) { if (input.value.length >= lastRawValue.length) {
setTimeout(() => { setTimeout(() => {
input.value = formatted; input.value = formatted;
@@ -197,15 +199,20 @@
} }
lastValue = formatter.getNumber(); lastValue = formatter.getNumber();
if (formatter.isValid() && formatter.isInternational() && value) { // If we have a valid international number, update the country
if (formatter.isValid() && formatter.isInternational()) {
const country = formatter.getCountry(); const country = formatter.getCountry();
if (country) { if (country) {
setCountryByISO(country); setCountryByISO(country);
// If the phone value has been set before, update it with the new value
if (value) {
setTimeout(() => { setTimeout(() => {
if (lastValue) input.value = lastValue.formatNational(); if (lastValue) input.value = lastValue.formatNational();
}, 1); }, 1);
} }
} }
}
}} }}
onblur={() => { onblur={() => {
value = lastValue; value = lastValue;