intl-tel-input 24.4.0 → 24.5.1

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.
@@ -2254,17 +2254,18 @@ var Iti = class {
2254
2254
  return;
2255
2255
  }
2256
2256
  if (strictMode) {
2257
- const isInitialPlus = this.telInput.selectionStart === 0 && e.key === "+";
2257
+ const value = this.telInput.value;
2258
+ const alreadyHasPlus = value.charAt(0) === "+";
2259
+ const isInitialPlus = !alreadyHasPlus && this.telInput.selectionStart === 0 && e.key === "+";
2258
2260
  const isNumeric = /^[0-9]$/.test(e.key);
2259
2261
  const isAllowedChar = separateDialCode ? isNumeric : isInitialPlus || isNumeric;
2260
2262
  const fullNumber = this._getFullNumber();
2261
2263
  const coreNumber = intlTelInput.utils.getCoreNumber(fullNumber, this.selectedCountryData.iso2);
2262
2264
  const hasReachedMaxLength = this.maxCoreNumberLength && coreNumber.length >= this.maxCoreNumberLength;
2263
- const selectedText = this.telInput.value.substring(this.telInput.selectionStart, this.telInput.selectionEnd);
2265
+ const selectedText = value.substring(this.telInput.selectionStart, this.telInput.selectionEnd);
2264
2266
  const hasSelectedDigit = /\d/.test(selectedText);
2265
- const currentCaretPos = this.telInput.selectionStart || 0;
2266
- const cursorAtEnd = currentCaretPos === this.telInput.value.length;
2267
- if (!isAllowedChar || hasReachedMaxLength && !hasSelectedDigit && cursorAtEnd) {
2267
+ const isChangingDialCode = isInitialPlus ? true : this._isChangingDialCode(e.key);
2268
+ if (!isAllowedChar || hasReachedMaxLength && !hasSelectedDigit && !isChangingDialCode) {
2268
2269
  e.preventDefault();
2269
2270
  }
2270
2271
  }
@@ -2273,6 +2274,17 @@ var Iti = class {
2273
2274
  this.telInput.addEventListener("keydown", this._handleKeydownEvent);
2274
2275
  }
2275
2276
  }
2277
+ _isChangingDialCode(char) {
2278
+ const value = this.telInput.value;
2279
+ if (value.charAt(0) === "+") {
2280
+ const currentCountry = this.selectedCountryData.iso2;
2281
+ const newValue = value.slice(0, this.telInput.selectionStart) + char + value.slice(this.telInput.selectionEnd);
2282
+ const newFullNumber = this._getFullNumber(newValue);
2283
+ const newCountry = this._getCountryFromNumber(newFullNumber);
2284
+ return newCountry !== currentCountry;
2285
+ }
2286
+ return false;
2287
+ }
2276
2288
  //* Adhere to the input's maxlength attr.
2277
2289
  _cap(number) {
2278
2290
  const max = parseInt(this.telInput.getAttribute("maxlength") || "", 10);
@@ -2495,6 +2507,13 @@ var Iti = class {
2495
2507
  //* Check if need to select a new country based on the given number
2496
2508
  //* Note: called from _setInitialState, keyup handler, setNumber.
2497
2509
  _updateCountryFromNumber(fullNumber) {
2510
+ const iso2 = this._getCountryFromNumber(fullNumber);
2511
+ if (iso2 !== null) {
2512
+ return this._setCountry(iso2);
2513
+ }
2514
+ return false;
2515
+ }
2516
+ _getCountryFromNumber(fullNumber) {
2498
2517
  const plusIndex = fullNumber.indexOf("+");
2499
2518
  let number = plusIndex ? fullNumber.substring(plusIndex) : fullNumber;
2500
2519
  const selectedDialCode = this.selectedCountryData.dialCode;
@@ -2510,7 +2529,6 @@ var Iti = class {
2510
2529
  }
2511
2530
  const dialCode = this._getDialCode(number, true);
2512
2531
  const numeric = getNumeric(number);
2513
- let iso2 = null;
2514
2532
  if (dialCode) {
2515
2533
  const iso2Codes = this.dialCodeToIso2Map[getNumeric(dialCode)];
2516
2534
  const alreadySelected = iso2Codes.indexOf(this.selectedCountryData.iso2) !== -1 && numeric.length <= dialCode.length - 1;
@@ -2518,20 +2536,16 @@ var Iti = class {
2518
2536
  if (!isRegionlessNanpNumber && !alreadySelected) {
2519
2537
  for (let j = 0; j < iso2Codes.length; j++) {
2520
2538
  if (iso2Codes[j]) {
2521
- iso2 = iso2Codes[j];
2522
- break;
2539
+ return iso2Codes[j];
2523
2540
  }
2524
2541
  }
2525
2542
  }
2526
2543
  } else if (number.charAt(0) === "+" && numeric.length) {
2527
- iso2 = "";
2544
+ return "";
2528
2545
  } else if ((!number || number === "+") && !this.selectedCountryData.iso2) {
2529
- iso2 = this.defaultCountry;
2530
- }
2531
- if (iso2 !== null) {
2532
- return this._setCountry(iso2);
2546
+ return this.defaultCountry;
2533
2547
  }
2534
- return false;
2548
+ return null;
2535
2549
  }
2536
2550
  //* Remove highlighting from other list items and highlight the given item.
2537
2551
  _highlightListItem(listItem, shouldFocus) {
@@ -2801,8 +2815,8 @@ var Iti = class {
2801
2815
  return dialCode;
2802
2816
  }
2803
2817
  //* Get the input val, adding the dial code if separateDialCode is enabled.
2804
- _getFullNumber() {
2805
- const val = this.telInput.value.trim();
2818
+ _getFullNumber(overrideVal) {
2819
+ const val = overrideVal || this.telInput.value.trim();
2806
2820
  const { dialCode } = this.selectedCountryData;
2807
2821
  let prefix;
2808
2822
  const numericVal = getNumeric(val);
@@ -3066,7 +3080,7 @@ var intlTelInput = Object.assign(
3066
3080
  //* A map from instance ID to instance object.
3067
3081
  instances: {},
3068
3082
  loadUtils,
3069
- version: "24.4.0"
3083
+ version: "24.5.1"
3070
3084
  }
3071
3085
  );
3072
3086
  var intl_tel_input_default = intlTelInput;