intl-tel-input 24.5.2 → 24.6.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.
@@ -1659,6 +1659,8 @@ var defaults = {
1659
1659
  i18n: {},
1660
1660
  //* Initial country.
1661
1661
  initialCountry: "",
1662
+ //* Specify the path to the libphonenumber script to enable validation/formatting.
1663
+ loadUtilsOnInit: "",
1662
1664
  //* National vs international formatting for numbers e.g. placeholders and displaying existing numbers.
1663
1665
  nationalMode: true,
1664
1666
  //* Display only these countries.
@@ -1679,7 +1681,7 @@ var defaults = {
1679
1681
  navigator.userAgent
1680
1682
  ) || window.innerWidth <= 500
1681
1683
  ) : false,
1682
- //* Specify the path to the libphonenumber script to enable validation/formatting.
1684
+ //* Deprecated! Use `loadUtilsOnInit` instead.
1683
1685
  utilsScript: "",
1684
1686
  //* The number type to enforce during validation.
1685
1687
  validationNumberType: "MOBILE"
@@ -1741,9 +1743,9 @@ var createEl = (name, attrs, container) => {
1741
1743
  }
1742
1744
  return el;
1743
1745
  };
1744
- var forEachInstance = (method) => {
1746
+ var forEachInstance = (method, ...args) => {
1745
1747
  const { instances } = intlTelInput;
1746
- Object.values(instances).forEach((instance) => instance[method]());
1748
+ Object.values(instances).forEach((instance) => instance[method](...args));
1747
1749
  };
1748
1750
  var Iti = class {
1749
1751
  constructor(input, customOptions = {}) {
@@ -2191,14 +2193,21 @@ var Iti = class {
2191
2193
  }
2192
2194
  //* Init many requests: utils script / geo ip lookup.
2193
2195
  _initRequests() {
2194
- const { utilsScript, initialCountry, geoIpLookup } = this.options;
2195
- if (utilsScript && !intlTelInput.utils) {
2196
+ let { loadUtilsOnInit, utilsScript, initialCountry, geoIpLookup } = this.options;
2197
+ if (!loadUtilsOnInit && utilsScript) {
2198
+ console.warn("intl-tel-input: The `utilsScript` option is deprecated and will be removed in a future release! Please use the `loadUtilsOnInit` option instead.");
2199
+ loadUtilsOnInit = utilsScript;
2200
+ }
2201
+ if (loadUtilsOnInit && !intlTelInput.utils) {
2202
+ this._handlePageLoad = () => {
2203
+ window.removeEventListener("load", this._handlePageLoad);
2204
+ intlTelInput.loadUtils(loadUtilsOnInit)?.catch(() => {
2205
+ });
2206
+ };
2196
2207
  if (intlTelInput.documentReady()) {
2197
- intlTelInput.loadUtils(utilsScript);
2208
+ this._handlePageLoad();
2198
2209
  } else {
2199
- window.addEventListener("load", () => {
2200
- intlTelInput.loadUtils(utilsScript);
2201
- });
2210
+ window.addEventListener("load", this._handlePageLoad);
2202
2211
  }
2203
2212
  } else {
2204
2213
  this.resolveUtilsScriptPromise();
@@ -2783,6 +2792,9 @@ var Iti = class {
2783
2792
  this.dropdown.parentNode.removeChild(this.dropdown);
2784
2793
  }
2785
2794
  }
2795
+ if (this._handlePageLoad) {
2796
+ window.removeEventListener("load", this._handlePageLoad);
2797
+ }
2786
2798
  this._trigger("close:countrydropdown");
2787
2799
  }
2788
2800
  //* Check if an element is visible within it's container, else scroll until it is.
@@ -3071,22 +3083,43 @@ var Iti = class {
3071
3083
  }
3072
3084
  }
3073
3085
  };
3074
- var loadUtils = (path) => {
3086
+ var loadUtils = (source) => {
3075
3087
  if (!intlTelInput.utils && !intlTelInput.startedLoadingUtilsScript) {
3076
- intlTelInput.startedLoadingUtilsScript = true;
3077
- return new Promise((resolve, reject) => {
3078
- import(
3088
+ let loadCall;
3089
+ if (typeof source === "string") {
3090
+ loadCall = import(
3079
3091
  /* webpackIgnore: true */
3080
3092
  /* @vite-ignore */
3081
- path
3082
- ).then(({ default: utils }) => {
3083
- intlTelInput.utils = utils;
3084
- forEachInstance("handleUtils");
3085
- resolve(true);
3086
- }).catch(() => {
3087
- forEachInstance("rejectUtilsScriptPromise");
3088
- reject();
3089
- });
3093
+ source
3094
+ );
3095
+ } else if (typeof source === "function") {
3096
+ try {
3097
+ loadCall = source();
3098
+ if (!(loadCall instanceof Promise)) {
3099
+ throw new TypeError(`The function passed to loadUtils must return a promise for the utilities module, not ${typeof loadCall}`);
3100
+ }
3101
+ } catch (error) {
3102
+ return Promise.reject(error);
3103
+ }
3104
+ } else {
3105
+ return Promise.reject(new TypeError(`The argument passed to loadUtils must be a URL string or a function that returns a promise for the utilities module, not ${typeof source}`));
3106
+ }
3107
+ intlTelInput.startedLoadingUtilsScript = true;
3108
+ return loadCall.then((module2) => {
3109
+ const utils = module2?.default;
3110
+ if (!utils || typeof utils !== "object") {
3111
+ if (typeof source === "string") {
3112
+ throw new TypeError(`The module loaded from ${source} did not set utils as its default export.`);
3113
+ } else {
3114
+ throw new TypeError("The loader function passed to loadUtils did not resolve to a module object with utils as its default export.");
3115
+ }
3116
+ }
3117
+ intlTelInput.utils = utils;
3118
+ forEachInstance("handleUtils");
3119
+ return true;
3120
+ }).catch((error) => {
3121
+ forEachInstance("rejectUtilsScriptPromise", error);
3122
+ throw error;
3090
3123
  });
3091
3124
  }
3092
3125
  return null;
@@ -3113,7 +3146,9 @@ var intlTelInput = Object.assign(
3113
3146
  //* A map from instance ID to instance object.
3114
3147
  instances: {},
3115
3148
  loadUtils,
3116
- version: "24.5.2"
3149
+ startedLoadingUtilsScript: false,
3150
+ startedLoadingAutoCountry: false,
3151
+ version: "24.6.1"
3117
3152
  }
3118
3153
  );
3119
3154
  var intl_tel_input_default = intlTelInput;
@@ -286,6 +286,9 @@ declare module "intl-tel-input/i18n/en" {
286
286
  declare module "intl-tel-input" {
287
287
  import { Country } from "intl-tel-input/data";
288
288
  import { I18n } from "intl-tel-input/i18n/types";
289
+ type UtilsLoader = () => Promise<{
290
+ default: ItiUtils;
291
+ }>;
289
292
  interface IntlTelInputInterface {
290
293
  (input: HTMLInputElement, options?: SomeOptions): Iti;
291
294
  autoCountry?: string;
@@ -296,9 +299,9 @@ declare module "intl-tel-input" {
296
299
  instances: {
297
300
  [key: string]: Iti;
298
301
  };
299
- loadUtils: (path: string) => Promise<unknown> | null;
300
- startedLoadingAutoCountry?: boolean;
301
- startedLoadingUtilsScript?: boolean;
302
+ loadUtils: (source: string | UtilsLoader) => Promise<unknown> | null;
303
+ startedLoadingAutoCountry: boolean;
304
+ startedLoadingUtilsScript: boolean;
302
305
  version: string | undefined;
303
306
  utils?: ItiUtils;
304
307
  }
@@ -345,6 +348,7 @@ declare module "intl-tel-input" {
345
348
  }) | null;
346
349
  i18n: I18n;
347
350
  initialCountry: string;
351
+ loadUtilsOnInit: string | UtilsLoader;
348
352
  nationalMode: boolean;
349
353
  onlyCountries: string[];
350
354
  placeholderNumberType: NumberType;
@@ -352,7 +356,8 @@ declare module "intl-tel-input" {
352
356
  separateDialCode: boolean;
353
357
  strictMode: boolean;
354
358
  useFullscreenPopup: boolean;
355
- utilsScript: string;
359
+ /** @deprecated Please use the `loadUtilsOnInit` option. */
360
+ utilsScript: string | UtilsLoader;
356
361
  validationNumberType: NumberType | null;
357
362
  }
358
363
  export type SomeOptions = Partial<AllOptions>;
@@ -400,6 +405,7 @@ declare module "intl-tel-input" {
400
405
  private _handleClickOffToClose;
401
406
  private _handleKeydownOnDropdown;
402
407
  private _handleSearchChange;
408
+ private _handlePageLoad;
403
409
  private resolveAutoCountryPromise;
404
410
  private rejectAutoCountryPromise;
405
411
  private resolveUtilsScriptPromise;
@@ -1623,6 +1623,8 @@ var defaults = {
1623
1623
  i18n: {},
1624
1624
  //* Initial country.
1625
1625
  initialCountry: "",
1626
+ //* Specify the path to the libphonenumber script to enable validation/formatting.
1627
+ loadUtilsOnInit: "",
1626
1628
  //* National vs international formatting for numbers e.g. placeholders and displaying existing numbers.
1627
1629
  nationalMode: true,
1628
1630
  //* Display only these countries.
@@ -1643,7 +1645,7 @@ var defaults = {
1643
1645
  navigator.userAgent
1644
1646
  ) || window.innerWidth <= 500
1645
1647
  ) : false,
1646
- //* Specify the path to the libphonenumber script to enable validation/formatting.
1648
+ //* Deprecated! Use `loadUtilsOnInit` instead.
1647
1649
  utilsScript: "",
1648
1650
  //* The number type to enforce during validation.
1649
1651
  validationNumberType: "MOBILE"
@@ -1705,9 +1707,9 @@ var createEl = (name, attrs, container) => {
1705
1707
  }
1706
1708
  return el;
1707
1709
  };
1708
- var forEachInstance = (method) => {
1710
+ var forEachInstance = (method, ...args) => {
1709
1711
  const { instances } = intlTelInput;
1710
- Object.values(instances).forEach((instance) => instance[method]());
1712
+ Object.values(instances).forEach((instance) => instance[method](...args));
1711
1713
  };
1712
1714
  var Iti = class {
1713
1715
  constructor(input, customOptions = {}) {
@@ -2155,14 +2157,21 @@ var Iti = class {
2155
2157
  }
2156
2158
  //* Init many requests: utils script / geo ip lookup.
2157
2159
  _initRequests() {
2158
- const { utilsScript, initialCountry, geoIpLookup } = this.options;
2159
- if (utilsScript && !intlTelInput.utils) {
2160
+ let { loadUtilsOnInit, utilsScript, initialCountry, geoIpLookup } = this.options;
2161
+ if (!loadUtilsOnInit && utilsScript) {
2162
+ console.warn("intl-tel-input: The `utilsScript` option is deprecated and will be removed in a future release! Please use the `loadUtilsOnInit` option instead.");
2163
+ loadUtilsOnInit = utilsScript;
2164
+ }
2165
+ if (loadUtilsOnInit && !intlTelInput.utils) {
2166
+ this._handlePageLoad = () => {
2167
+ window.removeEventListener("load", this._handlePageLoad);
2168
+ intlTelInput.loadUtils(loadUtilsOnInit)?.catch(() => {
2169
+ });
2170
+ };
2160
2171
  if (intlTelInput.documentReady()) {
2161
- intlTelInput.loadUtils(utilsScript);
2172
+ this._handlePageLoad();
2162
2173
  } else {
2163
- window.addEventListener("load", () => {
2164
- intlTelInput.loadUtils(utilsScript);
2165
- });
2174
+ window.addEventListener("load", this._handlePageLoad);
2166
2175
  }
2167
2176
  } else {
2168
2177
  this.resolveUtilsScriptPromise();
@@ -2747,6 +2756,9 @@ var Iti = class {
2747
2756
  this.dropdown.parentNode.removeChild(this.dropdown);
2748
2757
  }
2749
2758
  }
2759
+ if (this._handlePageLoad) {
2760
+ window.removeEventListener("load", this._handlePageLoad);
2761
+ }
2750
2762
  this._trigger("close:countrydropdown");
2751
2763
  }
2752
2764
  //* Check if an element is visible within it's container, else scroll until it is.
@@ -3035,22 +3047,43 @@ var Iti = class {
3035
3047
  }
3036
3048
  }
3037
3049
  };
3038
- var loadUtils = (path) => {
3050
+ var loadUtils = (source) => {
3039
3051
  if (!intlTelInput.utils && !intlTelInput.startedLoadingUtilsScript) {
3040
- intlTelInput.startedLoadingUtilsScript = true;
3041
- return new Promise((resolve, reject) => {
3042
- import(
3052
+ let loadCall;
3053
+ if (typeof source === "string") {
3054
+ loadCall = import(
3043
3055
  /* webpackIgnore: true */
3044
3056
  /* @vite-ignore */
3045
- path
3046
- ).then(({ default: utils }) => {
3047
- intlTelInput.utils = utils;
3048
- forEachInstance("handleUtils");
3049
- resolve(true);
3050
- }).catch(() => {
3051
- forEachInstance("rejectUtilsScriptPromise");
3052
- reject();
3053
- });
3057
+ source
3058
+ );
3059
+ } else if (typeof source === "function") {
3060
+ try {
3061
+ loadCall = source();
3062
+ if (!(loadCall instanceof Promise)) {
3063
+ throw new TypeError(`The function passed to loadUtils must return a promise for the utilities module, not ${typeof loadCall}`);
3064
+ }
3065
+ } catch (error) {
3066
+ return Promise.reject(error);
3067
+ }
3068
+ } else {
3069
+ return Promise.reject(new TypeError(`The argument passed to loadUtils must be a URL string or a function that returns a promise for the utilities module, not ${typeof source}`));
3070
+ }
3071
+ intlTelInput.startedLoadingUtilsScript = true;
3072
+ return loadCall.then((module) => {
3073
+ const utils = module?.default;
3074
+ if (!utils || typeof utils !== "object") {
3075
+ if (typeof source === "string") {
3076
+ throw new TypeError(`The module loaded from ${source} did not set utils as its default export.`);
3077
+ } else {
3078
+ throw new TypeError("The loader function passed to loadUtils did not resolve to a module object with utils as its default export.");
3079
+ }
3080
+ }
3081
+ intlTelInput.utils = utils;
3082
+ forEachInstance("handleUtils");
3083
+ return true;
3084
+ }).catch((error) => {
3085
+ forEachInstance("rejectUtilsScriptPromise", error);
3086
+ throw error;
3054
3087
  });
3055
3088
  }
3056
3089
  return null;
@@ -3077,7 +3110,9 @@ var intlTelInput = Object.assign(
3077
3110
  //* A map from instance ID to instance object.
3078
3111
  instances: {},
3079
3112
  loadUtils,
3080
- version: "24.5.2"
3113
+ startedLoadingUtilsScript: false,
3114
+ startedLoadingAutoCountry: false,
3115
+ version: "24.6.1"
3081
3116
  }
3082
3117
  );
3083
3118
  var intl_tel_input_default = intlTelInput;