lite-phone-input 0.3.0 → 0.4.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.
@@ -45,6 +45,7 @@ interface PhoneInputOptions {
45
45
  initialValue?: string;
46
46
  containerClass?: string;
47
47
  dropdownContainer?: HTMLElement;
48
+ geoIpLookup?: (callback: (countryCode: string | null) => void) => void;
48
49
  onChange?: (e164: string, country: Country, validation: ValidationResult) => void;
49
50
  onCountryChange?: (country: Country) => void;
50
51
  onValidationChange?: (validation: ValidationResult) => void;
@@ -45,6 +45,7 @@ interface PhoneInputOptions {
45
45
  initialValue?: string;
46
46
  containerClass?: string;
47
47
  dropdownContainer?: HTMLElement;
48
+ geoIpLookup?: (callback: (countryCode: string | null) => void) => void;
48
49
  onChange?: (e164: string, country: Country, validation: ValidationResult) => void;
49
50
  onCountryChange?: (country: Country) => void;
50
51
  onValidationChange?: (validation: ValidationResult) => void;
@@ -3273,6 +3273,7 @@ var PhoneInput = class _PhoneInput {
3273
3273
  constructor(el, options) {
3274
3274
  this.nationalDigits = "";
3275
3275
  this.displayInternational = false;
3276
+ this.userHasInteracted = false;
3276
3277
  this.lastValidation = null;
3277
3278
  this.dropdown = null;
3278
3279
  this.dialCodeEl = null;
@@ -3290,6 +3291,7 @@ var PhoneInput = class _PhoneInput {
3290
3291
  if (this.opts.initialValue) {
3291
3292
  this.setValueInternal(this.opts.initialValue, true);
3292
3293
  }
3294
+ this.invokeGeoIpLookup();
3293
3295
  }
3294
3296
  }
3295
3297
  get isNationalInput() {
@@ -3327,8 +3329,9 @@ var PhoneInput = class _PhoneInput {
3327
3329
  return validatePhone(this.nationalDigits, this.selectedCountry);
3328
3330
  }
3329
3331
  setOptions(opts) {
3332
+ const { geoIpLookup: _, ...rest } = opts;
3330
3333
  const prev = { ...this.opts };
3331
- Object.assign(this.opts, opts);
3334
+ Object.assign(this.opts, rest);
3332
3335
  if (opts.allowedCountries !== void 0 || opts.excludedCountries !== void 0) {
3333
3336
  this.countries = this.filterCountries(getAllCountries());
3334
3337
  if (!this.countries.find((c) => c.code === this.selectedCountry.code)) {
@@ -3370,6 +3373,16 @@ var PhoneInput = class _PhoneInput {
3370
3373
  }
3371
3374
  this.el.innerHTML = "";
3372
3375
  }
3376
+ invokeGeoIpLookup() {
3377
+ const lookup = this.opts.geoIpLookup;
3378
+ if (!lookup) return;
3379
+ this.opts.geoIpLookup = void 0;
3380
+ const signal = this.ac.signal;
3381
+ lookup((countryCode) => {
3382
+ if (signal.aborted || this.userHasInteracted || !countryCode) return;
3383
+ this.setCountry(countryCode);
3384
+ });
3385
+ }
3373
3386
  // --- DOM Construction ---
3374
3387
  buildDOM() {
3375
3388
  this.containerEl = document.createElement("div");
@@ -3469,6 +3482,7 @@ var PhoneInput = class _PhoneInput {
3469
3482
  this.inputEl.addEventListener("paste", (e) => this.handlePaste(e), { signal });
3470
3483
  }
3471
3484
  handleInput(e) {
3485
+ this.userHasInteracted = true;
3472
3486
  if (this.opts.strict !== false && e.data === "+" && this.isNationalInput) {
3473
3487
  this.inputEl.value = this.inputEl.value.replace("+", "");
3474
3488
  }
@@ -3534,6 +3548,7 @@ var PhoneInput = class _PhoneInput {
3534
3548
  }
3535
3549
  }
3536
3550
  handlePaste(e) {
3551
+ this.userHasInteracted = true;
3537
3552
  e.preventDefault();
3538
3553
  const text = e.clipboardData?.getData("text") ?? "";
3539
3554
  if (!text) return;
@@ -3572,6 +3587,7 @@ var PhoneInput = class _PhoneInput {
3572
3587
  // --- Dropdown ---
3573
3588
  openDropdown() {
3574
3589
  if (this.dropdown) return;
3590
+ this.userHasInteracted = true;
3575
3591
  this.dropdown = new Dropdown({
3576
3592
  countries: this.countries,
3577
3593
  preferredCountries: this.opts.preferredCountries ?? [],
@@ -3830,6 +3846,7 @@ var WIDGET_KEYS = /* @__PURE__ */ new Set([
3830
3846
  "initialValue",
3831
3847
  "containerClass",
3832
3848
  "dropdownContainer",
3849
+ "geoIpLookup",
3833
3850
  "onChange",
3834
3851
  "onCountryChange",
3835
3852
  "onValidationChange",
@@ -3887,6 +3904,7 @@ var PhoneInput2 = forwardRef(
3887
3904
  hiddenInput: p.hiddenInput,
3888
3905
  containerClass: p.containerClass,
3889
3906
  dropdownContainer: p.dropdownContainer,
3907
+ geoIpLookup: p.geoIpLookup,
3890
3908
  initialValue: p.initialValue,
3891
3909
  inputAttributes,
3892
3910
  // Callbacks read from ref so they always use the latest version