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;
const input = e.target as HTMLInputElement;
// Format the input string
const formatter = new AsYouType(formatterCountryCode);
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) {
setTimeout(() => {
input.value = formatted;
@@ -197,13 +199,18 @@
}
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();
if (country) {
setCountryByISO(country);
setTimeout(() => {
if (lastValue) input.value = lastValue.formatNational();
}, 1);
// If the phone value has been set before, update it with the new value
if (value) {
setTimeout(() => {
if (lastValue) input.value = lastValue.formatNational();
}, 1);
}
}
}
}}