lite-phone-input 0.3.0 → 0.5.0

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.
@@ -2805,7 +2805,27 @@ var phone_countries_default = [
2805
2805
  // src/core/format.ts
2806
2806
  var TRAILING_SEP = /[\s\-]+$/;
2807
2807
  var FALLBACK_GROUP = /(.{4})(?=.)/g;
2808
- var NON_ASCII_DIGITS = /[\u0660-\u0669\u06F0-\u06F9]/g;
2808
+ var NUMERAL_BASES = [
2809
+ 1632,
2810
+ // Arabic-Indic ٠-٩
2811
+ 1776,
2812
+ // Persian ۰-۹
2813
+ 2406,
2814
+ // Devanagari ०-९
2815
+ 2534,
2816
+ // Bengali ০-৯
2817
+ 3664,
2818
+ // Thai ๐-๙
2819
+ 3792,
2820
+ // Lao ໐-໙
2821
+ 4160,
2822
+ // Myanmar ၀-၉
2823
+ 6112,
2824
+ // Khmer ០-៩
2825
+ 65296
2826
+ // Fullwidth 0-9
2827
+ ];
2828
+ var NON_ASCII_DIGITS = /[\u0660-\u0669\u06F0-\u06F9\u0966-\u096F\u09E6-\u09EF\u0E50-\u0E59\u0ED0-\u0ED9\u1040-\u1049\u17E0-\u17E9\uFF10-\uFF19]/g;
2809
2829
  function isRelevantChar(ch) {
2810
2830
  const c = ch.charCodeAt(0);
2811
2831
  return c >= 48 && c <= 57 || c === 43;
@@ -2855,9 +2875,18 @@ function extractDigits(value) {
2855
2875
  function normalizeNumerals(value) {
2856
2876
  return value.replace(NON_ASCII_DIGITS, (c) => {
2857
2877
  const code = c.charCodeAt(0);
2858
- return code <= 1641 ? String(code - 1632) : String(code - 1776);
2878
+ for (const base of NUMERAL_BASES) {
2879
+ if (code >= base && code <= base + 9) {
2880
+ return String(code - base);
2881
+ }
2882
+ }
2883
+ return c;
2859
2884
  });
2860
2885
  }
2886
+ function isNonAsciiDigit(ch) {
2887
+ const code = ch.charCodeAt(0);
2888
+ return NUMERAL_BASES.some((base) => code >= base && code <= base + 9);
2889
+ }
2861
2890
 
2862
2891
  // src/core/countries.ts
2863
2892
  function getFlag(code) {
@@ -3270,6 +3299,7 @@ var PhoneInput = class _PhoneInput {
3270
3299
  constructor(el, options) {
3271
3300
  this.nationalDigits = "";
3272
3301
  this.displayInternational = false;
3302
+ this.userHasInteracted = false;
3273
3303
  this.lastValidation = null;
3274
3304
  this.dropdown = null;
3275
3305
  this.dialCodeEl = null;
@@ -3287,6 +3317,7 @@ var PhoneInput = class _PhoneInput {
3287
3317
  if (this.opts.initialValue) {
3288
3318
  this.setValueInternal(this.opts.initialValue, true);
3289
3319
  }
3320
+ this.invokeGeoIpLookup();
3290
3321
  }
3291
3322
  }
3292
3323
  get isNationalInput() {
@@ -3324,8 +3355,9 @@ var PhoneInput = class _PhoneInput {
3324
3355
  return validatePhone(this.nationalDigits, this.selectedCountry);
3325
3356
  }
3326
3357
  setOptions(opts) {
3358
+ const { geoIpLookup: _, ...rest } = opts;
3327
3359
  const prev = { ...this.opts };
3328
- Object.assign(this.opts, opts);
3360
+ Object.assign(this.opts, rest);
3329
3361
  if (opts.allowedCountries !== void 0 || opts.excludedCountries !== void 0) {
3330
3362
  this.countries = this.filterCountries(getAllCountries());
3331
3363
  if (!this.countries.find((c) => c.code === this.selectedCountry.code)) {
@@ -3367,6 +3399,16 @@ var PhoneInput = class _PhoneInput {
3367
3399
  }
3368
3400
  this.el.innerHTML = "";
3369
3401
  }
3402
+ invokeGeoIpLookup() {
3403
+ const lookup = this.opts.geoIpLookup;
3404
+ if (!lookup) return;
3405
+ this.opts.geoIpLookup = void 0;
3406
+ const signal = this.ac.signal;
3407
+ lookup((countryCode) => {
3408
+ if (signal.aborted || this.userHasInteracted || !countryCode) return;
3409
+ this.setCountry(countryCode);
3410
+ });
3411
+ }
3370
3412
  // --- DOM Construction ---
3371
3413
  buildDOM() {
3372
3414
  this.containerEl = document.createElement("div");
@@ -3466,6 +3508,7 @@ var PhoneInput = class _PhoneInput {
3466
3508
  this.inputEl.addEventListener("paste", (e) => this.handlePaste(e), { signal });
3467
3509
  }
3468
3510
  handleInput(e) {
3511
+ this.userHasInteracted = true;
3469
3512
  if (this.opts.strict !== false && e.data === "+" && this.isNationalInput) {
3470
3513
  this.inputEl.value = this.inputEl.value.replace("+", "");
3471
3514
  }
@@ -3481,7 +3524,7 @@ var PhoneInput = class _PhoneInput {
3481
3524
  const formatted = this.formatNationalValue(digits);
3482
3525
  const np = this.shouldPrependPrefix ? this.selectedCountry.nationalPrefix : "";
3483
3526
  const display = !digits && np && extractDigits(raw).length > 0 ? np : digits && np ? np + formatted : formatted;
3484
- const newCursor = this.getNationalCursor(this.inputEl.value, oldCursor, formatted);
3527
+ const newCursor = this.getNationalCursor(raw, oldCursor, formatted);
3485
3528
  this.inputEl.value = display;
3486
3529
  this.inputEl.setSelectionRange(newCursor, newCursor);
3487
3530
  } else {
@@ -3508,7 +3551,7 @@ var PhoneInput = class _PhoneInput {
3508
3551
  } else {
3509
3552
  formatted = this.formatNationalValue(national);
3510
3553
  }
3511
- const newCursor = getCursorPosition(this.inputEl.value, oldCursor, formatted);
3554
+ const newCursor = getCursorPosition(raw, oldCursor, formatted);
3512
3555
  this.inputEl.value = formatted;
3513
3556
  this.inputEl.setSelectionRange(newCursor, newCursor);
3514
3557
  }
@@ -3523,14 +3566,12 @@ var PhoneInput = class _PhoneInput {
3523
3566
  if (!this.isNationalInput && e.key === "+" && this.inputEl.selectionStart === 0) {
3524
3567
  return;
3525
3568
  }
3526
- if (e.key.length === 1 && !/\d/.test(e.key)) {
3527
- const code = e.key.charCodeAt(0);
3528
- if (!(code >= 1632 && code <= 1641 || code >= 1776 && code <= 1785)) {
3529
- e.preventDefault();
3530
- }
3569
+ if (e.key.length === 1 && !/\d/.test(e.key) && !isNonAsciiDigit(e.key)) {
3570
+ e.preventDefault();
3531
3571
  }
3532
3572
  }
3533
3573
  handlePaste(e) {
3574
+ this.userHasInteracted = true;
3534
3575
  e.preventDefault();
3535
3576
  const text = e.clipboardData?.getData("text") ?? "";
3536
3577
  if (!text) return;
@@ -3569,6 +3610,7 @@ var PhoneInput = class _PhoneInput {
3569
3610
  // --- Dropdown ---
3570
3611
  openDropdown() {
3571
3612
  if (this.dropdown) return;
3613
+ this.userHasInteracted = true;
3572
3614
  this.dropdown = new Dropdown({
3573
3615
  countries: this.countries,
3574
3616
  preferredCountries: this.opts.preferredCountries ?? [],