diff --git a/src/lib/PhoneInput.svelte b/src/lib/PhoneInput.svelte index cd4ba2a..596673c 100644 --- a/src/lib/PhoneInput.svelte +++ b/src/lib/PhoneInput.svelte @@ -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); + } } } }}