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.
@@ -3299,6 +3299,7 @@ var PhoneInput = class _PhoneInput {
3299
3299
  constructor(el, options) {
3300
3300
  this.nationalDigits = "";
3301
3301
  this.displayInternational = false;
3302
+ this.userHasInteracted = false;
3302
3303
  this.lastValidation = null;
3303
3304
  this.dropdown = null;
3304
3305
  this.dialCodeEl = null;
@@ -3316,6 +3317,7 @@ var PhoneInput = class _PhoneInput {
3316
3317
  if (this.opts.initialValue) {
3317
3318
  this.setValueInternal(this.opts.initialValue, true);
3318
3319
  }
3320
+ this.invokeGeoIpLookup();
3319
3321
  }
3320
3322
  }
3321
3323
  get isNationalInput() {
@@ -3353,8 +3355,9 @@ var PhoneInput = class _PhoneInput {
3353
3355
  return validatePhone(this.nationalDigits, this.selectedCountry);
3354
3356
  }
3355
3357
  setOptions(opts) {
3358
+ const { geoIpLookup: _, ...rest } = opts;
3356
3359
  const prev = { ...this.opts };
3357
- Object.assign(this.opts, opts);
3360
+ Object.assign(this.opts, rest);
3358
3361
  if (opts.allowedCountries !== void 0 || opts.excludedCountries !== void 0) {
3359
3362
  this.countries = this.filterCountries(getAllCountries());
3360
3363
  if (!this.countries.find((c) => c.code === this.selectedCountry.code)) {
@@ -3396,6 +3399,16 @@ var PhoneInput = class _PhoneInput {
3396
3399
  }
3397
3400
  this.el.innerHTML = "";
3398
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
+ }
3399
3412
  // --- DOM Construction ---
3400
3413
  buildDOM() {
3401
3414
  this.containerEl = document.createElement("div");
@@ -3495,6 +3508,7 @@ var PhoneInput = class _PhoneInput {
3495
3508
  this.inputEl.addEventListener("paste", (e) => this.handlePaste(e), { signal });
3496
3509
  }
3497
3510
  handleInput(e) {
3511
+ this.userHasInteracted = true;
3498
3512
  if (this.opts.strict !== false && e.data === "+" && this.isNationalInput) {
3499
3513
  this.inputEl.value = this.inputEl.value.replace("+", "");
3500
3514
  }
@@ -3560,6 +3574,7 @@ var PhoneInput = class _PhoneInput {
3560
3574
  }
3561
3575
  }
3562
3576
  handlePaste(e) {
3577
+ this.userHasInteracted = true;
3563
3578
  e.preventDefault();
3564
3579
  const text = e.clipboardData?.getData("text") ?? "";
3565
3580
  if (!text) return;
@@ -3598,6 +3613,7 @@ var PhoneInput = class _PhoneInput {
3598
3613
  // --- Dropdown ---
3599
3614
  openDropdown() {
3600
3615
  if (this.dropdown) return;
3616
+ this.userHasInteracted = true;
3601
3617
  this.dropdown = new Dropdown({
3602
3618
  countries: this.countries,
3603
3619
  preferredCountries: this.opts.preferredCountries ?? [],
@@ -3856,6 +3872,7 @@ var WIDGET_KEYS = /* @__PURE__ */ new Set([
3856
3872
  "initialValue",
3857
3873
  "containerClass",
3858
3874
  "dropdownContainer",
3875
+ "geoIpLookup",
3859
3876
  "onChange",
3860
3877
  "onCountryChange",
3861
3878
  "onValidationChange",
@@ -3913,6 +3930,7 @@ var PhoneInput2 = (0, import_react.forwardRef)(
3913
3930
  hiddenInput: p.hiddenInput,
3914
3931
  containerClass: p.containerClass,
3915
3932
  dropdownContainer: p.dropdownContainer,
3933
+ geoIpLookup: p.geoIpLookup,
3916
3934
  initialValue: p.initialValue,
3917
3935
  inputAttributes,
3918
3936
  // Callbacks read from ref so they always use the latest version