intl-tel-input 19.2.17 → 19.2.19

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -258,9 +258,9 @@ class Iti {
258
258
  // Translate Countries by object literal provided on config
259
259
  _translateCountryNames() {
260
260
  for (let i = 0; i < this.countries.length; i++) {
261
- const iso = this.countries[i].iso2.toLowerCase();
262
- if (this.options.i18n.hasOwnProperty(iso)) {
263
- this.countries[i].name = this.options.i18n[iso];
261
+ const iso2 = this.countries[i].iso2.toLowerCase();
262
+ if (this.options.i18n.hasOwnProperty(iso2)) {
263
+ this.countries[i].name = this.options.i18n[iso2];
264
264
  }
265
265
  }
266
266
  }
@@ -325,7 +325,7 @@ class Iti {
325
325
  this.preferredCountries = [];
326
326
  for (let i = 0; i < this.options.preferredCountries.length; i++) {
327
327
  const countryCode = this.options.preferredCountries[i].toLowerCase();
328
- const countryData = this._getCountryData(countryCode, false, true);
328
+ const countryData = this._getCountryData(countryCode, true);
329
329
  if (countryData) {
330
330
  this.preferredCountries.push(countryData);
331
331
  }
@@ -520,6 +520,12 @@ class Iti {
520
520
  name: hiddenInputName
521
521
  });
522
522
  wrapper.appendChild(this.hiddenInput);
523
+ // add a 2nd hidden input for the selected country code - this is useful for handling invalid numbers with server-side validation, as getNumber does not always include the international dial code for invalid numbers
524
+ this.hiddenInputCountry = this._createEl("input", {
525
+ type: "hidden",
526
+ name: `${hiddenInputName}_country`
527
+ });
528
+ wrapper.appendChild(this.hiddenInputCountry);
523
529
  }
524
530
  }
525
531
 
@@ -563,7 +569,7 @@ class Iti {
563
569
  // 2. using explicit initialCountry
564
570
  // 3. picking the first preferred country
565
571
  // 4. picking the first country
566
- _setInitialState() {
572
+ _setInitialState(overrideAutoCountry = false) {
567
573
  // fix firefox bug: when first load page (with input with value set to number with intl dial
568
574
  // code) and initialising plugin removes the dial code from the input, then refresh page,
569
575
  // and we try to init plugin again but this time on number without dial code so get grey flag
@@ -582,8 +588,8 @@ class Iti {
582
588
  // flag, else fall back to the default country
583
589
  if (dialCode && !isRegionlessNanp) {
584
590
  this._updateFlagFromNumber(val);
585
- } else if (initialCountry !== "auto") {
586
- const isValidInitialCountry = initialCountry && this._getCountryData(initialCountry, false, true);
591
+ } else if (initialCountry !== "auto" || overrideAutoCountry) {
592
+ const isValidInitialCountry = initialCountry && !!this._getCountryData(initialCountry, true);
587
593
  // see if we should select a flag
588
594
  if (isValidInitialCountry) {
589
595
  this._setFlag(initialCountry.toLowerCase());
@@ -633,6 +639,7 @@ class Iti {
633
639
  _initHiddenInputListener() {
634
640
  this._handleHiddenInputSubmit = () => {
635
641
  this.hiddenInput.value = this.getNumber();
642
+ this.hiddenInputCountry.value = this.getSelectedCountryData().iso2;
636
643
  };
637
644
  if (this.telInput.form) {
638
645
  this.telInput.form.addEventListener(
@@ -719,7 +726,8 @@ class Iti {
719
726
  this.resolveUtilsScriptPromise();
720
727
  }
721
728
 
722
- if (this.options.initialCountry === "auto") {
729
+ // dont bother with IP lookup if we already have a selected country
730
+ if (this.options.initialCountry === "auto" && !this.selectedCountryData.iso2) {
723
731
  this._loadAutoCountry();
724
732
  } else {
725
733
  this.resolveAutoCountryPromise();
@@ -740,15 +748,22 @@ class Iti {
740
748
 
741
749
  if (typeof this.options.geoIpLookup === "function") {
742
750
  this.options.geoIpLookup(
743
- (countryCode) => {
744
- window.intlTelInputGlobals.autoCountry = countryCode.toLowerCase();
745
- // tell all instances the auto country is ready
746
- // TODO: this should just be the current instances
747
- // UPDATE: use setTimeout in case their geoIpLookup function calls this callback straight
748
- // away (e.g. if they have already done the geo ip lookup somewhere else). Using
749
- // setTimeout means that the current thread of execution will finish before executing
750
- // this, which allows the plugin to finish initialising.
751
- setTimeout(() => forEachInstance("handleAutoCountry"));
751
+ (countryCode = "") => {
752
+ const lowerCountryCode = countryCode.toLowerCase();
753
+ const isValid = !!this._getCountryData(lowerCountryCode, true);
754
+ if (isValid) {
755
+ window.intlTelInputGlobals.autoCountry = lowerCountryCode;
756
+ // tell all instances the auto country is ready
757
+ // TODO: this should just be the current instances
758
+ // UPDATE: use setTimeout in case their geoIpLookup function calls this callback straight
759
+ // away (e.g. if they have already done the geo ip lookup somewhere else). Using
760
+ // setTimeout means that the current thread of execution will finish before executing
761
+ // this, which allows the plugin to finish initialising.
762
+ setTimeout(() => forEachInstance("handleAutoCountry"));
763
+ } else {
764
+ this._setInitialState(true);
765
+ forEachInstance("rejectAutoCountryPromise");
766
+ }
752
767
  },
753
768
  () => forEachInstance("rejectAutoCountryPromise")
754
769
  );
@@ -1253,8 +1268,8 @@ class Iti {
1253
1268
  // Note: use getNumeric here because the number has not been formatted yet, so could contain
1254
1269
  // bad chars
1255
1270
  countryCode = "";
1256
- } else if (!number || number === "+") {
1257
- // empty, or just a plus, so default
1271
+ } else if ((!number || number === "+") && !this.selectedCountryData.iso2) {
1272
+ // if no selected flag, and user either clears the input, or just types a plus, then show default
1258
1273
  countryCode = this.defaultCountry;
1259
1274
  }
1260
1275
 
@@ -1295,13 +1310,10 @@ class Iti {
1295
1310
 
1296
1311
  // find the country data for the given country code
1297
1312
  // the ignoreOnlyCountriesOption is only used during init() while parsing the onlyCountries array
1298
- _getCountryData(countryCode, ignoreOnlyCountriesOption, allowFail) {
1299
- const countryList = ignoreOnlyCountriesOption
1300
- ? allCountries
1301
- : this.countries;
1302
- for (let i = 0; i < countryList.length; i++) {
1303
- if (countryList[i].iso2 === countryCode) {
1304
- return countryList[i];
1313
+ _getCountryData(countryCode, allowFail) {
1314
+ for (let i = 0; i < this.countries.length; i++) {
1315
+ if (this.countries[i].iso2 === countryCode) {
1316
+ return this.countries[i];
1305
1317
  }
1306
1318
  }
1307
1319
  if (allowFail) {
@@ -1321,7 +1333,7 @@ class Iti {
1321
1333
 
1322
1334
  // do this first as it will throw an error and stop if countryCode is invalid
1323
1335
  this.selectedCountryData = countryCode
1324
- ? this._getCountryData(countryCode, false, false)
1336
+ ? this._getCountryData(countryCode, false)
1325
1337
  : {};
1326
1338
  // update the defaultCountry - we only need the iso2 from now on, so just store that
1327
1339
  if (this.selectedCountryData.iso2) {