jackocnr / intl-tel-input

A JavaScript plugin for entering and validating international telephone numbers
https://intl-tel-input.com
MIT License
7.36k stars 1.92k forks source link

Formatting does not return country to default #1654

Closed HKallaste closed 3 weeks ago

HKallaste commented 3 weeks ago

Plugin version

v23.0.4

Steps to reproduce

  1. An input that's been initialized
  2. Call one of the functions that formats the field e.g. setNumber
  3. Change the content of the input via js (for example model gets opened with different values)
  4. Call setNumber again

Expected behaviour

If there is a valid country code: it'll pick out the country for it, if not: then it would reset to the default

Actual behaviour

If you first open a number that lacks the code, it'll stay as default as intended. If you open a number that has the code, and after open one without it - the country will still be set to the previous number's country after the format.

Initialisation options

countryOrder: ["ee", "fi", "se", "no", "dk", "de", "gb"], countrySearch: false, utilsScript: "https://cdn.jsdelivr.net/npm/intl-tel-input@23.0.4/build/js/utils.js", nationalMode: false, (added due to the leading 0 causing issues)

jackocnr commented 3 weeks ago

Sorry I don't understand the issue.

Following your instructions, on the demo site I just did the following in the console:

const iti = window.intlTelInput.instances[0];
iti.setNumber("+17021231234");
document.querySelector("#phone").value = "123"
iti.setNumber("+447947123123");

After the first setNumber, the selected country correctly changes to US. Then after the 2nd setNumber, the selected country correctly changes to UK. So I don't understand the problem. Please can you provide steps to reproduce, being more specific about exactly what values to use to reproduce the issue.

HKallaste commented 3 weeks ago

If the second number is missing the country code, there isn't a case for returning to the default. e.g. iti.setNumber("7947123123");

jackocnr commented 3 weeks ago

As of the latest version, v23.0.12, you should be able to call setCountry() (passing no argument) to reset it to the globe icon. Let me know if you have any other issues.