intl-tel-input 20.2.0 → 20.3.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.
@@ -1,9 +1,8 @@
1
1
  /*
2
- * International Telephone Input v20.2.0
2
+ * International Telephone Input v20.3.0
3
3
  * https://github.com/jackocnr/intl-tel-input.git
4
4
  * Licensed under the MIT license
5
5
  */
6
-
7
6
  // wrap in UMD
8
7
  (function(factory) {
9
8
  if (typeof module === "object" && module.exports) module.exports = factory(); else window.intlTelInput = factory();
@@ -37,138 +36,21 @@
37
36
  nodeById: {}
38
37
  };
39
38
  }
40
- "use strict";
41
- function ownKeys(object, enumerableOnly) {
42
- var keys = Object.keys(object);
43
- if (Object.getOwnPropertySymbols) {
44
- var symbols = Object.getOwnPropertySymbols(object);
45
- enumerableOnly && (symbols = symbols.filter(function(sym) {
46
- return Object.getOwnPropertyDescriptor(object, sym).enumerable;
47
- })), keys.push.apply(keys, symbols);
48
- }
49
- return keys;
50
- }
51
- function _objectSpread(target) {
52
- for (var i = 1; i < arguments.length; i++) {
53
- var source = null != arguments[i] ? arguments[i] : {};
54
- i % 2 ? ownKeys(Object(source), !0).forEach(function(key) {
55
- _defineProperty(target, key, source[key]);
56
- }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function(key) {
57
- Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
58
- });
59
- }
60
- return target;
61
- }
62
- function _defineProperty(obj, key, value) {
63
- key = _toPropertyKey(key);
64
- if (key in obj) {
65
- Object.defineProperty(obj, key, {
66
- value: value,
67
- enumerable: true,
68
- configurable: true,
69
- writable: true
70
- });
71
- } else {
72
- obj[key] = value;
73
- }
74
- return obj;
75
- }
76
- function _slicedToArray(arr, i) {
77
- return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest();
78
- }
79
- function _nonIterableRest() {
80
- throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
81
- }
82
- function _unsupportedIterableToArray(o, minLen) {
83
- if (!o) return;
84
- if (typeof o === "string") return _arrayLikeToArray(o, minLen);
85
- var n = Object.prototype.toString.call(o).slice(8, -1);
86
- if (n === "Object" && o.constructor) n = o.constructor.name;
87
- if (n === "Map" || n === "Set") return Array.from(o);
88
- if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
89
- }
90
- function _arrayLikeToArray(arr, len) {
91
- if (len == null || len > arr.length) len = arr.length;
92
- for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i];
93
- return arr2;
94
- }
95
- function _iterableToArrayLimit(arr, i) {
96
- var _i = null == arr ? null : "undefined" != typeof Symbol && arr[Symbol.iterator] || arr["@@iterator"];
97
- if (null != _i) {
98
- var _s, _e, _x, _r, _arr = [], _n = !0, _d = !1;
99
- try {
100
- if (_x = (_i = _i.call(arr)).next, 0 === i) {
101
- if (Object(_i) !== _i) return;
102
- _n = !1;
103
- } else for (;!(_n = (_s = _x.call(_i)).done) && (_arr.push(_s.value), _arr.length !== i); _n = !0) ;
104
- } catch (err) {
105
- _d = !0, _e = err;
106
- } finally {
107
- try {
108
- if (!_n && null != _i["return"] && (_r = _i["return"](), Object(_r) !== _r)) return;
109
- } finally {
110
- if (_d) throw _e;
111
- }
112
- }
113
- return _arr;
114
- }
115
- }
116
- function _arrayWithHoles(arr) {
117
- if (Array.isArray(arr)) return arr;
118
- }
119
- function _classCallCheck(instance, Constructor) {
120
- if (!(instance instanceof Constructor)) {
121
- throw new TypeError("Cannot call a class as a function");
122
- }
123
- }
124
- function _defineProperties(target, props) {
125
- for (var i = 0; i < props.length; i++) {
126
- var descriptor = props[i];
127
- descriptor.enumerable = descriptor.enumerable || false;
128
- descriptor.configurable = true;
129
- if ("value" in descriptor) descriptor.writable = true;
130
- Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor);
131
- }
132
- }
133
- function _createClass(Constructor, protoProps, staticProps) {
134
- if (protoProps) _defineProperties(Constructor.prototype, protoProps);
135
- if (staticProps) _defineProperties(Constructor, staticProps);
136
- Object.defineProperty(Constructor, "prototype", {
137
- writable: false
138
- });
139
- return Constructor;
140
- }
141
- function _toPropertyKey(arg) {
142
- var key = _toPrimitive(arg, "string");
143
- return typeof key === "symbol" ? key : String(key);
144
- }
145
- function _toPrimitive(input, hint) {
146
- if (typeof input !== "object" || input === null) return input;
147
- var prim = input[Symbol.toPrimitive];
148
- if (prim !== undefined) {
149
- var res = prim.call(input, hint || "default");
150
- if (typeof res !== "object") return res;
151
- throw new TypeError("@@toPrimitive must return a primitive value.");
152
- }
153
- return (hint === "string" ? String : Number)(input);
154
- }
155
- var intlTelInputGlobals = {
156
- getInstance: function getInstance(input) {
157
- var id = input.getAttribute("data-intl-tel-input-id");
39
+ const intlTelInputGlobals = {
40
+ getInstance: input => {
41
+ const id = input.getAttribute("data-intl-tel-input-id");
158
42
  return window.intlTelInputGlobals.instances[id];
159
43
  },
160
44
  instances: {},
161
45
  // using a global like this allows us to mock it in the tests
162
- documentReady: function documentReady() {
163
- return document.readyState === "complete";
164
- }
46
+ documentReady: () => document.readyState === "complete"
165
47
  };
166
48
  if (typeof window === "object") {
167
49
  window.intlTelInputGlobals = intlTelInputGlobals;
168
50
  }
169
51
  // these vars persist through all instances of the plugin
170
- var id = 0;
171
- var defaults = {
52
+ let id = 0;
53
+ const defaults = {
172
54
  // whether or not to allow the dropdown
173
55
  allowDropdown: true,
174
56
  // add a placeholder in the input with an example number for the selected country
@@ -220,20 +102,18 @@
220
102
  utilsScript: ""
221
103
  };
222
104
  // https://en.wikipedia.org/wiki/List_of_North_American_Numbering_Plan_area_codes#Non-geographic_area_codes
223
- var regionlessNanpNumbers = [ "800", "822", "833", "844", "855", "866", "877", "880", "881", "882", "883", "884", "885", "886", "887", "888", "889" ];
105
+ const regionlessNanpNumbers = [ "800", "822", "833", "844", "855", "866", "877", "880", "881", "882", "883", "884", "885", "886", "887", "888", "889" ];
224
106
  // run a method on each instance of the plugin
225
- var forEachInstance = function forEachInstance(method) {
226
- var instances = window.intlTelInputGlobals.instances;
227
- Object.values(instances).forEach(function(instance) {
228
- return instance[method]();
229
- });
107
+ const forEachInstance = method => {
108
+ const {
109
+ instances
110
+ } = window.intlTelInputGlobals;
111
+ Object.values(instances).forEach(instance => instance[method]());
230
112
  };
231
113
  // this is our plugin class that we will create an instance of
232
114
  // eslint-disable-next-line no-unused-vars
233
- var Iti = /*#__PURE__*/ function() {
234
- function Iti(input) {
235
- var customOptions = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
236
- _classCallCheck(this, Iti);
115
+ class Iti {
116
+ constructor(input, customOptions = {}) {
237
117
  this.id = id++;
238
118
  this.telInput = input;
239
119
  this.activeItem = null;
@@ -242,1558 +122,1536 @@
242
122
  this.options = Object.assign({}, defaults, customOptions);
243
123
  this.hadInitialPlaceholder = Boolean(input.getAttribute("placeholder"));
244
124
  }
245
- _createClass(Iti, [ {
246
- key: "_init",
247
- value: function _init() {
248
- var _this = this;
249
- // if showing fullscreen popup, do not fix the width
250
- if (this.options.useFullscreenPopup) {
251
- this.options.fixDropdownWidth = false;
252
- }
253
- // when search enabled, we must fix the width else it would change with different results
254
- if (this.options.countrySearch && !this.options.useFullscreenPopup) {
255
- this.options.fixDropdownWidth = true;
256
- }
257
- // force showFlags=true if there's a dropdown and we're not displaying the dial code,
258
- // as otherwise you just have a down arrow on it's own which doesn't make sense
259
- var forceShowFlags = this.options.allowDropdown && !this.options.showSelectedDialCode;
260
- if (!this.options.showFlags && forceShowFlags) {
261
- this.options.showFlags = true;
262
- }
263
- // on mobile, we want a full screen dropdown, so we must append it to the body
264
- if (this.options.useFullscreenPopup && !this.options.dropdownContainer) {
265
- this.options.dropdownContainer = document.body;
266
- }
267
- // check if input has one parent with RTL
268
- this.isRTL = !!this.telInput.closest("[dir=rtl]");
269
- // these promises get resolved when their individual requests complete
270
- // this way the dev can do something like iti.promise.then(...) to know when all requests are
271
- // complete
272
- var autoCountryPromise = new Promise(function(resolve, reject) {
273
- _this.resolveAutoCountryPromise = resolve;
274
- _this.rejectAutoCountryPromise = reject;
275
- });
276
- var utilsScriptPromise = new Promise(function(resolve, reject) {
277
- _this.resolveUtilsScriptPromise = resolve;
278
- _this.rejectUtilsScriptPromise = reject;
279
- });
280
- this.promise = Promise.all([ autoCountryPromise, utilsScriptPromise ]);
281
- // in various situations there could be no country selected initially, but we need to be able
282
- // to assume this variable exists
283
- this.selectedCountryData = {};
284
- // process all the data: onlyCountries, excludeCountries, preferredCountries etc
285
- this._processCountryData();
286
- // generate the markup
287
- this._generateMarkup();
288
- // set the initial state of the input value and the selected flag
289
- this._setInitialState();
290
- // start all of the event listeners: input keydown, selectedFlag click
291
- this._initListeners();
292
- // utils script, and auto country
293
- this._initRequests();
294
- }
295
- }, {
296
- key: "_processCountryData",
297
- value: function _processCountryData() {
298
- // process onlyCountries or excludeCountries array if present
299
- this._processAllCountries();
300
- // generate this.dialCodes and this.dialCodeToIso2Map
301
- this._processDialCodes();
302
- // process the preferredCountries
303
- this._processPreferredCountries();
304
- // translate country names according to i18n option
305
- this._translateCountryNames();
306
- // sort countries by name
307
- if (this.options.onlyCountries.length || this.options.i18n) {
308
- this.countries.sort(this._countryNameSort);
309
- }
125
+ _init() {
126
+ // if showing fullscreen popup, do not fix the width
127
+ if (this.options.useFullscreenPopup) {
128
+ this.options.fixDropdownWidth = false;
129
+ }
130
+ // when search enabled, we must fix the width else it would change with different results
131
+ if (this.options.countrySearch && !this.options.useFullscreenPopup) {
132
+ this.options.fixDropdownWidth = true;
133
+ }
134
+ // force showFlags=true if there's a dropdown and we're not displaying the dial code,
135
+ // as otherwise you just have a down arrow on it's own which doesn't make sense
136
+ const forceShowFlags = this.options.allowDropdown && !this.options.showSelectedDialCode;
137
+ if (!this.options.showFlags && forceShowFlags) {
138
+ this.options.showFlags = true;
139
+ }
140
+ // on mobile, we want a full screen dropdown, so we must append it to the body
141
+ if (this.options.useFullscreenPopup && !this.options.dropdownContainer) {
142
+ this.options.dropdownContainer = document.body;
143
+ }
144
+ // check if input has one parent with RTL
145
+ this.isRTL = !!this.telInput.closest("[dir=rtl]");
146
+ // these promises get resolved when their individual requests complete
147
+ // this way the dev can do something like iti.promise.then(...) to know when all requests are
148
+ // complete
149
+ const autoCountryPromise = new Promise((resolve, reject) => {
150
+ this.resolveAutoCountryPromise = resolve;
151
+ this.rejectAutoCountryPromise = reject;
152
+ });
153
+ const utilsScriptPromise = new Promise((resolve, reject) => {
154
+ this.resolveUtilsScriptPromise = resolve;
155
+ this.rejectUtilsScriptPromise = reject;
156
+ });
157
+ this.promise = Promise.all([ autoCountryPromise, utilsScriptPromise ]);
158
+ // in various situations there could be no country selected initially, but we need to be able
159
+ // to assume this variable exists
160
+ this.selectedCountryData = {};
161
+ // process all the data: onlyCountries, excludeCountries, preferredCountries etc
162
+ this._processCountryData();
163
+ // generate the markup
164
+ this._generateMarkup();
165
+ // set the initial state of the input value and the selected flag
166
+ this._setInitialState();
167
+ // start all of the event listeners: input keydown, selectedFlag click
168
+ this._initListeners();
169
+ // utils script, and auto country
170
+ this._initRequests();
171
+ }
172
+ /********************
173
+ * PRIVATE METHODS
174
+ ********************/
175
+ // prepare all of the country data, including onlyCountries, excludeCountries and
176
+ // preferredCountries options
177
+ _processCountryData() {
178
+ // process onlyCountries or excludeCountries array if present
179
+ this._processAllCountries();
180
+ // generate this.dialCodes and this.dialCodeToIso2Map
181
+ this._processDialCodes();
182
+ // process the preferredCountries
183
+ this._processPreferredCountries();
184
+ // translate country names according to i18n option
185
+ this._translateCountryNames();
186
+ // sort countries by name
187
+ if (this.options.onlyCountries.length || this.options.i18n) {
188
+ this.countries.sort(this._countryNameSort);
310
189
  }
311
- }, {
312
- key: "_addToDialCodeMap",
313
- value: function _addToDialCodeMap(iso2, dialCode, priority) {
314
- if (dialCode.length > this.dialCodeMaxLen) {
315
- this.dialCodeMaxLen = dialCode.length;
316
- }
317
- if (!this.dialCodeToIso2Map.hasOwnProperty(dialCode)) {
318
- this.dialCodeToIso2Map[dialCode] = [];
319
- }
320
- // bail if we already have this country for this dialCode
321
- for (var i = 0; i < this.dialCodeToIso2Map[dialCode].length; i++) {
322
- if (this.dialCodeToIso2Map[dialCode][i] === iso2) {
323
- return;
324
- }
325
- }
326
- // check for undefined as 0 is falsy
327
- var index = priority !== undefined ? priority : this.dialCodeToIso2Map[dialCode].length;
328
- this.dialCodeToIso2Map[dialCode][index] = iso2;
329
- }
330
- }, {
331
- key: "_processAllCountries",
332
- value: function _processAllCountries() {
333
- if (this.options.onlyCountries.length) {
334
- var lowerCaseOnlyCountries = this.options.onlyCountries.map(function(country) {
335
- return country.toLowerCase();
336
- });
337
- this.countries = allCountries.filter(function(country) {
338
- return lowerCaseOnlyCountries.indexOf(country.iso2) > -1;
339
- });
340
- } else if (this.options.excludeCountries.length) {
341
- var lowerCaseExcludeCountries = this.options.excludeCountries.map(function(country) {
342
- return country.toLowerCase();
343
- });
344
- this.countries = allCountries.filter(function(country) {
345
- return lowerCaseExcludeCountries.indexOf(country.iso2) === -1;
346
- });
347
- } else {
348
- this.countries = allCountries;
349
- }
190
+ }
191
+ // add a dial code to this.dialCodeToIso2Map
192
+ _addToDialCodeMap(iso2, dialCode, priority) {
193
+ if (dialCode.length > this.dialCodeMaxLen) {
194
+ this.dialCodeMaxLen = dialCode.length;
350
195
  }
351
- }, {
352
- key: "_translateCountryNames",
353
- value: function _translateCountryNames() {
354
- for (var i = 0; i < this.countries.length; i++) {
355
- var iso2 = this.countries[i].iso2.toLowerCase();
356
- if (this.options.i18n.hasOwnProperty(iso2)) {
357
- this.countries[i].name = this.options.i18n[iso2];
358
- }
359
- }
196
+ if (!this.dialCodeToIso2Map.hasOwnProperty(dialCode)) {
197
+ this.dialCodeToIso2Map[dialCode] = [];
360
198
  }
361
- }, {
362
- key: "_countryNameSort",
363
- value: function _countryNameSort(a, b) {
364
- if (a.name < b.name) {
365
- return -1;
366
- }
367
- if (a.name > b.name) {
368
- return 1;
199
+ // bail if we already have this country for this dialCode
200
+ for (let i = 0; i < this.dialCodeToIso2Map[dialCode].length; i++) {
201
+ if (this.dialCodeToIso2Map[dialCode][i] === iso2) {
202
+ return;
369
203
  }
370
- return 0;
371
204
  }
372
- }, {
373
- key: "_processDialCodes",
374
- value: function _processDialCodes() {
375
- // here we store just dial codes, where the key is the dial code, and the value is true
376
- // e.g. { 1: true, 7: true, 20: true, ... }
377
- this.dialCodes = {};
378
- this.dialCodeMaxLen = 0;
379
- // here we map dialCodes (inc both dialCode and dialCode+areaCode) to iso2 codes
380
- /* e.g.
381
- * {
382
- * 1: [ 'us', 'ca', ... ], # all NANP countries
383
- * 12: [ 'us', 'ca', ... ], # subset of NANP countries
384
- * 120: [ 'us', 'ca' ], # just US and Canada
385
- * 1204: [ 'ca' ], # only Canada
386
- * ...
387
- * }
388
- */
389
- this.dialCodeToIso2Map = {};
390
- // first: add dial codes
391
- for (var i = 0; i < this.countries.length; i++) {
392
- var c = this.countries[i];
393
- if (!this.dialCodes[c.dialCode]) {
394
- this.dialCodes[c.dialCode] = true;
395
- }
396
- this._addToDialCodeMap(c.iso2, c.dialCode, c.priority);
205
+ // check for undefined as 0 is falsy
206
+ const index = priority !== undefined ? priority : this.dialCodeToIso2Map[dialCode].length;
207
+ this.dialCodeToIso2Map[dialCode][index] = iso2;
208
+ }
209
+ // process onlyCountries or excludeCountries array if present
210
+ _processAllCountries() {
211
+ if (this.options.onlyCountries.length) {
212
+ const lowerCaseOnlyCountries = this.options.onlyCountries.map(country => country.toLowerCase());
213
+ this.countries = allCountries.filter(country => lowerCaseOnlyCountries.indexOf(country.iso2) > -1);
214
+ } else if (this.options.excludeCountries.length) {
215
+ const lowerCaseExcludeCountries = this.options.excludeCountries.map(country => country.toLowerCase());
216
+ this.countries = allCountries.filter(country => lowerCaseExcludeCountries.indexOf(country.iso2) === -1);
217
+ } else {
218
+ this.countries = allCountries;
219
+ }
220
+ }
221
+ // Translate Countries by object literal provided on config
222
+ _translateCountryNames() {
223
+ for (let i = 0; i < this.countries.length; i++) {
224
+ const iso2 = this.countries[i].iso2.toLowerCase();
225
+ if (this.options.i18n.hasOwnProperty(iso2)) {
226
+ this.countries[i].name = this.options.i18n[iso2];
397
227
  }
398
- // next: add area codes
399
- // this is a second loop over countries, to make sure we have all of the "root" countries
400
- // already in the map, so that we can access them, as each time we add an area code substring
401
- // to the map, we also need to include the "root" country's code, as that also matches
402
- for (var _i = 0; _i < this.countries.length; _i++) {
403
- var _c = this.countries[_i];
404
- // area codes
405
- if (_c.areaCodes) {
406
- var rootIso2Code = this.dialCodeToIso2Map[_c.dialCode][0];
407
- // for each area code
408
- for (var j = 0; j < _c.areaCodes.length; j++) {
409
- var areaCode = _c.areaCodes[j];
410
- // for each digit in the area code to add all partial matches as well
411
- for (var k = 1; k < areaCode.length; k++) {
412
- var partialDialCode = _c.dialCode + areaCode.substr(0, k);
413
- // start with the root country, as that also matches this dial code
414
- this._addToDialCodeMap(rootIso2Code, partialDialCode);
415
- this._addToDialCodeMap(_c.iso2, partialDialCode);
416
- }
417
- // add the full area code
418
- this._addToDialCodeMap(_c.iso2, _c.dialCode + areaCode);
228
+ }
229
+ }
230
+ // sort by country name
231
+ _countryNameSort(a, b) {
232
+ if (a.name < b.name) {
233
+ return -1;
234
+ }
235
+ if (a.name > b.name) {
236
+ return 1;
237
+ }
238
+ return 0;
239
+ }
240
+ // generate this.dialCodes and this.dialCodeToIso2Map
241
+ _processDialCodes() {
242
+ // here we store just dial codes, where the key is the dial code, and the value is true
243
+ // e.g. { 1: true, 7: true, 20: true, ... }
244
+ this.dialCodes = {};
245
+ this.dialCodeMaxLen = 0;
246
+ // here we map dialCodes (inc both dialCode and dialCode+areaCode) to iso2 codes
247
+ /* e.g.
248
+ * {
249
+ * 1: [ 'us', 'ca', ... ], # all NANP countries
250
+ * 12: [ 'us', 'ca', ... ], # subset of NANP countries
251
+ * 120: [ 'us', 'ca' ], # just US and Canada
252
+ * 1204: [ 'ca' ], # only Canada
253
+ * ...
254
+ * }
255
+ */
256
+ this.dialCodeToIso2Map = {};
257
+ // first: add dial codes
258
+ for (let i = 0; i < this.countries.length; i++) {
259
+ const c = this.countries[i];
260
+ if (!this.dialCodes[c.dialCode]) {
261
+ this.dialCodes[c.dialCode] = true;
262
+ }
263
+ this._addToDialCodeMap(c.iso2, c.dialCode, c.priority);
264
+ }
265
+ // next: add area codes
266
+ // this is a second loop over countries, to make sure we have all of the "root" countries
267
+ // already in the map, so that we can access them, as each time we add an area code substring
268
+ // to the map, we also need to include the "root" country's code, as that also matches
269
+ for (let i = 0; i < this.countries.length; i++) {
270
+ const c = this.countries[i];
271
+ // area codes
272
+ if (c.areaCodes) {
273
+ const rootIso2Code = this.dialCodeToIso2Map[c.dialCode][0];
274
+ // for each area code
275
+ for (let j = 0; j < c.areaCodes.length; j++) {
276
+ const areaCode = c.areaCodes[j];
277
+ // for each digit in the area code to add all partial matches as well
278
+ for (let k = 1; k < areaCode.length; k++) {
279
+ const partialDialCode = c.dialCode + areaCode.substr(0, k);
280
+ // start with the root country, as that also matches this dial code
281
+ this._addToDialCodeMap(rootIso2Code, partialDialCode);
282
+ this._addToDialCodeMap(c.iso2, partialDialCode);
419
283
  }
284
+ // add the full area code
285
+ this._addToDialCodeMap(c.iso2, c.dialCode + areaCode);
420
286
  }
421
287
  }
422
288
  }
423
- }, {
424
- key: "_processPreferredCountries",
425
- value: function _processPreferredCountries() {
426
- this.preferredCountries = [];
427
- for (var i = 0; i < this.options.preferredCountries.length; i++) {
428
- var iso2 = this.options.preferredCountries[i].toLowerCase();
429
- var countryData = this._getCountryData(iso2, true);
430
- if (countryData) {
431
- this.preferredCountries.push(countryData);
432
- }
289
+ }
290
+ // process preferred countries - iterate through the preferences, fetching the country data for
291
+ // each one
292
+ _processPreferredCountries() {
293
+ this.preferredCountries = [];
294
+ for (let i = 0; i < this.options.preferredCountries.length; i++) {
295
+ const iso2 = this.options.preferredCountries[i].toLowerCase();
296
+ const countryData = this._getCountryData(iso2, true);
297
+ if (countryData) {
298
+ this.preferredCountries.push(countryData);
433
299
  }
434
300
  }
435
- }, {
436
- key: "_createEl",
437
- value: function _createEl(name, attrs, container) {
438
- var el = document.createElement(name);
439
- if (attrs) {
440
- Object.entries(attrs).forEach(function(_ref) {
441
- var _ref2 = _slicedToArray(_ref, 2), key = _ref2[0], value = _ref2[1];
442
- return el.setAttribute(key, value);
443
- });
444
- }
445
- if (container) {
446
- container.appendChild(el);
447
- }
448
- return el;
449
- }
450
- }, {
451
- key: "_generateMarkup",
452
- value: function _generateMarkup() {
453
- this.telInput.classList.add("iti__tel-input");
454
- // if autocomplete does not exist on the element and its form, then
455
- // prevent autocomplete as there's no safe, cross-browser event we can react to, so it can
456
- // easily put the plugin in an inconsistent state e.g. the wrong flag selected for the
457
- // autocompleted number, which on submit could mean wrong number is saved
458
- if (!this.telInput.hasAttribute("autocomplete") && !(this.telInput.form && this.telInput.form.hasAttribute("autocomplete"))) {
459
- this.telInput.setAttribute("autocomplete", "off");
460
- }
461
- var _this$options = this.options, allowDropdown = _this$options.allowDropdown, showSelectedDialCode = _this$options.showSelectedDialCode, showFlags = _this$options.showFlags, containerClass = _this$options.containerClass, hiddenInput = _this$options.hiddenInput, dropdownContainer = _this$options.dropdownContainer, fixDropdownWidth = _this$options.fixDropdownWidth, useFullscreenPopup = _this$options.useFullscreenPopup, countrySearch = _this$options.countrySearch, i18n = _this$options.i18n;
462
- // containers (mostly for positioning)
463
- var parentClass = "iti";
464
- if (allowDropdown) {
465
- parentClass += " iti--allow-dropdown";
466
- }
467
- if (showSelectedDialCode) {
468
- parentClass += " iti--show-selected-dial-code";
469
- }
470
- if (showFlags) {
471
- parentClass += " iti--show-flags";
472
- }
473
- if (containerClass) {
474
- parentClass += " ".concat(containerClass);
475
- }
476
- if (!useFullscreenPopup) {
477
- parentClass += " iti--inline-dropdown";
478
- }
479
- var wrapper = this._createEl("div", {
480
- "class": parentClass
481
- });
482
- this.telInput.parentNode.insertBefore(wrapper, this.telInput);
483
- // if we're showing flags or dial codes, we need the flags container etc
484
- if (showFlags || showSelectedDialCode) {
485
- this.flagsContainer = this._createEl("div", {
486
- "class": "iti__flag-container"
487
- }, wrapper);
488
- // selected flag (displayed on left of input while allowDropdown is enabled, otherwise to right)
489
- // when countrySearch disabled: using Aria tags for "Select-Only Combobox Example"
490
- // https://www.w3.org/WAI/ARIA/apg/patterns/combobox/examples/combobox-select-only/
491
- this.selectedFlag = this._createEl("div", _objectSpread({
492
- "class": "iti__selected-flag"
493
- }, allowDropdown && _objectSpread({
301
+ }
302
+ // create a DOM element
303
+ _createEl(name, attrs, container) {
304
+ const el = document.createElement(name);
305
+ if (attrs) {
306
+ Object.entries(attrs).forEach(([ key, value ]) => el.setAttribute(key, value));
307
+ }
308
+ if (container) {
309
+ container.appendChild(el);
310
+ }
311
+ return el;
312
+ }
313
+ // generate all of the markup for the plugin: the selected flag overlay, and the dropdown
314
+ _generateMarkup() {
315
+ this.telInput.classList.add("iti__tel-input");
316
+ // if autocomplete does not exist on the element and its form, then
317
+ // prevent autocomplete as there's no safe, cross-browser event we can react to, so it can
318
+ // easily put the plugin in an inconsistent state e.g. the wrong flag selected for the
319
+ // autocompleted number, which on submit could mean wrong number is saved
320
+ if (!this.telInput.hasAttribute("autocomplete") && !(this.telInput.form && this.telInput.form.hasAttribute("autocomplete"))) {
321
+ this.telInput.setAttribute("autocomplete", "off");
322
+ }
323
+ const {
324
+ allowDropdown,
325
+ showSelectedDialCode,
326
+ showFlags,
327
+ containerClass,
328
+ hiddenInput,
329
+ dropdownContainer,
330
+ fixDropdownWidth,
331
+ useFullscreenPopup,
332
+ countrySearch,
333
+ i18n
334
+ } = this.options;
335
+ // containers (mostly for positioning)
336
+ let parentClass = "iti";
337
+ if (allowDropdown) {
338
+ parentClass += " iti--allow-dropdown";
339
+ }
340
+ if (showSelectedDialCode) {
341
+ parentClass += " iti--show-selected-dial-code";
342
+ }
343
+ if (showFlags) {
344
+ parentClass += " iti--show-flags";
345
+ }
346
+ if (containerClass) {
347
+ parentClass += ` ${containerClass}`;
348
+ }
349
+ if (!useFullscreenPopup) {
350
+ parentClass += " iti--inline-dropdown";
351
+ }
352
+ const wrapper = this._createEl("div", {
353
+ class: parentClass
354
+ });
355
+ this.telInput.parentNode.insertBefore(wrapper, this.telInput);
356
+ // if we're showing flags or dial codes, we need the flags container etc
357
+ if (showFlags || showSelectedDialCode) {
358
+ this.flagsContainer = this._createEl("div", {
359
+ class: "iti__flag-container"
360
+ }, wrapper);
361
+ // selected flag (displayed on left of input while allowDropdown is enabled, otherwise to right)
362
+ // when countrySearch disabled: using Aria tags for "Select-Only Combobox Example"
363
+ // https://www.w3.org/WAI/ARIA/apg/patterns/combobox/examples/combobox-select-only/
364
+ this.selectedFlag = this._createEl("div", {
365
+ class: "iti__selected-flag",
366
+ ...allowDropdown && {
494
367
  role: "button",
495
368
  "aria-expanded": "false",
496
369
  "aria-label": this.options.i18n.selectedCountryAriaLabel || "Selected country",
497
370
  "aria-haspopup": countrySearch ? "true" : "listbox",
498
- "aria-controls": countrySearch ? "iti-".concat(this.id, "__dropdown-content") : "iti-".concat(this.id, "__country-listbox")
499
- }, countrySearch || {
500
- role: "combobox"
501
- })), this.flagsContainer);
502
- // we now include the selected flag element even when showFlags is disabled,
503
- // as need to show globe icon for showSelectedDialCode empty state
504
- this.selectedFlagInner = this._createEl("div", null, this.selectedFlag);
505
- this.selectedFlagA11yText = this._createEl("span", {
506
- "class": "iti__a11y-text"
507
- }, this.selectedFlagInner);
371
+ "aria-controls": countrySearch ? `iti-${this.id}__dropdown-content` : `iti-${this.id}__country-listbox`,
372
+ ...countrySearch || {
373
+ role: "combobox"
374
+ }
375
+ }
376
+ }, this.flagsContainer);
377
+ // we now include the selected flag element even when showFlags is disabled,
378
+ // as need to show globe icon for showSelectedDialCode empty state
379
+ this.selectedFlagInner = this._createEl("div", null, this.selectedFlag);
380
+ this.selectedFlagA11yText = this._createEl("span", {
381
+ class: "iti__a11y-text"
382
+ }, this.selectedFlagInner);
383
+ }
384
+ wrapper.appendChild(this.telInput);
385
+ if (this.selectedFlag && this.telInput.disabled) {
386
+ this.selectedFlag.setAttribute("aria-disabled", "true");
387
+ }
388
+ if (showSelectedDialCode) {
389
+ this.selectedDialCode = this._createEl("div", {
390
+ class: "iti__selected-dial-code"
391
+ }, this.selectedFlag);
392
+ }
393
+ if (allowDropdown) {
394
+ if (!this.telInput.disabled) {
395
+ // make element focusable and tab navigable
396
+ this.selectedFlag.setAttribute("tabindex", "0");
397
+ }
398
+ this.dropdownArrow = this._createEl("div", {
399
+ class: "iti__arrow",
400
+ "aria-hidden": "true"
401
+ }, this.selectedFlag);
402
+ const extraClasses = fixDropdownWidth ? "" : "iti--flexible-dropdown-width";
403
+ this.dropdownContent = this._createEl("div", {
404
+ id: `iti-${this.id}__dropdown-content`,
405
+ class: `iti__dropdown-content iti__hide ${extraClasses}`
406
+ });
407
+ if (countrySearch) {
408
+ this.searchInput = this._createEl("input", {
409
+ type: "text",
410
+ class: "iti__search-input",
411
+ placeholder: i18n.searchPlaceholder || "Search",
412
+ role: "combobox",
413
+ "aria-expanded": "true",
414
+ "aria-label": i18n.searchPlaceholder || "Search",
415
+ "aria-controls": `iti-${this.id}__country-listbox`,
416
+ "aria-autocomplete": "list",
417
+ autocomplete: "off"
418
+ }, this.dropdownContent);
419
+ this.searchResultsA11yText = this._createEl("span", {
420
+ class: "iti__a11y-text"
421
+ }, this.dropdownContent);
508
422
  }
509
- wrapper.appendChild(this.telInput);
510
- if (this.selectedFlag && this.telInput.disabled) {
511
- this.selectedFlag.setAttribute("aria-disabled", "true");
423
+ // country list: preferred countries, then divider, then all countries
424
+ this.countryList = this._createEl("ul", {
425
+ class: "iti__country-list",
426
+ id: `iti-${this.id}__country-listbox`,
427
+ role: "listbox",
428
+ "aria-label": i18n.countryListAriaLabel || "List of countries"
429
+ }, this.dropdownContent);
430
+ if (this.preferredCountries.length && !countrySearch) {
431
+ this._appendListItems(this.preferredCountries, "iti__preferred", true);
432
+ this._createEl("li", {
433
+ class: "iti__divider",
434
+ "aria-hidden": "true"
435
+ }, this.countryList);
512
436
  }
513
- if (showSelectedDialCode) {
514
- this.selectedDialCode = this._createEl("div", {
515
- "class": "iti__selected-dial-code"
516
- }, this.selectedFlag);
437
+ this._appendListItems(this.countries, "iti__standard");
438
+ if (countrySearch) {
439
+ this._updateSearchResultsText();
517
440
  }
518
- if (allowDropdown) {
519
- if (!this.telInput.disabled) {
520
- // make element focusable and tab navigable
521
- this.selectedFlag.setAttribute("tabindex", "0");
522
- }
523
- this.dropdownArrow = this._createEl("div", {
524
- "class": "iti__arrow",
525
- "aria-hidden": "true"
526
- }, this.selectedFlag);
527
- var extraClasses = fixDropdownWidth ? "" : "iti--flexible-dropdown-width";
528
- this.dropdownContent = this._createEl("div", {
529
- id: "iti-".concat(this.id, "__dropdown-content"),
530
- "class": "iti__dropdown-content iti__hide ".concat(extraClasses)
531
- });
532
- if (countrySearch) {
533
- this.searchInput = this._createEl("input", {
534
- type: "text",
535
- "class": "iti__search-input",
536
- placeholder: i18n.searchPlaceholder || "Search",
537
- role: "combobox",
538
- "aria-expanded": "true",
539
- "aria-label": i18n.searchPlaceholder || "Search",
540
- "aria-controls": "iti-".concat(this.id, "__country-listbox"),
541
- "aria-autocomplete": "list",
542
- autocomplete: "off"
543
- }, this.dropdownContent);
544
- this.searchResultsA11yText = this._createEl("span", {
545
- "class": "iti__a11y-text"
546
- }, this.dropdownContent);
547
- }
548
- // country list: preferred countries, then divider, then all countries
549
- this.countryList = this._createEl("ul", {
550
- "class": "iti__country-list",
551
- id: "iti-".concat(this.id, "__country-listbox"),
552
- role: "listbox",
553
- "aria-label": i18n.countryListAriaLabel || "List of countries"
554
- }, this.dropdownContent);
555
- if (this.preferredCountries.length && !countrySearch) {
556
- this._appendListItems(this.preferredCountries, "iti__preferred", true);
557
- this._createEl("li", {
558
- "class": "iti__divider",
559
- "aria-hidden": "true"
560
- }, this.countryList);
561
- }
562
- this._appendListItems(this.countries, "iti__standard");
563
- if (countrySearch) {
564
- this._updateSearchResultsText();
565
- }
566
- // create dropdownContainer markup
567
- if (dropdownContainer) {
568
- var dropdownClasses = "iti iti--container";
569
- if (useFullscreenPopup) {
570
- dropdownClasses += " iti--fullscreen-popup";
571
- } else {
572
- dropdownClasses += " iti--inline-dropdown";
573
- }
574
- if (countrySearch) {
575
- dropdownClasses += " iti--country-search";
576
- }
577
- this.dropdown = this._createEl("div", {
578
- "class": dropdownClasses
579
- });
580
- this.dropdown.appendChild(this.dropdownContent);
441
+ // create dropdownContainer markup
442
+ if (dropdownContainer) {
443
+ let dropdownClasses = "iti iti--container";
444
+ if (useFullscreenPopup) {
445
+ dropdownClasses += " iti--fullscreen-popup";
581
446
  } else {
582
- this.flagsContainer.appendChild(this.dropdownContent);
447
+ dropdownClasses += " iti--inline-dropdown";
583
448
  }
584
- }
585
- if (hiddenInput) {
586
- var telInputName = this.telInput.getAttribute("name");
587
- var names = hiddenInput(telInputName);
588
- if (names.phone) {
589
- // Create hidden input for the full international number
590
- this.hiddenInput = this._createEl("input", {
591
- type: "hidden",
592
- name: names.phone
593
- });
594
- wrapper.appendChild(this.hiddenInput);
595
- }
596
- if (names.country) {
597
- // Create hidden input for the selected country iso2 code
598
- this.hiddenInputCountry = this._createEl("input", {
599
- type: "hidden",
600
- name: names.country
601
- });
602
- wrapper.appendChild(this.hiddenInputCountry);
449
+ if (countrySearch) {
450
+ dropdownClasses += " iti--country-search";
603
451
  }
452
+ this.dropdown = this._createEl("div", {
453
+ class: dropdownClasses
454
+ });
455
+ this.dropdown.appendChild(this.dropdownContent);
456
+ } else {
457
+ this.flagsContainer.appendChild(this.dropdownContent);
604
458
  }
605
459
  }
606
- }, {
607
- key: "_appendListItems",
608
- value: function _appendListItems(countries, className, preferred) {
609
- for (var i = 0; i < countries.length; i++) {
610
- var c = countries[i];
611
- var idSuffix = preferred ? "-preferred" : "";
612
- var listItem = this._createEl("li", {
613
- id: "iti-".concat(this.id, "__item-").concat(c.iso2).concat(idSuffix),
614
- "class": "iti__country ".concat(className),
615
- tabindex: "-1",
616
- role: "option",
617
- "data-dial-code": c.dialCode,
618
- "data-country-code": c.iso2,
619
- "aria-selected": "false"
620
- }, this.countryList);
621
- // store this for later use e.g. country search filtering
622
- c.nodeById[this.id] = listItem;
623
- var content = "";
624
- // add the flag
625
- if (this.options.showFlags) {
626
- content += "<div class='iti__flag-box'><div class='iti__flag iti__".concat(c.iso2, "'></div></div>");
627
- }
628
- // and the country name and dial code
629
- content += "<span class='iti__country-name'>".concat(c.name, "</span>");
630
- content += "<span class='iti__dial-code'>+".concat(c.dialCode, "</span>");
631
- listItem.insertAdjacentHTML("beforeend", content);
460
+ if (hiddenInput) {
461
+ const telInputName = this.telInput.getAttribute("name");
462
+ const names = hiddenInput(telInputName);
463
+ if (names.phone) {
464
+ // Create hidden input for the full international number
465
+ this.hiddenInput = this._createEl("input", {
466
+ type: "hidden",
467
+ name: names.phone
468
+ });
469
+ wrapper.appendChild(this.hiddenInput);
632
470
  }
471
+ if (names.country) {
472
+ // Create hidden input for the selected country iso2 code
473
+ this.hiddenInputCountry = this._createEl("input", {
474
+ type: "hidden",
475
+ name: names.country
476
+ });
477
+ wrapper.appendChild(this.hiddenInputCountry);
478
+ }
479
+ }
480
+ }
481
+ // for each of the passed countries: add a country <li> to the countryList <ul> container
482
+ _appendListItems(countries, className, preferred) {
483
+ for (let i = 0; i < countries.length; i++) {
484
+ const c = countries[i];
485
+ const idSuffix = preferred ? "-preferred" : "";
486
+ const listItem = this._createEl("li", {
487
+ id: `iti-${this.id}__item-${c.iso2}${idSuffix}`,
488
+ class: `iti__country ${className}`,
489
+ tabindex: "-1",
490
+ role: "option",
491
+ "data-dial-code": c.dialCode,
492
+ "data-country-code": c.iso2,
493
+ "aria-selected": "false"
494
+ }, this.countryList);
495
+ // store this for later use e.g. country search filtering
496
+ c.nodeById[this.id] = listItem;
497
+ let content = "";
498
+ // add the flag
499
+ if (this.options.showFlags) {
500
+ content += `<div class='iti__flag-box'><div class='iti__flag iti__${c.iso2}'></div></div>`;
501
+ }
502
+ // and the country name and dial code
503
+ content += `<span class='iti__country-name'>${c.name}</span>`;
504
+ content += `<span class='iti__dial-code'>+${c.dialCode}</span>`;
505
+ listItem.insertAdjacentHTML("beforeend", content);
633
506
  }
634
- }, {
635
- key: "_setInitialState",
636
- value: function _setInitialState() {
637
- var overrideAutoCountry = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
638
- // fix firefox bug: when first load page (with input with value set to number with intl dial
639
- // code) and initialising plugin removes the dial code from the input, then refresh page,
640
- // and we try to init plugin again but this time on number without dial code so get grey flag
641
- var attributeValue = this.telInput.getAttribute("value");
642
- var inputValue = this.telInput.value;
643
- var useAttribute = attributeValue && attributeValue.charAt(0) === "+" && (!inputValue || inputValue.charAt(0) !== "+");
644
- var val = useAttribute ? attributeValue : inputValue;
645
- var dialCode = this._getDialCode(val);
646
- var isRegionlessNanp = this._isRegionlessNanp(val);
647
- var initialCountry = this.options.initialCountry;
648
- // if we already have a dial code, and it's not a regionlessNanp, we can go ahead and set the
649
- // flag, else fall back to the default country
650
- if (dialCode && !isRegionlessNanp) {
651
- this._updateFlagFromNumber(val);
652
- } else if (initialCountry !== "auto" || overrideAutoCountry) {
653
- var lowerInitialCountry = initialCountry ? initialCountry.toLowerCase() : "";
654
- var isValidInitialCountry = lowerInitialCountry && this._getCountryData(lowerInitialCountry, true);
655
- // see if we should select a flag
656
- if (isValidInitialCountry) {
657
- this._setCountry(lowerInitialCountry);
507
+ }
508
+ // set the initial state of the input value and the selected flag by:
509
+ // 1. extracting a dial code from the given number
510
+ // 2. using explicit initialCountry
511
+ // 3. picking the first preferred country
512
+ // 4. picking the first country
513
+ _setInitialState(overrideAutoCountry = false) {
514
+ // fix firefox bug: when first load page (with input with value set to number with intl dial
515
+ // code) and initialising plugin removes the dial code from the input, then refresh page,
516
+ // and we try to init plugin again but this time on number without dial code so get grey flag
517
+ const attributeValue = this.telInput.getAttribute("value");
518
+ const inputValue = this.telInput.value;
519
+ const useAttribute = attributeValue && attributeValue.charAt(0) === "+" && (!inputValue || inputValue.charAt(0) !== "+");
520
+ const val = useAttribute ? attributeValue : inputValue;
521
+ const dialCode = this._getDialCode(val);
522
+ const isRegionlessNanp = this._isRegionlessNanp(val);
523
+ const {
524
+ initialCountry
525
+ } = this.options;
526
+ // if we already have a dial code, and it's not a regionlessNanp, we can go ahead and set the
527
+ // flag, else fall back to the default country
528
+ if (dialCode && !isRegionlessNanp) {
529
+ this._updateFlagFromNumber(val);
530
+ } else if (initialCountry !== "auto" || overrideAutoCountry) {
531
+ const lowerInitialCountry = initialCountry ? initialCountry.toLowerCase() : "";
532
+ const isValidInitialCountry = lowerInitialCountry && this._getCountryData(lowerInitialCountry, true);
533
+ // see if we should select a flag
534
+ if (isValidInitialCountry) {
535
+ this._setCountry(lowerInitialCountry);
536
+ } else {
537
+ if (dialCode && isRegionlessNanp) {
538
+ // has intl dial code, is regionless nanp, and no initialCountry, so default to US
539
+ this._setCountry("us");
658
540
  } else {
659
- if (dialCode && isRegionlessNanp) {
660
- // has intl dial code, is regionless nanp, and no initialCountry, so default to US
661
- this._setCountry("us");
662
- } else {
663
- // display the empty state (globe icon)
664
- this._setCountry();
665
- }
541
+ // display the empty state (globe icon)
542
+ this._setCountry();
666
543
  }
667
544
  }
668
- // NOTE: if initialCountry is set to auto, that will be handled separately
669
- // format - note this wont be run after _updateDialCode as that's only called if no val
670
- if (val) {
671
- this._updateValFromNumber(val);
672
- }
673
545
  }
674
- }, {
675
- key: "_initListeners",
676
- value: function _initListeners() {
677
- this._initTelInputListeners();
678
- if (this.options.allowDropdown) {
679
- this._initDropdownListeners();
680
- }
681
- if ((this.hiddenInput || this.hiddenInputCountry) && this.telInput.form) {
682
- this._initHiddenInputListener();
683
- }
546
+ // NOTE: if initialCountry is set to auto, that will be handled separately
547
+ // format - note this wont be run after _updateDialCode as that's only called if no val
548
+ if (val) {
549
+ this._updateValFromNumber(val);
684
550
  }
685
- }, {
686
- key: "_initHiddenInputListener",
687
- value: function _initHiddenInputListener() {
688
- var _this2 = this;
689
- this._handleHiddenInputSubmit = function() {
690
- if (_this2.hiddenInput) {
691
- _this2.hiddenInput.value = _this2.getNumber();
692
- }
693
- if (_this2.hiddenInputCountry) {
694
- _this2.hiddenInputCountry.value = _this2.getSelectedCountryData().iso2;
695
- }
696
- };
697
- this.telInput.form.addEventListener("submit", this._handleHiddenInputSubmit);
698
- }
699
- }, {
700
- key: "_initDropdownListeners",
701
- value: function _initDropdownListeners() {
702
- var _this3 = this;
703
- // hack for input nested inside label (which is valid markup): clicking the selected-flag to
704
- // open the dropdown would then automatically trigger a 2nd click on the input which would
705
- // close it again
706
- this._handleLabelClick = function(e) {
707
- // if the dropdown is closed, then focus the input, else ignore the click
708
- if (_this3.dropdownContent.classList.contains("iti__hide")) {
709
- _this3.telInput.focus();
710
- } else {
711
- e.preventDefault();
712
- }
713
- };
714
- var label = this.telInput.closest("label");
715
- if (label) {
716
- label.addEventListener("click", this._handleLabelClick);
551
+ }
552
+ // initialise the main event listeners: input keyup, and click selected flag
553
+ _initListeners() {
554
+ this._initTelInputListeners();
555
+ if (this.options.allowDropdown) {
556
+ this._initDropdownListeners();
557
+ }
558
+ if ((this.hiddenInput || this.hiddenInputCountry) && this.telInput.form) {
559
+ this._initHiddenInputListener();
560
+ }
561
+ }
562
+ // update hidden input on form submit
563
+ _initHiddenInputListener() {
564
+ this._handleHiddenInputSubmit = () => {
565
+ if (this.hiddenInput) {
566
+ this.hiddenInput.value = this.getNumber();
717
567
  }
718
- // toggle country dropdown on click
719
- this._handleClickSelectedFlag = function() {
720
- // only intercept this event if we're opening the dropdown
721
- // else let it bubble up to the top ("click-off-to-close" listener)
722
- // we cannot just stopPropagation as it may be needed to close another instance
723
- if (_this3.dropdownContent.classList.contains("iti__hide") && !_this3.telInput.disabled && !_this3.telInput.readOnly) {
724
- _this3._openDropdown();
725
- }
726
- };
727
- this.selectedFlag.addEventListener("click", this._handleClickSelectedFlag);
728
- // open dropdown if selected flag is focused and they press up/down/space/enter
729
- this._handleFlagsContainerKeydown = function(e) {
730
- var isDropdownHidden = _this3.dropdownContent.classList.contains("iti__hide");
731
- if (isDropdownHidden && [ "ArrowUp", "ArrowDown", " ", "Enter" ].includes(e.key)) {
732
- // prevent form from being submitted if "ENTER" was pressed
733
- e.preventDefault();
734
- // prevent event from being handled again by document
735
- e.stopPropagation();
736
- _this3._openDropdown();
737
- }
738
- // allow navigation from dropdown to input on TAB
739
- if (e.key === "Tab") {
740
- _this3._closeDropdown();
741
- }
742
- };
743
- this.flagsContainer.addEventListener("keydown", this._handleFlagsContainerKeydown);
744
- }
745
- }, {
746
- key: "_initRequests",
747
- value: function _initRequests() {
748
- var _this4 = this;
749
- // if the user has specified the path to the utils script, fetch it on window.load, else resolve
750
- if (this.options.utilsScript && !window.intlTelInputUtils) {
751
- // if the plugin is being initialised after the window.load event has already been fired
752
- if (window.intlTelInputGlobals.documentReady()) {
753
- window.intlTelInputGlobals.loadUtils(this.options.utilsScript);
754
- } else {
755
- // wait until the load event so we don't block any other requests e.g. the flags image
756
- window.addEventListener("load", function() {
757
- window.intlTelInputGlobals.loadUtils(_this4.options.utilsScript);
758
- });
759
- }
760
- } else {
761
- this.resolveUtilsScriptPromise();
568
+ if (this.hiddenInputCountry) {
569
+ this.hiddenInputCountry.value = this.getSelectedCountryData().iso2;
762
570
  }
763
- // dont bother with IP lookup if we already have a selected country
764
- if (this.options.initialCountry === "auto" && !this.selectedCountryData.iso2) {
765
- this._loadAutoCountry();
571
+ };
572
+ this.telInput.form.addEventListener("submit", this._handleHiddenInputSubmit);
573
+ }
574
+ // initialise the dropdown listeners
575
+ _initDropdownListeners() {
576
+ // hack for input nested inside label (which is valid markup): clicking the selected-flag to
577
+ // open the dropdown would then automatically trigger a 2nd click on the input which would
578
+ // close it again
579
+ this._handleLabelClick = e => {
580
+ // if the dropdown is closed, then focus the input, else ignore the click
581
+ if (this.dropdownContent.classList.contains("iti__hide")) {
582
+ this.telInput.focus();
766
583
  } else {
767
- this.resolveAutoCountryPromise();
584
+ e.preventDefault();
768
585
  }
586
+ };
587
+ const label = this.telInput.closest("label");
588
+ if (label) {
589
+ label.addEventListener("click", this._handleLabelClick);
769
590
  }
770
- }, {
771
- key: "_loadAutoCountry",
772
- value: function _loadAutoCountry() {
773
- var _this5 = this;
774
- // 3 options:
775
- // 1) already loaded (we're done)
776
- // 2) not already started loading (start)
777
- // 3) already started loading (do nothing - just wait for loading callback to fire)
778
- if (window.intlTelInputGlobals.autoCountry) {
779
- this.handleAutoCountry();
780
- } else if (!window.intlTelInputGlobals.startedLoadingAutoCountry) {
781
- // don't do this twice!
782
- window.intlTelInputGlobals.startedLoadingAutoCountry = true;
783
- if (typeof this.options.geoIpLookup === "function") {
784
- this.options.geoIpLookup(function() {
785
- var iso2 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : "";
786
- var iso2Lower = iso2.toLowerCase();
787
- var isValidIso2 = iso2Lower && _this5._getCountryData(iso2Lower, true);
788
- if (isValidIso2) {
789
- window.intlTelInputGlobals.autoCountry = iso2Lower;
790
- // tell all instances the auto country is ready
791
- // TODO: this should just be the current instances
792
- // UPDATE: use setTimeout in case their geoIpLookup function calls this callback straight
793
- // away (e.g. if they have already done the geo ip lookup somewhere else). Using
794
- // setTimeout means that the current thread of execution will finish before executing
795
- // this, which allows the plugin to finish initialising.
796
- setTimeout(function() {
797
- return forEachInstance("handleAutoCountry");
798
- });
799
- } else {
800
- _this5._setInitialState(true);
801
- forEachInstance("rejectAutoCountryPromise");
802
- }
803
- }, function() {
804
- return forEachInstance("rejectAutoCountryPromise");
805
- });
806
- }
591
+ // toggle country dropdown on click
592
+ this._handleClickSelectedFlag = () => {
593
+ // only intercept this event if we're opening the dropdown
594
+ // else let it bubble up to the top ("click-off-to-close" listener)
595
+ // we cannot just stopPropagation as it may be needed to close another instance
596
+ if (this.dropdownContent.classList.contains("iti__hide") && !this.telInput.disabled && !this.telInput.readOnly) {
597
+ this._openDropdown();
598
+ }
599
+ };
600
+ this.selectedFlag.addEventListener("click", this._handleClickSelectedFlag);
601
+ // open dropdown if selected flag is focused and they press up/down/space/enter
602
+ this._handleFlagsContainerKeydown = e => {
603
+ const isDropdownHidden = this.dropdownContent.classList.contains("iti__hide");
604
+ if (isDropdownHidden && [ "ArrowUp", "ArrowDown", " ", "Enter" ].includes(e.key)) {
605
+ // prevent form from being submitted if "ENTER" was pressed
606
+ e.preventDefault();
607
+ // prevent event from being handled again by document
608
+ e.stopPropagation();
609
+ this._openDropdown();
610
+ }
611
+ // allow navigation from dropdown to input on TAB
612
+ if (e.key === "Tab") {
613
+ this._closeDropdown();
614
+ }
615
+ };
616
+ this.flagsContainer.addEventListener("keydown", this._handleFlagsContainerKeydown);
617
+ }
618
+ // init many requests: utils script / geo ip lookup
619
+ _initRequests() {
620
+ // if the user has specified the path to the utils script, fetch it on window.load, else resolve
621
+ if (this.options.utilsScript && !window.intlTelInputUtils) {
622
+ // if the plugin is being initialised after the window.load event has already been fired
623
+ if (window.intlTelInputGlobals.documentReady()) {
624
+ window.intlTelInputGlobals.loadUtils(this.options.utilsScript);
625
+ } else {
626
+ // wait until the load event so we don't block any other requests e.g. the flags image
627
+ window.addEventListener("load", () => {
628
+ window.intlTelInputGlobals.loadUtils(this.options.utilsScript);
629
+ });
807
630
  }
631
+ } else {
632
+ this.resolveUtilsScriptPromise();
808
633
  }
809
- }, {
810
- key: "_initTelInputListeners",
811
- value: function _initTelInputListeners() {
812
- var _this6 = this;
813
- var _this$options2 = this.options, strictMode = _this$options2.strictMode, formatAsYouType = _this$options2.formatAsYouType;
814
- var userOverrideFormatting = false;
815
- // update flag on input event
816
- this._handleInputEvent = function(e) {
817
- if (_this6._updateFlagFromNumber(_this6.telInput.value)) {
818
- _this6._triggerCountryChange();
819
- }
820
- // if user types their own formatting char (not a plus or a numeric), or they paste something, then set the override
821
- var isFormattingChar = e && e.data && /[^+0-9]/.test(e.data);
822
- var isPaste = e && e.inputType === "insertFromPaste" && _this6.telInput.value;
823
- if (isFormattingChar || isPaste && !strictMode) {
824
- userOverrideFormatting = true;
825
- } else if (!/[^+0-9]/.test(_this6.telInput.value)) {
826
- userOverrideFormatting = false;
827
- }
828
- // handle FAYT, unless userOverrideFormatting
829
- if (formatAsYouType && !userOverrideFormatting) {
830
- // maintain caret position after reformatting
831
- var currentCaretPos = _this6.telInput.selectionStart;
832
- var valueBeforeCaret = _this6.telInput.value.substring(0, currentCaretPos);
833
- var relevantCharsBeforeCaret = valueBeforeCaret.replace(/[^+0-9]/g, "").length;
834
- var isDeleteForwards = e && e.inputType === "deleteContentForward";
835
- var formattedValue = _this6._formatNumberAsYouType();
836
- var newCaretPos = _this6._translateCursorPosition(relevantCharsBeforeCaret, formattedValue, currentCaretPos, isDeleteForwards);
837
- _this6.telInput.value = formattedValue;
838
- _this6.telInput.setSelectionRange(newCaretPos, newCaretPos);
839
- }
840
- };
841
- // this handles individual key presses as well as cut/paste events
842
- // the advantage of the "input" event over "keyup" etc is that "input" only fires when the value changes,
843
- // whereas "keyup" fires even for shift key, arrow key presses etc
844
- this.telInput.addEventListener("input", this._handleInputEvent);
845
- if (strictMode) {
846
- this._handleKeydownEvent = function(e) {
847
- // only ignore actual character presses, rather than ctrl, alt, shift, command, arrow keys, delete/backspace, cut/copy/paste etc
848
- if (e.key.length === 1 && !e.altKey && !e.ctrlKey && !e.metaKey) {
849
- var isInitialPlus = _this6.telInput.selectionStart === 0 && e.key === "+";
850
- var isNumeric = /^[0-9]$/.test(e.key);
851
- var isAllowedChar = isInitialPlus || isNumeric;
852
- var fullNumber = _this6._getFullNumber();
853
- var coreNumber = intlTelInputUtils.getCoreNumber(fullNumber, _this6.selectedCountryData.iso2);
854
- var hasReachedMaxLength = _this6.maxCoreNumberLength && coreNumber.length >= _this6.maxCoreNumberLength;
855
- if (!isAllowedChar || hasReachedMaxLength) {
856
- e.preventDefault();
857
- }
634
+ // dont bother with IP lookup if we already have a selected country
635
+ if (this.options.initialCountry === "auto" && !this.selectedCountryData.iso2) {
636
+ this._loadAutoCountry();
637
+ } else {
638
+ this.resolveAutoCountryPromise();
639
+ }
640
+ }
641
+ // perform the geo ip lookup
642
+ _loadAutoCountry() {
643
+ // 3 options:
644
+ // 1) already loaded (we're done)
645
+ // 2) not already started loading (start)
646
+ // 3) already started loading (do nothing - just wait for loading callback to fire)
647
+ if (window.intlTelInputGlobals.autoCountry) {
648
+ this.handleAutoCountry();
649
+ } else if (!window.intlTelInputGlobals.startedLoadingAutoCountry) {
650
+ // don't do this twice!
651
+ window.intlTelInputGlobals.startedLoadingAutoCountry = true;
652
+ if (typeof this.options.geoIpLookup === "function") {
653
+ this.options.geoIpLookup((iso2 = "") => {
654
+ const iso2Lower = iso2.toLowerCase();
655
+ const isValidIso2 = iso2Lower && this._getCountryData(iso2Lower, true);
656
+ if (isValidIso2) {
657
+ window.intlTelInputGlobals.autoCountry = iso2Lower;
658
+ // tell all instances the auto country is ready
659
+ // TODO: this should just be the current instances
660
+ // UPDATE: use setTimeout in case their geoIpLookup function calls this callback straight
661
+ // away (e.g. if they have already done the geo ip lookup somewhere else). Using
662
+ // setTimeout means that the current thread of execution will finish before executing
663
+ // this, which allows the plugin to finish initialising.
664
+ setTimeout(() => forEachInstance("handleAutoCountry"));
665
+ } else {
666
+ this._setInitialState(true);
667
+ forEachInstance("rejectAutoCountryPromise");
858
668
  }
859
- };
860
- this.telInput.addEventListener("keydown", this._handleKeydownEvent);
669
+ }, () => forEachInstance("rejectAutoCountryPromise"));
861
670
  }
862
671
  }
863
- }, {
864
- key: "_translateCursorPosition",
865
- value: function _translateCursorPosition(relevantChars, formattedValue, prevCaretPos, isDeleteForwards) {
866
- // if the first char is a formatting char, and they backspace delete it:
867
- // cursor should stay at the start (pos 0), rather than stick to the first digit (pos 1)
868
- if (prevCaretPos === 0 && !isDeleteForwards) {
869
- return 0;
870
- }
871
- var count = 0;
872
- for (var i = 0; i < formattedValue.length; i++) {
873
- if (/[+0-9]/.test(formattedValue[i])) {
874
- count++;
875
- }
876
- // normal case: stop when you hit the right number of relevant chars
877
- // (cursor will be just after the final relevant char)
878
- if (count === relevantChars && !isDeleteForwards) {
879
- return i + 1;
880
- }
881
- // spacial case: delete forwards (fn + delete on a mac):
882
- // wait until hit one extra relevant char, and put the cursor just before it (after any formatting chars)
883
- if (isDeleteForwards && count === relevantChars + 1) {
884
- return i;
885
- }
672
+ }
673
+ // initialize the tel input listeners
674
+ _initTelInputListeners() {
675
+ const {
676
+ strictMode,
677
+ formatAsYouType
678
+ } = this.options;
679
+ let userOverrideFormatting = false;
680
+ // update flag on input event
681
+ this._handleInputEvent = e => {
682
+ if (this._updateFlagFromNumber(this.telInput.value)) {
683
+ this._triggerCountryChange();
886
684
  }
887
- return formattedValue.length;
888
- }
889
- }, {
890
- key: "_cap",
891
- value: function _cap(number) {
892
- var max = this.telInput.getAttribute("maxlength");
893
- return max && number.length > max ? number.substr(0, max) : number;
894
- }
895
- }, {
896
- key: "_removeEmptyDialCode",
897
- value: function _removeEmptyDialCode() {
898
- if (this.telInput.value.charAt(0) === "+") {
899
- var numeric = this._getNumeric(this.telInput.value);
900
- // if just a plus, or if just a dial code
901
- if (!numeric || this.selectedCountryData.dialCode === numeric) {
902
- this.telInput.value = "";
685
+ // if user types their own formatting char (not a plus or a numeric), or they paste something, then set the override
686
+ const isFormattingChar = e && e.data && /[^+0-9]/.test(e.data);
687
+ const isPaste = e && e.inputType === "insertFromPaste" && this.telInput.value;
688
+ if (isFormattingChar || isPaste && !strictMode) {
689
+ userOverrideFormatting = true;
690
+ }
691
+ // if user removes all formatting chars, then reset the override
692
+ else if (!/[^+0-9]/.test(this.telInput.value)) {
693
+ userOverrideFormatting = false;
694
+ }
695
+ // handle FAYT, unless userOverrideFormatting
696
+ if (formatAsYouType && !userOverrideFormatting) {
697
+ // maintain caret position after reformatting
698
+ const currentCaretPos = this.telInput.selectionStart;
699
+ const valueBeforeCaret = this.telInput.value.substring(0, currentCaretPos);
700
+ const relevantCharsBeforeCaret = valueBeforeCaret.replace(/[^+0-9]/g, "").length;
701
+ const isDeleteForwards = e && e.inputType === "deleteContentForward";
702
+ const formattedValue = this._formatNumberAsYouType();
703
+ const newCaretPos = this._translateCursorPosition(relevantCharsBeforeCaret, formattedValue, currentCaretPos, isDeleteForwards);
704
+ this.telInput.value = formattedValue;
705
+ this.telInput.setSelectionRange(newCaretPos, newCaretPos);
706
+ }
707
+ };
708
+ // this handles individual key presses as well as cut/paste events
709
+ // the advantage of the "input" event over "keyup" etc is that "input" only fires when the value changes,
710
+ // whereas "keyup" fires even for shift key, arrow key presses etc
711
+ this.telInput.addEventListener("input", this._handleInputEvent);
712
+ if (strictMode) {
713
+ this._handleKeydownEvent = e => {
714
+ // only ignore actual character presses, rather than ctrl, alt, shift, command, arrow keys, delete/backspace, cut/copy/paste etc
715
+ if (e.key.length === 1 && !e.altKey && !e.ctrlKey && !e.metaKey) {
716
+ const isInitialPlus = this.telInput.selectionStart === 0 && e.key === "+";
717
+ const isNumeric = /^[0-9]$/.test(e.key);
718
+ const isAllowedChar = isInitialPlus || isNumeric;
719
+ const fullNumber = this._getFullNumber();
720
+ const coreNumber = intlTelInputUtils.getCoreNumber(fullNumber, this.selectedCountryData.iso2);
721
+ const hasReachedMaxLength = this.maxCoreNumberLength && coreNumber.length >= this.maxCoreNumberLength;
722
+ if (!isAllowedChar || hasReachedMaxLength) {
723
+ e.preventDefault();
724
+ }
903
725
  }
904
- }
726
+ };
727
+ this.telInput.addEventListener("keydown", this._handleKeydownEvent);
905
728
  }
906
- }, {
907
- key: "_getNumeric",
908
- value: function _getNumeric(s) {
909
- return s.replace(/\D/g, "");
910
- }
911
- }, {
912
- key: "_trigger",
913
- value: function _trigger(name) {
914
- var e = new Event(name, {
915
- bubbles: true,
916
- cancelable: true
917
- });
918
- this.telInput.dispatchEvent(e);
919
- }
920
- }, {
921
- key: "_openDropdown",
922
- value: function _openDropdown() {
923
- var _this$options3 = this.options, fixDropdownWidth = _this$options3.fixDropdownWidth, countrySearch = _this$options3.countrySearch;
924
- if (fixDropdownWidth) {
925
- this.dropdownContent.style.width = "".concat(this.telInput.offsetWidth, "px");
729
+ }
730
+ // iterate through the formattedValue until hit the right number of relevant chars
731
+ _translateCursorPosition(relevantChars, formattedValue, prevCaretPos, isDeleteForwards) {
732
+ // if the first char is a formatting char, and they backspace delete it:
733
+ // cursor should stay at the start (pos 0), rather than stick to the first digit (pos 1)
734
+ if (prevCaretPos === 0 && !isDeleteForwards) {
735
+ return 0;
736
+ }
737
+ let count = 0;
738
+ for (let i = 0; i < formattedValue.length; i++) {
739
+ if (/[+0-9]/.test(formattedValue[i])) {
740
+ count++;
926
741
  }
927
- this.dropdownContent.classList.remove("iti__hide");
928
- this.selectedFlag.setAttribute("aria-expanded", "true");
929
- this._setDropdownPosition();
930
- // if we have previously selected a country (and countrySearch is disabled), then highlight that item and scroll to it
931
- // else highlight the first item and scroll to top (even if countrySearch is disabled e.g. on init, showing globe icon)
932
- if (this.activeItem && !countrySearch) {
933
- // update highlighting and scroll to active list item
934
- this._highlightListItem(this.activeItem, false);
935
- this._scrollTo(this.activeItem, true);
936
- } else {
937
- // start by highlighting the first item in the list
938
- var firstElementChild = this.countryList.firstElementChild;
939
- if (firstElementChild) {
940
- this._highlightListItem(firstElementChild, false);
941
- this.countryList.scrollTop = 0;
942
- }
943
- if (countrySearch) {
944
- this.searchInput.focus();
945
- }
742
+ // normal case: stop when you hit the right number of relevant chars
743
+ // (cursor will be just after the final relevant char)
744
+ if (count === relevantChars && !isDeleteForwards) {
745
+ return i + 1;
946
746
  }
947
- // bind all the dropdown-related listeners: mouseover, click, click-off, keydown
948
- this._bindDropdownListeners();
949
- // update the arrow
950
- this.dropdownArrow.classList.add("iti__arrow--up");
951
- this._trigger("open:countrydropdown");
952
- }
953
- }, {
954
- key: "_toggleClass",
955
- value: function _toggleClass(el, className, shouldHaveClass) {
956
- if (shouldHaveClass && !el.classList.contains(className)) {
957
- el.classList.add(className);
958
- } else if (!shouldHaveClass && el.classList.contains(className)) {
959
- el.classList.remove(className);
747
+ // spacial case: delete forwards (fn + delete on a mac):
748
+ // wait until hit one extra relevant char, and put the cursor just before it (after any formatting chars)
749
+ if (isDeleteForwards && count === relevantChars + 1) {
750
+ return i;
960
751
  }
961
752
  }
962
- }, {
963
- key: "_setDropdownPosition",
964
- value: function _setDropdownPosition() {
965
- var _this7 = this;
966
- if (this.options.dropdownContainer) {
967
- this.options.dropdownContainer.appendChild(this.dropdown);
753
+ return formattedValue.length;
754
+ }
755
+ // adhere to the input's maxlength attr
756
+ _cap(number) {
757
+ const max = this.telInput.getAttribute("maxlength");
758
+ return max && number.length > max ? number.substr(0, max) : number;
759
+ }
760
+ // clear the input if it just contains a dial code
761
+ _removeEmptyDialCode() {
762
+ if (this.telInput.value.charAt(0) === "+") {
763
+ const numeric = this._getNumeric(this.telInput.value);
764
+ // if just a plus, or if just a dial code
765
+ if (!numeric || this.selectedCountryData.dialCode === numeric) {
766
+ this.telInput.value = "";
968
767
  }
969
- if (!this.options.useFullscreenPopup) {
970
- var pos = this.telInput.getBoundingClientRect();
971
- // windowTop from https://stackoverflow.com/a/14384091/217866
972
- var windowTop = document.documentElement.scrollTop;
973
- var inputTop = pos.top + windowTop;
974
- var dropdownHeight = this.dropdownContent.offsetHeight;
975
- // dropdownFitsBelow = (dropdownBottom < windowBottom)
976
- var dropdownFitsBelow = inputTop + this.telInput.offsetHeight + dropdownHeight < windowTop + window.innerHeight;
977
- var dropdownFitsAbove = inputTop - dropdownHeight > windowTop;
978
- // dont allow positioning above when country search enabled as the search box jumps around as you filter countries
979
- var positionDropdownAboveInput = !this.options.countrySearch && !dropdownFitsBelow && dropdownFitsAbove;
980
- // by default, the dropdown will be below the input. If we want to position it above the
981
- // input, we add the dropup class.
982
- this._toggleClass(this.dropdownContent, "iti__dropdown-content--dropup", positionDropdownAboveInput);
983
- // if dropdownContainer is enabled, calculate postion
984
- if (this.options.dropdownContainer) {
985
- // if we want to position the dropdown below the input, we need to add the input height to the top value
986
- var extraTop = positionDropdownAboveInput ? 0 : this.telInput.offsetHeight;
987
- // calculate placement
988
- this.dropdown.style.top = "".concat(inputTop + extraTop, "px");
989
- this.dropdown.style.left = "".concat(pos.left + document.body.scrollLeft, "px");
990
- // close menu on window scroll
991
- this._handleWindowScroll = function() {
992
- return _this7._closeDropdown();
993
- };
994
- window.addEventListener("scroll", this._handleWindowScroll);
995
- }
768
+ }
769
+ }
770
+ // extract the numeric digits from the given string
771
+ _getNumeric(s) {
772
+ return s.replace(/\D/g, "");
773
+ }
774
+ // trigger a custom event on the input
775
+ _trigger(name) {
776
+ const e = new Event(name, {
777
+ bubbles: true,
778
+ cancelable: true
779
+ });
780
+ this.telInput.dispatchEvent(e);
781
+ }
782
+ // open the dropdown
783
+ _openDropdown() {
784
+ const {
785
+ fixDropdownWidth,
786
+ countrySearch
787
+ } = this.options;
788
+ if (fixDropdownWidth) {
789
+ this.dropdownContent.style.width = `${this.telInput.offsetWidth}px`;
790
+ }
791
+ this.dropdownContent.classList.remove("iti__hide");
792
+ this.selectedFlag.setAttribute("aria-expanded", "true");
793
+ this._setDropdownPosition();
794
+ // if we have previously selected a country (and countrySearch is disabled), then highlight that item and scroll to it
795
+ // else highlight the first item and scroll to top (even if countrySearch is disabled e.g. on init, showing globe icon)
796
+ if (this.activeItem && !countrySearch) {
797
+ // update highlighting and scroll to active list item
798
+ this._highlightListItem(this.activeItem, false);
799
+ this._scrollTo(this.activeItem, true);
800
+ } else {
801
+ // start by highlighting the first item in the list
802
+ const {
803
+ firstElementChild
804
+ } = this.countryList;
805
+ if (firstElementChild) {
806
+ this._highlightListItem(firstElementChild, false);
807
+ this.countryList.scrollTop = 0;
808
+ }
809
+ if (countrySearch) {
810
+ this.searchInput.focus();
811
+ }
812
+ }
813
+ // bind all the dropdown-related listeners: mouseover, click, click-off, keydown
814
+ this._bindDropdownListeners();
815
+ // update the arrow
816
+ this.dropdownArrow.classList.add("iti__arrow--up");
817
+ this._trigger("open:countrydropdown");
818
+ }
819
+ // make sure the el has the className or not, depending on the value of shouldHaveClass
820
+ _toggleClass(el, className, shouldHaveClass) {
821
+ if (shouldHaveClass && !el.classList.contains(className)) {
822
+ el.classList.add(className);
823
+ } else if (!shouldHaveClass && el.classList.contains(className)) {
824
+ el.classList.remove(className);
825
+ }
826
+ }
827
+ // decide if should position dropdown above or below input (depends on position within viewport, and scroll)
828
+ _setDropdownPosition() {
829
+ if (this.options.dropdownContainer) {
830
+ this.options.dropdownContainer.appendChild(this.dropdown);
831
+ }
832
+ if (!this.options.useFullscreenPopup) {
833
+ const pos = this.telInput.getBoundingClientRect();
834
+ // windowTop from https://stackoverflow.com/a/14384091/217866
835
+ const windowTop = document.documentElement.scrollTop;
836
+ const inputTop = pos.top + windowTop;
837
+ const dropdownHeight = this.dropdownContent.offsetHeight;
838
+ // dropdownFitsBelow = (dropdownBottom < windowBottom)
839
+ const dropdownFitsBelow = inputTop + this.telInput.offsetHeight + dropdownHeight < windowTop + window.innerHeight;
840
+ const dropdownFitsAbove = inputTop - dropdownHeight > windowTop;
841
+ // dont allow positioning above when country search enabled as the search box jumps around as you filter countries
842
+ const positionDropdownAboveInput = !this.options.countrySearch && !dropdownFitsBelow && dropdownFitsAbove;
843
+ // by default, the dropdown will be below the input. If we want to position it above the
844
+ // input, we add the dropup class.
845
+ this._toggleClass(this.dropdownContent, "iti__dropdown-content--dropup", positionDropdownAboveInput);
846
+ // if dropdownContainer is enabled, calculate postion
847
+ if (this.options.dropdownContainer) {
848
+ // if we want to position the dropdown below the input, we need to add the input height to the top value
849
+ const extraTop = positionDropdownAboveInput ? 0 : this.telInput.offsetHeight;
850
+ // calculate placement
851
+ this.dropdown.style.top = `${inputTop + extraTop}px`;
852
+ this.dropdown.style.left = `${pos.left + document.body.scrollLeft}px`;
853
+ // close menu on window scroll
854
+ this._handleWindowScroll = () => this._closeDropdown();
855
+ window.addEventListener("scroll", this._handleWindowScroll);
996
856
  }
997
857
  }
998
- }, {
999
- key: "_bindDropdownListeners",
1000
- value: function _bindDropdownListeners() {
1001
- var _this8 = this;
1002
- // when mouse over a list item, just highlight that one
1003
- // we add the class "highlight", so if they hit "enter" we know which one to select
1004
- this._handleMouseoverCountryList = function(e) {
1005
- // handle event delegation, as we're listening for this event on the countryList
1006
- var listItem = e.target.closest(".iti__country");
1007
- if (listItem) {
1008
- _this8._highlightListItem(listItem, false);
858
+ }
859
+ // we only bind dropdown listeners when the dropdown is open
860
+ _bindDropdownListeners() {
861
+ // when mouse over a list item, just highlight that one
862
+ // we add the class "highlight", so if they hit "enter" we know which one to select
863
+ this._handleMouseoverCountryList = e => {
864
+ // handle event delegation, as we're listening for this event on the countryList
865
+ const listItem = e.target.closest(".iti__country");
866
+ if (listItem) {
867
+ this._highlightListItem(listItem, false);
868
+ }
869
+ };
870
+ this.countryList.addEventListener("mouseover", this._handleMouseoverCountryList);
871
+ // listen for country selection
872
+ this._handleClickCountryList = e => {
873
+ const listItem = e.target.closest(".iti__country");
874
+ if (listItem) {
875
+ this._selectListItem(listItem);
876
+ }
877
+ };
878
+ this.countryList.addEventListener("click", this._handleClickCountryList);
879
+ // click off to close
880
+ // (except when this initial opening click is bubbling up)
881
+ // we cannot just stopPropagation as it may be needed to close another instance
882
+ let isOpening = true;
883
+ this._handleClickOffToClose = () => {
884
+ if (!isOpening) {
885
+ this._closeDropdown();
886
+ }
887
+ isOpening = false;
888
+ };
889
+ document.documentElement.addEventListener("click", this._handleClickOffToClose);
890
+ // listen for up/down scrolling, enter to select, or escape to close
891
+ // use keydown as keypress doesn't fire for non-char keys and we want to catch if they
892
+ // just hit down and hold it to scroll down (no keyup event).
893
+ // listen on the document because that's where key events are triggered if no input has focus
894
+ let query = "";
895
+ let queryTimer = null;
896
+ this._handleKeydownOnDropdown = e => {
897
+ // prevent down key from scrolling the whole page,
898
+ // and enter key from submitting a form etc
899
+ if ([ "ArrowUp", "ArrowDown", "Enter", "Escape" ].includes(e.key)) {
900
+ e.preventDefault();
901
+ e.stopPropagation();
902
+ // up and down to navigate
903
+ if (e.key === "ArrowUp" || e.key === "ArrowDown") {
904
+ this._handleUpDownKey(e.key);
1009
905
  }
1010
- };
1011
- this.countryList.addEventListener("mouseover", this._handleMouseoverCountryList);
1012
- // listen for country selection
1013
- this._handleClickCountryList = function(e) {
1014
- var listItem = e.target.closest(".iti__country");
1015
- if (listItem) {
1016
- _this8._selectListItem(listItem);
906
+ // enter to select
907
+ else if (e.key === "Enter") {
908
+ this._handleEnterKey();
1017
909
  }
1018
- };
1019
- this.countryList.addEventListener("click", this._handleClickCountryList);
1020
- // click off to close
1021
- // (except when this initial opening click is bubbling up)
1022
- // we cannot just stopPropagation as it may be needed to close another instance
1023
- var isOpening = true;
1024
- this._handleClickOffToClose = function() {
1025
- if (!isOpening) {
1026
- _this8._closeDropdown();
910
+ // esc to close
911
+ else if (e.key === "Escape") {
912
+ this._closeDropdown();
1027
913
  }
1028
- isOpening = false;
1029
- };
1030
- document.documentElement.addEventListener("click", this._handleClickOffToClose);
1031
- // listen for up/down scrolling, enter to select, or escape to close
1032
- // use keydown as keypress doesn't fire for non-char keys and we want to catch if they
1033
- // just hit down and hold it to scroll down (no keyup event).
1034
- // listen on the document because that's where key events are triggered if no input has focus
1035
- var query = "";
1036
- var queryTimer = null;
1037
- this._handleKeydownOnDropdown = function(e) {
1038
- // prevent down key from scrolling the whole page,
1039
- // and enter key from submitting a form etc
1040
- if ([ "ArrowUp", "ArrowDown", "Enter", "Escape" ].includes(e.key)) {
1041
- e.preventDefault();
1042
- e.stopPropagation();
1043
- // up and down to navigate
1044
- if (e.key === "ArrowUp" || e.key === "ArrowDown") {
1045
- _this8._handleUpDownKey(e.key);
1046
- } else if (e.key === "Enter") {
1047
- _this8._handleEnterKey();
1048
- } else if (e.key === "Escape") {
1049
- _this8._closeDropdown();
1050
- }
914
+ }
915
+ // alpha chars to perform search
916
+ // regex allows one latin alpha char or space, based on https://stackoverflow.com/a/26900132/217866)
917
+ if (!this.options.countrySearch && /^[a-zA-ZÀ-ÿа-яА-Я ]$/.test(e.key)) {
918
+ e.stopPropagation();
919
+ // jump to countries that start with the query string
920
+ if (queryTimer) {
921
+ clearTimeout(queryTimer);
1051
922
  }
1052
- // alpha chars to perform search
1053
- // regex allows one latin alpha char or space, based on https://stackoverflow.com/a/26900132/217866)
1054
- if (!_this8.options.countrySearch && /^[a-zA-ZÀ-ÿа-яА-Я ]$/.test(e.key)) {
1055
- e.stopPropagation();
1056
- // jump to countries that start with the query string
1057
- if (queryTimer) {
1058
- clearTimeout(queryTimer);
1059
- }
1060
- query += e.key.toLowerCase();
1061
- _this8._searchForCountry(query);
1062
- // if the timer hits 1 second, reset the query
1063
- queryTimer = setTimeout(function() {
1064
- query = "";
1065
- }, 1e3);
923
+ query += e.key.toLowerCase();
924
+ this._searchForCountry(query);
925
+ // if the timer hits 1 second, reset the query
926
+ queryTimer = setTimeout(() => {
927
+ query = "";
928
+ }, 1e3);
929
+ }
930
+ };
931
+ document.addEventListener("keydown", this._handleKeydownOnDropdown);
932
+ if (this.options.countrySearch) {
933
+ const doFilter = () => {
934
+ const inputQuery = this.searchInput.value.trim();
935
+ if (inputQuery) {
936
+ this._filterCountries(inputQuery);
937
+ } else {
938
+ this._filterCountries("", true);
1066
939
  }
1067
940
  };
1068
- document.addEventListener("keydown", this._handleKeydownOnDropdown);
1069
- if (this.options.countrySearch) {
1070
- var doFilter = function doFilter() {
1071
- var inputQuery = _this8.searchInput.value.trim();
1072
- if (inputQuery) {
1073
- _this8._filterCountries(inputQuery);
1074
- } else {
1075
- _this8._filterCountries("", true);
1076
- }
1077
- };
1078
- var keyupTimer = null;
1079
- this._handleSearchChange = function() {
1080
- // filtering country nodes is expensive (lots of DOM manipulation), so rate limit it
1081
- if (keyupTimer) {
1082
- clearTimeout(keyupTimer);
1083
- }
1084
- keyupTimer = setTimeout(function() {
1085
- doFilter();
1086
- keyupTimer = null;
1087
- }, 100);
1088
- };
1089
- this.searchInput.addEventListener("input", this._handleSearchChange);
1090
- // stop propagation on search input click, so doesn't trigger click-off-to-close listener
1091
- this.searchInput.addEventListener("click", function(e) {
1092
- return e.stopPropagation();
1093
- });
1094
- }
1095
- }
1096
- }, {
1097
- key: "_normaliseString",
1098
- value: function _normaliseString() {
1099
- var s = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : "";
1100
- return s.normalize("NFD").replace(/[\u0300-\u036f]/g, "").toLowerCase();
1101
- }
1102
- }, {
1103
- key: "_filterCountries",
1104
- value: function _filterCountries(query) {
1105
- var isReset = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
1106
- var isFirst = true;
1107
- this.countryList.innerHTML = "";
1108
- var normalisedQuery = this._normaliseString(query);
1109
- for (var i = 0; i < this.countries.length; i++) {
1110
- var c = this.countries[i];
1111
- var normalisedCountryName = this._normaliseString(c.name);
1112
- var fullDialCode = "+".concat(c.dialCode);
1113
- if (isReset || normalisedCountryName.includes(normalisedQuery) || fullDialCode.includes(normalisedQuery) || c.iso2.includes(normalisedQuery)) {
1114
- this.countryList.appendChild(c.nodeById[this.id]);
1115
- // highlight the first item
1116
- if (isFirst) {
1117
- this._highlightListItem(c.nodeById[this.id], false);
1118
- isFirst = false;
1119
- }
941
+ let keyupTimer = null;
942
+ this._handleSearchChange = () => {
943
+ // filtering country nodes is expensive (lots of DOM manipulation), so rate limit it
944
+ if (keyupTimer) {
945
+ clearTimeout(keyupTimer);
1120
946
  }
1121
- }
1122
- // scroll to top (useful if user had previously scrolled down)
1123
- this.countryList.scrollTop = 0;
1124
- this._updateSearchResultsText();
1125
- }
1126
- }, {
1127
- key: "_updateSearchResultsText",
1128
- value: function _updateSearchResultsText() {
1129
- var i18n = this.options.i18n;
1130
- var count = this.countryList.childElementCount;
1131
- var searchText;
1132
- if (count === 0) {
1133
- searchText = i18n.zeroSearchResults || "No results found";
1134
- } else if (count === 1) {
1135
- searchText = i18n.oneSearchResult || "1 result found";
1136
- } else {
1137
- // eslint-disable-next-line no-template-curly-in-string
1138
- searchText = i18n.multipleSearchResults ? i18n.multipleSearchResults.replace("${count}", count) : "".concat(count, " results found");
1139
- }
1140
- this.searchResultsA11yText.textContent = searchText;
1141
- }
1142
- }, {
1143
- key: "_handleUpDownKey",
1144
- value: function _handleUpDownKey(key) {
1145
- var next = key === "ArrowUp" ? this.highlightedItem.previousElementSibling : this.highlightedItem.nextElementSibling;
1146
- if (next) {
1147
- // skip the divider
1148
- if (next.classList.contains("iti__divider")) {
1149
- next = key === "ArrowUp" ? next.previousElementSibling : next.nextElementSibling;
947
+ keyupTimer = setTimeout(() => {
948
+ doFilter();
949
+ keyupTimer = null;
950
+ }, 100);
951
+ };
952
+ this.searchInput.addEventListener("input", this._handleSearchChange);
953
+ // stop propagation on search input click, so doesn't trigger click-off-to-close listener
954
+ this.searchInput.addEventListener("click", e => e.stopPropagation());
955
+ }
956
+ }
957
+ // turns "Réunion" into "Reunion"
958
+ // from https://stackoverflow.com/a/37511463
959
+ _normaliseString(s = "") {
960
+ return s.normalize("NFD").replace(/[\u0300-\u036f]/g, "").toLowerCase();
961
+ }
962
+ _filterCountries(query, isReset = false) {
963
+ let isFirst = true;
964
+ this.countryList.innerHTML = "";
965
+ const normalisedQuery = this._normaliseString(query);
966
+ for (let i = 0; i < this.countries.length; i++) {
967
+ const c = this.countries[i];
968
+ const normalisedCountryName = this._normaliseString(c.name);
969
+ const fullDialCode = `+${c.dialCode}`;
970
+ if (isReset || normalisedCountryName.includes(normalisedQuery) || fullDialCode.includes(normalisedQuery) || c.iso2.includes(normalisedQuery)) {
971
+ this.countryList.appendChild(c.nodeById[this.id]);
972
+ // highlight the first item
973
+ if (isFirst) {
974
+ this._highlightListItem(c.nodeById[this.id], false);
975
+ isFirst = false;
1150
976
  }
1151
- } else if (this.countryList.childElementCount > 1) {
1152
- // otherwise, we must be at the end, so loop round again
1153
- next = key === "ArrowUp" ? this.countryList.lastElementChild : this.countryList.firstElementChild;
1154
- }
1155
- if (next) {
1156
- // make sure the next item is visible
1157
- // (before calling focus(), which can cause the next item to scroll to the middle of the dropdown, which is jarring)
1158
- this._scrollTo(next, false);
1159
- // if country search enabled, dont lose focus from the search input on up/down
1160
- var doFocus = !this.options.countrySearch;
1161
- this._highlightListItem(next, doFocus);
1162
977
  }
1163
978
  }
1164
- }, {
1165
- key: "_handleEnterKey",
1166
- value: function _handleEnterKey() {
1167
- if (this.highlightedItem) {
1168
- this._selectListItem(this.highlightedItem);
1169
- }
979
+ // scroll to top (useful if user had previously scrolled down)
980
+ this.countryList.scrollTop = 0;
981
+ this._updateSearchResultsText();
982
+ }
983
+ // update search results text (for a11y)
984
+ _updateSearchResultsText() {
985
+ const {
986
+ i18n
987
+ } = this.options;
988
+ const count = this.countryList.childElementCount;
989
+ let searchText;
990
+ if (count === 0) {
991
+ searchText = i18n.zeroSearchResults || "No results found";
992
+ } else if (count === 1) {
993
+ searchText = i18n.oneSearchResult || "1 result found";
994
+ } else {
995
+ // eslint-disable-next-line no-template-curly-in-string
996
+ searchText = i18n.multipleSearchResults ? i18n.multipleSearchResults.replace("${count}", count) : `${count} results found`;
997
+ }
998
+ this.searchResultsA11yText.textContent = searchText;
999
+ }
1000
+ // highlight the next/prev item in the list (and ensure it is visible)
1001
+ _handleUpDownKey(key) {
1002
+ let next = key === "ArrowUp" ? this.highlightedItem.previousElementSibling : this.highlightedItem.nextElementSibling;
1003
+ if (next) {
1004
+ // skip the divider
1005
+ if (next.classList.contains("iti__divider")) {
1006
+ next = key === "ArrowUp" ? next.previousElementSibling : next.nextElementSibling;
1007
+ }
1008
+ } else if (this.countryList.childElementCount > 1) {
1009
+ // otherwise, we must be at the end, so loop round again
1010
+ next = key === "ArrowUp" ? this.countryList.lastElementChild : this.countryList.firstElementChild;
1011
+ }
1012
+ if (next) {
1013
+ // make sure the next item is visible
1014
+ // (before calling focus(), which can cause the next item to scroll to the middle of the dropdown, which is jarring)
1015
+ this._scrollTo(next, false);
1016
+ // if country search enabled, dont lose focus from the search input on up/down
1017
+ const doFocus = !this.options.countrySearch;
1018
+ this._highlightListItem(next, doFocus);
1170
1019
  }
1171
- }, {
1172
- key: "_searchForCountry",
1173
- value: function _searchForCountry(query) {
1174
- for (var i = 0; i < this.countries.length; i++) {
1175
- if (this._startsWith(this.countries[i].name, query)) {
1176
- var listItem = this.countries[i].nodeById[this.id];
1177
- // update highlighting and scroll
1178
- this._highlightListItem(listItem, false);
1179
- this._scrollTo(listItem, true);
1180
- break;
1181
- }
1182
- }
1020
+ }
1021
+ // select the currently highlighted item
1022
+ _handleEnterKey() {
1023
+ if (this.highlightedItem) {
1024
+ this._selectListItem(this.highlightedItem);
1183
1025
  }
1184
- }, {
1185
- key: "_startsWith",
1186
- value: function _startsWith(a, b) {
1187
- return a.substr(0, b.length).toLowerCase() === b;
1188
- }
1189
- }, {
1190
- key: "_updateValFromNumber",
1191
- value: function _updateValFromNumber(fullNumber) {
1192
- var number = fullNumber;
1193
- if (this.options.formatOnDisplay && window.intlTelInputUtils && this.selectedCountryData) {
1194
- var useNational = this.options.nationalMode || number.charAt(0) !== "+" && !this.options.showSelectedDialCode;
1195
- var _intlTelInputUtils$nu = intlTelInputUtils.numberFormat, NATIONAL = _intlTelInputUtils$nu.NATIONAL, INTERNATIONAL = _intlTelInputUtils$nu.INTERNATIONAL;
1196
- var format = useNational ? NATIONAL : INTERNATIONAL;
1197
- number = intlTelInputUtils.formatNumber(number, this.selectedCountryData.iso2, format);
1198
- }
1199
- number = this._beforeSetNumber(number);
1200
- this.telInput.value = number;
1201
- }
1202
- }, {
1203
- key: "_updateFlagFromNumber",
1204
- value: function _updateFlagFromNumber(fullNumber) {
1205
- var plusIndex = fullNumber.indexOf("+");
1206
- // if it contains a plus, discard any chars before it e.g. accidental space char.
1207
- // this keeps the selected country auto-updating correctly, which we want as
1208
- // libphonenumber's validation/getNumber methods will ignore these chars anyway
1209
- var number = plusIndex ? fullNumber.substring(plusIndex) : fullNumber;
1210
- // if we already have US/Canada selected, make sure the number starts
1211
- // with a +1 so _getDialCode will be able to extract the area code
1212
- // update: if we dont yet have selectedCountryData, but we're here (trying to update the flag
1213
- // from the number), that means we're initialising the plugin with a number that already has a
1214
- // dial code, so fine to ignore this bit
1215
- var selectedDialCode = this.selectedCountryData.dialCode;
1216
- var isNanp = selectedDialCode === "1";
1217
- if (number && isNanp && number.charAt(0) !== "+") {
1218
- if (number.charAt(0) !== "1") {
1219
- number = "1".concat(number);
1220
- }
1221
- number = "+".concat(number);
1222
- }
1223
- // if showSelectedDialCode enabled, then consider the selected dial code to be part of the number
1224
- if (this.options.showSelectedDialCode && selectedDialCode && number.charAt(0) !== "+") {
1225
- number = "+".concat(selectedDialCode).concat(number);
1026
+ }
1027
+ // find the first list item whose name starts with the query string
1028
+ _searchForCountry(query) {
1029
+ for (let i = 0; i < this.countries.length; i++) {
1030
+ if (this._startsWith(this.countries[i].name, query)) {
1031
+ const listItem = this.countries[i].nodeById[this.id];
1032
+ // update highlighting and scroll
1033
+ this._highlightListItem(listItem, false);
1034
+ this._scrollTo(listItem, true);
1035
+ break;
1226
1036
  }
1227
- // try and extract valid dial code from input
1228
- var dialCode = this._getDialCode(number, true);
1229
- var numeric = this._getNumeric(number);
1230
- var iso2 = null;
1231
- if (dialCode) {
1232
- var iso2Codes = this.dialCodeToIso2Map[this._getNumeric(dialCode)];
1233
- // check if the right country is already selected. this should be false if the number is
1234
- // longer than the matched dial code because in this case we need to make sure that if
1235
- // there are multiple country matches, that the first one is selected (note: we could
1236
- // just check that here, but it requires the same loop that we already have later)
1237
- var alreadySelected = iso2Codes.indexOf(this.selectedCountryData.iso2) !== -1 && numeric.length <= dialCode.length - 1;
1238
- var isRegionlessNanpNumber = selectedDialCode === "1" && this._isRegionlessNanp(numeric);
1239
- // only update the flag if:
1240
- // A) NOT (we currently have a NANP flag selected, and the number is a regionlessNanp)
1241
- // AND
1242
- // B) the right country is not already selected
1243
- if (!isRegionlessNanpNumber && !alreadySelected) {
1244
- // if using onlyCountries option, iso2Codes[0] may be empty, so we must find the first
1245
- // non-empty index
1246
- for (var j = 0; j < iso2Codes.length; j++) {
1247
- if (iso2Codes[j]) {
1248
- iso2 = iso2Codes[j];
1249
- break;
1250
- }
1037
+ }
1038
+ }
1039
+ // check if string a starts with string b
1040
+ _startsWith(a, b) {
1041
+ return a.substr(0, b.length).toLowerCase() === b;
1042
+ }
1043
+ // update the input's value to the given val (format first if possible)
1044
+ // NOTE: this is called from _setInitialState, handleUtils and setNumber
1045
+ _updateValFromNumber(fullNumber) {
1046
+ let number = fullNumber;
1047
+ if (this.options.formatOnDisplay && window.intlTelInputUtils && this.selectedCountryData) {
1048
+ const useNational = this.options.nationalMode || number.charAt(0) !== "+" && !this.options.showSelectedDialCode;
1049
+ const {
1050
+ NATIONAL,
1051
+ INTERNATIONAL
1052
+ } = intlTelInputUtils.numberFormat;
1053
+ const format = useNational ? NATIONAL : INTERNATIONAL;
1054
+ number = intlTelInputUtils.formatNumber(number, this.selectedCountryData.iso2, format);
1055
+ }
1056
+ number = this._beforeSetNumber(number);
1057
+ this.telInput.value = number;
1058
+ }
1059
+ // check if need to select a new flag based on the given number
1060
+ // Note: called from _setInitialState, keyup handler, setNumber
1061
+ _updateFlagFromNumber(fullNumber) {
1062
+ const plusIndex = fullNumber.indexOf("+");
1063
+ // if it contains a plus, discard any chars before it e.g. accidental space char.
1064
+ // this keeps the selected country auto-updating correctly, which we want as
1065
+ // libphonenumber's validation/getNumber methods will ignore these chars anyway
1066
+ let number = plusIndex ? fullNumber.substring(plusIndex) : fullNumber;
1067
+ // if we already have US/Canada selected, make sure the number starts
1068
+ // with a +1 so _getDialCode will be able to extract the area code
1069
+ // update: if we dont yet have selectedCountryData, but we're here (trying to update the flag
1070
+ // from the number), that means we're initialising the plugin with a number that already has a
1071
+ // dial code, so fine to ignore this bit
1072
+ const selectedDialCode = this.selectedCountryData.dialCode;
1073
+ const isNanp = selectedDialCode === "1";
1074
+ if (number && isNanp && number.charAt(0) !== "+") {
1075
+ if (number.charAt(0) !== "1") {
1076
+ number = `1${number}`;
1077
+ }
1078
+ number = `+${number}`;
1079
+ }
1080
+ // if showSelectedDialCode enabled, then consider the selected dial code to be part of the number
1081
+ if (this.options.showSelectedDialCode && selectedDialCode && number.charAt(0) !== "+") {
1082
+ number = `+${selectedDialCode}${number}`;
1083
+ }
1084
+ // try and extract valid dial code from input
1085
+ const dialCode = this._getDialCode(number, true);
1086
+ const numeric = this._getNumeric(number);
1087
+ let iso2 = null;
1088
+ if (dialCode) {
1089
+ const iso2Codes = this.dialCodeToIso2Map[this._getNumeric(dialCode)];
1090
+ // check if the right country is already selected. this should be false if the number is
1091
+ // longer than the matched dial code because in this case we need to make sure that if
1092
+ // there are multiple country matches, that the first one is selected (note: we could
1093
+ // just check that here, but it requires the same loop that we already have later)
1094
+ const alreadySelected = iso2Codes.indexOf(this.selectedCountryData.iso2) !== -1 && numeric.length <= dialCode.length - 1;
1095
+ const isRegionlessNanpNumber = selectedDialCode === "1" && this._isRegionlessNanp(numeric);
1096
+ // only update the flag if:
1097
+ // A) NOT (we currently have a NANP flag selected, and the number is a regionlessNanp)
1098
+ // AND
1099
+ // B) the right country is not already selected
1100
+ if (!isRegionlessNanpNumber && !alreadySelected) {
1101
+ // if using onlyCountries option, iso2Codes[0] may be empty, so we must find the first
1102
+ // non-empty index
1103
+ for (let j = 0; j < iso2Codes.length; j++) {
1104
+ if (iso2Codes[j]) {
1105
+ iso2 = iso2Codes[j];
1106
+ break;
1251
1107
  }
1252
1108
  }
1253
- } else if (number.charAt(0) === "+" && numeric.length) {
1254
- // invalid dial code, so empty
1255
- // Note: use getNumeric here because the number has not been formatted yet, so could contain
1256
- // bad chars
1257
- iso2 = "";
1258
- } else if ((!number || number === "+") && !this.selectedCountryData.iso2) {
1259
- // if no selected flag, and user either clears the input, or just types a plus, then show default
1260
- iso2 = this.defaultCountry;
1261
1109
  }
1262
- if (iso2 !== null) {
1263
- return this._setCountry(iso2);
1110
+ } else if (number.charAt(0) === "+" && numeric.length) {
1111
+ // invalid dial code, so empty
1112
+ // Note: use getNumeric here because the number has not been formatted yet, so could contain
1113
+ // bad chars
1114
+ iso2 = "";
1115
+ } else if ((!number || number === "+") && !this.selectedCountryData.iso2) {
1116
+ // if no selected flag, and user either clears the input, or just types a plus, then show default
1117
+ iso2 = this.defaultCountry;
1118
+ }
1119
+ if (iso2 !== null) {
1120
+ return this._setCountry(iso2);
1121
+ }
1122
+ return false;
1123
+ }
1124
+ // check if the given number is a regionless NANP number (expects the number to contain an
1125
+ // international dial code)
1126
+ _isRegionlessNanp(number) {
1127
+ const numeric = this._getNumeric(number);
1128
+ if (numeric.charAt(0) === "1") {
1129
+ const areaCode = numeric.substr(1, 3);
1130
+ return regionlessNanpNumbers.indexOf(areaCode) !== -1;
1131
+ }
1132
+ return false;
1133
+ }
1134
+ // remove highlighting from other list items and highlight the given item
1135
+ _highlightListItem(listItem, shouldFocus) {
1136
+ const prevItem = this.highlightedItem;
1137
+ if (prevItem) {
1138
+ prevItem.classList.remove("iti__highlight");
1139
+ prevItem.setAttribute("aria-selected", "false");
1140
+ }
1141
+ this.highlightedItem = listItem;
1142
+ this.highlightedItem.classList.add("iti__highlight");
1143
+ this.highlightedItem.setAttribute("aria-selected", "true");
1144
+ this.selectedFlag.setAttribute("aria-activedescendant", listItem.getAttribute("id"));
1145
+ if (this.options.countrySearch) {
1146
+ this.searchInput.setAttribute("aria-activedescendant", listItem.getAttribute("id"));
1147
+ }
1148
+ if (shouldFocus) {
1149
+ this.highlightedItem.focus();
1150
+ }
1151
+ }
1152
+ // find the country data for the given iso2 code
1153
+ // the ignoreOnlyCountriesOption is only used during init() while parsing the onlyCountries array
1154
+ _getCountryData(iso2, allowFail) {
1155
+ for (let i = 0; i < this.countries.length; i++) {
1156
+ if (this.countries[i].iso2 === iso2) {
1157
+ return this.countries[i];
1264
1158
  }
1265
- return false;
1266
1159
  }
1267
- }, {
1268
- key: "_isRegionlessNanp",
1269
- value: function _isRegionlessNanp(number) {
1270
- var numeric = this._getNumeric(number);
1271
- if (numeric.charAt(0) === "1") {
1272
- var areaCode = numeric.substr(1, 3);
1273
- return regionlessNanpNumbers.indexOf(areaCode) !== -1;
1160
+ if (allowFail) {
1161
+ return null;
1162
+ }
1163
+ throw new Error(`No country data for '${iso2}'`);
1164
+ }
1165
+ // update the selected flag, dial code (if showSelectedDialCode), placeholder, title, and active list item
1166
+ // Note: called from _setInitialState, _updateFlagFromNumber, _selectListItem, setCountry
1167
+ _setCountry(iso2) {
1168
+ const {
1169
+ allowDropdown,
1170
+ showSelectedDialCode,
1171
+ showFlags,
1172
+ countrySearch,
1173
+ i18n
1174
+ } = this.options;
1175
+ const prevCountry = this.selectedCountryData.iso2 ? this.selectedCountryData : {};
1176
+ // do this first as it will throw an error and stop if iso2 is invalid
1177
+ this.selectedCountryData = iso2 ? this._getCountryData(iso2, false) : {};
1178
+ // update the defaultCountry - we only need the iso2 from now on, so just store that
1179
+ if (this.selectedCountryData.iso2) {
1180
+ this.defaultCountry = this.selectedCountryData.iso2;
1181
+ }
1182
+ // update the flag class and the a11y text
1183
+ if (this.selectedFlagInner) {
1184
+ let flagClass = "";
1185
+ let a11yText = "";
1186
+ if (iso2) {
1187
+ if (showFlags) {
1188
+ flagClass = `iti__flag iti__${iso2}`;
1189
+ a11yText = `${this.selectedCountryData.name} +${this.selectedCountryData.dialCode}`;
1190
+ }
1191
+ // if showFlags disabled and showSelectedDialCode is enabled, we dont show a flag or have any a11y text, as the displayed dial code is enough
1192
+ } else {
1193
+ flagClass = "iti__flag iti__globe";
1194
+ a11yText = i18n.noCountrySelected || "No country selected";
1195
+ }
1196
+ this.selectedFlagInner.className = flagClass;
1197
+ this.selectedFlagA11yText.textContent = a11yText;
1198
+ }
1199
+ this._setSelectedCountryFlagTitleAttribute(iso2, showSelectedDialCode);
1200
+ // update the selected dial code
1201
+ if (showSelectedDialCode) {
1202
+ const dialCode = this.selectedCountryData.dialCode ? `+${this.selectedCountryData.dialCode}` : "";
1203
+ this.selectedDialCode.innerHTML = dialCode;
1204
+ // offsetWidth is zero if input is in a hidden container during initialisation
1205
+ const selectedFlagWidth = this.selectedFlag.offsetWidth || this._getHiddenSelectedFlagWidth();
1206
+ // add 6px of padding after the grey selected-dial-code box, as this is what we use in the css
1207
+ if (this.isRTL) {
1208
+ this.telInput.style.paddingRight = `${selectedFlagWidth + 6}px`;
1209
+ } else {
1210
+ this.telInput.style.paddingLeft = `${selectedFlagWidth + 6}px`;
1274
1211
  }
1275
- return false;
1276
1212
  }
1277
- }, {
1278
- key: "_highlightListItem",
1279
- value: function _highlightListItem(listItem, shouldFocus) {
1280
- var prevItem = this.highlightedItem;
1213
+ // and the input's placeholder
1214
+ this._updatePlaceholder();
1215
+ // update the maximum valid number length
1216
+ this._updateMaxLength();
1217
+ // update the active list item (only if country search disabled, as country search doesn't store the active item)
1218
+ if (allowDropdown && !countrySearch) {
1219
+ const prevItem = this.activeItem;
1281
1220
  if (prevItem) {
1282
- prevItem.classList.remove("iti__highlight");
1221
+ prevItem.classList.remove("iti__active");
1283
1222
  prevItem.setAttribute("aria-selected", "false");
1284
1223
  }
1285
- this.highlightedItem = listItem;
1286
- this.highlightedItem.classList.add("iti__highlight");
1287
- this.highlightedItem.setAttribute("aria-selected", "true");
1288
- this.selectedFlag.setAttribute("aria-activedescendant", listItem.getAttribute("id"));
1289
- if (this.options.countrySearch) {
1290
- this.searchInput.setAttribute("aria-activedescendant", listItem.getAttribute("id"));
1291
- }
1292
- if (shouldFocus) {
1293
- this.highlightedItem.focus();
1224
+ if (iso2) {
1225
+ // check if there is a preferred item first, else fall back to standard
1226
+ const nextItem = this.countryList.querySelector(`#iti-${this.id}__item-${iso2}-preferred`) || this.countryList.querySelector(`#iti-${this.id}__item-${iso2}`);
1227
+ nextItem.setAttribute("aria-selected", "true");
1228
+ nextItem.classList.add("iti__active");
1229
+ this.activeItem = nextItem;
1294
1230
  }
1295
1231
  }
1296
- }, {
1297
- key: "_getCountryData",
1298
- value: function _getCountryData(iso2, allowFail) {
1299
- for (var i = 0; i < this.countries.length; i++) {
1300
- if (this.countries[i].iso2 === iso2) {
1301
- return this.countries[i];
1302
- }
1303
- }
1304
- if (allowFail) {
1305
- return null;
1306
- }
1307
- throw new Error("No country data for '".concat(iso2, "'"));
1308
- }
1309
- }, {
1310
- key: "_setCountry",
1311
- value: function _setCountry(iso2) {
1312
- var _this$options4 = this.options, allowDropdown = _this$options4.allowDropdown, showSelectedDialCode = _this$options4.showSelectedDialCode, showFlags = _this$options4.showFlags, countrySearch = _this$options4.countrySearch, i18n = _this$options4.i18n;
1313
- var prevCountry = this.selectedCountryData.iso2 ? this.selectedCountryData : {};
1314
- // do this first as it will throw an error and stop if iso2 is invalid
1315
- this.selectedCountryData = iso2 ? this._getCountryData(iso2, false) : {};
1316
- // update the defaultCountry - we only need the iso2 from now on, so just store that
1232
+ // return if the flag has changed or not
1233
+ return prevCountry.iso2 !== iso2;
1234
+ }
1235
+ // update the maximum valid number length for the currently selected country
1236
+ _updateMaxLength() {
1237
+ if (this.options.strictMode && window.intlTelInputUtils) {
1317
1238
  if (this.selectedCountryData.iso2) {
1318
- this.defaultCountry = this.selectedCountryData.iso2;
1319
- }
1320
- // update the flag class and the a11y text
1321
- if (this.selectedFlagInner) {
1322
- var flagClass = "";
1323
- var a11yText = "";
1324
- if (iso2) {
1325
- if (showFlags) {
1326
- flagClass = "iti__flag iti__".concat(iso2);
1327
- a11yText = "".concat(this.selectedCountryData.name, " +").concat(this.selectedCountryData.dialCode);
1328
- }
1329
- } else {
1330
- flagClass = "iti__flag iti__globe";
1331
- a11yText = i18n.noCountrySelected || "No country selected";
1239
+ const numberType = intlTelInputUtils.numberType[this.options.placeholderNumberType];
1240
+ let exampleNumber = intlTelInputUtils.getExampleNumber(this.selectedCountryData.iso2, null, numberType, true);
1241
+ // see if adding more digits is still valid to get the true maximum valid length
1242
+ let validNumber = exampleNumber;
1243
+ while (intlTelInputUtils.isPossibleNumber(exampleNumber, this.selectedCountryData.iso2)) {
1244
+ validNumber = exampleNumber;
1245
+ exampleNumber += "0";
1332
1246
  }
1333
- this.selectedFlagInner.className = flagClass;
1334
- this.selectedFlagA11yText.textContent = a11yText;
1335
- }
1336
- this._setSelectedCountryFlagTitleAttribute(iso2, showSelectedDialCode);
1337
- // update the selected dial code
1338
- if (showSelectedDialCode) {
1339
- var dialCode = this.selectedCountryData.dialCode ? "+".concat(this.selectedCountryData.dialCode) : "";
1340
- this.selectedDialCode.innerHTML = dialCode;
1341
- // offsetWidth is zero if input is in a hidden container during initialisation
1342
- var selectedFlagWidth = this.selectedFlag.offsetWidth || this._getHiddenSelectedFlagWidth();
1343
- // add 6px of padding after the grey selected-dial-code box, as this is what we use in the css
1344
- if (this.isRTL) {
1345
- this.telInput.style.paddingRight = "".concat(selectedFlagWidth + 6, "px");
1346
- } else {
1347
- this.telInput.style.paddingLeft = "".concat(selectedFlagWidth + 6, "px");
1348
- }
1349
- }
1350
- // and the input's placeholder
1351
- this._updatePlaceholder();
1352
- // update the maximum valid number length
1353
- this._updateMaxLength();
1354
- // update the active list item (only if country search disabled, as country search doesn't store the active item)
1355
- if (allowDropdown && !countrySearch) {
1356
- var prevItem = this.activeItem;
1357
- if (prevItem) {
1358
- prevItem.classList.remove("iti__active");
1359
- prevItem.setAttribute("aria-selected", "false");
1360
- }
1361
- if (iso2) {
1362
- // check if there is a preferred item first, else fall back to standard
1363
- var nextItem = this.countryList.querySelector("#iti-".concat(this.id, "__item-").concat(iso2, "-preferred")) || this.countryList.querySelector("#iti-".concat(this.id, "__item-").concat(iso2));
1364
- nextItem.setAttribute("aria-selected", "true");
1365
- nextItem.classList.add("iti__active");
1366
- this.activeItem = nextItem;
1367
- }
1368
- }
1369
- // return if the flag has changed or not
1370
- return prevCountry.iso2 !== iso2;
1371
- }
1372
- }, {
1373
- key: "_updateMaxLength",
1374
- value: function _updateMaxLength() {
1375
- if (this.options.strictMode && window.intlTelInputUtils) {
1376
- if (this.selectedCountryData.iso2) {
1377
- var numberType = intlTelInputUtils.numberType[this.options.placeholderNumberType];
1378
- var exampleNumber = intlTelInputUtils.getExampleNumber(this.selectedCountryData.iso2, null, numberType, true);
1379
- // see if adding more digits is still valid to get the true maximum valid length
1380
- var validNumber = exampleNumber;
1381
- while (intlTelInputUtils.isPossibleNumber(exampleNumber, this.selectedCountryData.iso2)) {
1382
- validNumber = exampleNumber;
1383
- exampleNumber += "0";
1384
- }
1385
- var coreNumber = intlTelInputUtils.getCoreNumber(validNumber, this.selectedCountryData.iso2);
1386
- this.maxCoreNumberLength = coreNumber.length;
1387
- } else {
1388
- this.maxCoreNumberLength = null;
1389
- }
1390
- }
1391
- }
1392
- }, {
1393
- key: "_setSelectedCountryFlagTitleAttribute",
1394
- value: function _setSelectedCountryFlagTitleAttribute(iso2, showSelectedDialCode) {
1395
- if (!this.selectedFlag) {
1396
- return;
1397
- }
1398
- var title;
1399
- if (iso2 && !showSelectedDialCode) {
1400
- title = "".concat(this.selectedCountryData.name, ": +").concat(this.selectedCountryData.dialCode);
1401
- } else if (iso2) {
1402
- // For screen reader output, we don't want to include the dial code in the reader output twice
1403
- // so just use the selected country name here:
1404
- title = this.selectedCountryData.name;
1247
+ const coreNumber = intlTelInputUtils.getCoreNumber(validNumber, this.selectedCountryData.iso2);
1248
+ this.maxCoreNumberLength = coreNumber.length;
1405
1249
  } else {
1406
- title = "Unknown";
1407
- }
1408
- this.selectedFlag.setAttribute("title", title);
1409
- }
1410
- }, {
1411
- key: "_getHiddenSelectedFlagWidth",
1412
- value: function _getHiddenSelectedFlagWidth() {
1413
- // to get the right styling to apply, all we need is a shallow clone of the container,
1414
- // and then to inject a deep clone of the selectedFlag element
1415
- var containerClone = this.telInput.parentNode.cloneNode();
1416
- containerClone.style.visibility = "hidden";
1417
- document.body.appendChild(containerClone);
1418
- var flagsContainerClone = this.flagsContainer.cloneNode();
1419
- containerClone.appendChild(flagsContainerClone);
1420
- var selectedFlagClone = this.selectedFlag.cloneNode(true);
1421
- flagsContainerClone.appendChild(selectedFlagClone);
1422
- var width = selectedFlagClone.offsetWidth;
1423
- containerClone.parentNode.removeChild(containerClone);
1424
- return width;
1425
- }
1426
- }, {
1427
- key: "_updatePlaceholder",
1428
- value: function _updatePlaceholder() {
1429
- var _this$options5 = this.options, autoPlaceholder = _this$options5.autoPlaceholder, placeholderNumberType = _this$options5.placeholderNumberType, nationalMode = _this$options5.nationalMode, customPlaceholder = _this$options5.customPlaceholder;
1430
- var shouldSetPlaceholder = autoPlaceholder === "aggressive" || !this.hadInitialPlaceholder && autoPlaceholder === "polite";
1431
- if (window.intlTelInputUtils && shouldSetPlaceholder) {
1432
- var numberType = intlTelInputUtils.numberType[placeholderNumberType];
1433
- // note: must set placeholder to empty string if no country selected (globe icon showing)
1434
- var placeholder = this.selectedCountryData.iso2 ? intlTelInputUtils.getExampleNumber(this.selectedCountryData.iso2, nationalMode, numberType) : "";
1435
- placeholder = this._beforeSetNumber(placeholder);
1436
- if (typeof customPlaceholder === "function") {
1437
- placeholder = customPlaceholder(placeholder, this.selectedCountryData);
1438
- }
1439
- this.telInput.setAttribute("placeholder", placeholder);
1250
+ this.maxCoreNumberLength = null;
1440
1251
  }
1441
1252
  }
1442
- }, {
1443
- key: "_selectListItem",
1444
- value: function _selectListItem(listItem) {
1445
- // update selected flag and active list item
1446
- var flagChanged = this._setCountry(listItem.getAttribute("data-country-code"));
1447
- this._closeDropdown();
1448
- this._updateDialCode(listItem.getAttribute("data-dial-code"));
1449
- // focus the input
1450
- this.telInput.focus();
1451
- if (flagChanged) {
1452
- this._triggerCountryChange();
1453
- }
1253
+ }
1254
+ _setSelectedCountryFlagTitleAttribute(iso2, showSelectedDialCode) {
1255
+ if (!this.selectedFlag) {
1256
+ return;
1257
+ }
1258
+ let title;
1259
+ if (iso2 && !showSelectedDialCode) {
1260
+ title = `${this.selectedCountryData.name}: +${this.selectedCountryData.dialCode}`;
1261
+ } else if (iso2) {
1262
+ // For screen reader output, we don't want to include the dial code in the reader output twice
1263
+ // so just use the selected country name here:
1264
+ title = this.selectedCountryData.name;
1265
+ } else {
1266
+ title = "Unknown";
1267
+ }
1268
+ this.selectedFlag.setAttribute("title", title);
1269
+ }
1270
+ // when the input is in a hidden container during initialisation, we must inject some markup
1271
+ // into the end of the DOM to calculate the correct offsetWidth
1272
+ // NOTE: this is only used when showSelectedDialCode is enabled, so flagsContainer and selectedFlag
1273
+ // will definitely exist
1274
+ _getHiddenSelectedFlagWidth() {
1275
+ // to get the right styling to apply, all we need is a shallow clone of the container,
1276
+ // and then to inject a deep clone of the selectedFlag element
1277
+ const containerClone = this.telInput.parentNode.cloneNode();
1278
+ containerClone.style.visibility = "hidden";
1279
+ document.body.appendChild(containerClone);
1280
+ const flagsContainerClone = this.flagsContainer.cloneNode();
1281
+ containerClone.appendChild(flagsContainerClone);
1282
+ const selectedFlagClone = this.selectedFlag.cloneNode(true);
1283
+ flagsContainerClone.appendChild(selectedFlagClone);
1284
+ const width = selectedFlagClone.offsetWidth;
1285
+ containerClone.parentNode.removeChild(containerClone);
1286
+ return width;
1287
+ }
1288
+ // update the input placeholder to an example number from the currently selected country
1289
+ _updatePlaceholder() {
1290
+ const {
1291
+ autoPlaceholder,
1292
+ placeholderNumberType,
1293
+ nationalMode,
1294
+ customPlaceholder
1295
+ } = this.options;
1296
+ const shouldSetPlaceholder = autoPlaceholder === "aggressive" || !this.hadInitialPlaceholder && autoPlaceholder === "polite";
1297
+ if (window.intlTelInputUtils && shouldSetPlaceholder) {
1298
+ const numberType = intlTelInputUtils.numberType[placeholderNumberType];
1299
+ // note: must set placeholder to empty string if no country selected (globe icon showing)
1300
+ let placeholder = this.selectedCountryData.iso2 ? intlTelInputUtils.getExampleNumber(this.selectedCountryData.iso2, nationalMode, numberType) : "";
1301
+ placeholder = this._beforeSetNumber(placeholder);
1302
+ if (typeof customPlaceholder === "function") {
1303
+ placeholder = customPlaceholder(placeholder, this.selectedCountryData);
1304
+ }
1305
+ this.telInput.setAttribute("placeholder", placeholder);
1454
1306
  }
1455
- }, {
1456
- key: "_closeDropdown",
1457
- value: function _closeDropdown() {
1458
- this.dropdownContent.classList.add("iti__hide");
1459
- this.selectedFlag.setAttribute("aria-expanded", "false");
1460
- this.selectedFlag.removeAttribute("aria-activedescendant");
1461
- if (this.highlightedItem) {
1462
- this.highlightedItem.setAttribute("aria-selected", "false");
1463
- }
1464
- if (this.options.countrySearch) {
1465
- this.searchInput.removeAttribute("aria-activedescendant");
1307
+ }
1308
+ // called when the user selects a list item from the dropdown
1309
+ _selectListItem(listItem) {
1310
+ // update selected flag and active list item
1311
+ const flagChanged = this._setCountry(listItem.getAttribute("data-country-code"));
1312
+ this._closeDropdown();
1313
+ this._updateDialCode(listItem.getAttribute("data-dial-code"));
1314
+ // focus the input
1315
+ this.telInput.focus();
1316
+ if (flagChanged) {
1317
+ this._triggerCountryChange();
1318
+ }
1319
+ }
1320
+ // close the dropdown and unbind any listeners
1321
+ _closeDropdown() {
1322
+ this.dropdownContent.classList.add("iti__hide");
1323
+ this.selectedFlag.setAttribute("aria-expanded", "false");
1324
+ this.selectedFlag.removeAttribute("aria-activedescendant");
1325
+ if (this.highlightedItem) {
1326
+ this.highlightedItem.setAttribute("aria-selected", "false");
1327
+ }
1328
+ if (this.options.countrySearch) {
1329
+ this.searchInput.removeAttribute("aria-activedescendant");
1330
+ }
1331
+ // update the arrow
1332
+ this.dropdownArrow.classList.remove("iti__arrow--up");
1333
+ // unbind key events
1334
+ document.removeEventListener("keydown", this._handleKeydownOnDropdown);
1335
+ if (this.options.countrySearch) {
1336
+ this.searchInput.removeEventListener("input", this._handleSearchChange);
1337
+ }
1338
+ document.documentElement.removeEventListener("click", this._handleClickOffToClose);
1339
+ this.countryList.removeEventListener("mouseover", this._handleMouseoverCountryList);
1340
+ this.countryList.removeEventListener("click", this._handleClickCountryList);
1341
+ // remove menu from container
1342
+ if (this.options.dropdownContainer) {
1343
+ if (!this.options.useFullscreenPopup) {
1344
+ window.removeEventListener("scroll", this._handleWindowScroll);
1466
1345
  }
1467
- // update the arrow
1468
- this.dropdownArrow.classList.remove("iti__arrow--up");
1469
- // unbind key events
1470
- document.removeEventListener("keydown", this._handleKeydownOnDropdown);
1471
- if (this.options.countrySearch) {
1472
- this.searchInput.removeEventListener("input", this._handleSearchChange);
1346
+ if (this.dropdown.parentNode) {
1347
+ this.dropdown.parentNode.removeChild(this.dropdown);
1473
1348
  }
1474
- document.documentElement.removeEventListener("click", this._handleClickOffToClose);
1475
- this.countryList.removeEventListener("mouseover", this._handleMouseoverCountryList);
1476
- this.countryList.removeEventListener("click", this._handleClickCountryList);
1477
- // remove menu from container
1478
- if (this.options.dropdownContainer) {
1479
- if (!this.options.useFullscreenPopup) {
1480
- window.removeEventListener("scroll", this._handleWindowScroll);
1481
- }
1482
- if (this.dropdown.parentNode) {
1483
- this.dropdown.parentNode.removeChild(this.dropdown);
1484
- }
1485
- }
1486
- this._trigger("close:countrydropdown");
1487
1349
  }
1488
- }, {
1489
- key: "_scrollTo",
1490
- value: function _scrollTo(element, middle) {
1491
- var container = this.countryList;
1492
- // windowTop from https://stackoverflow.com/a/14384091/217866
1493
- var windowTop = document.documentElement.scrollTop;
1494
- var containerHeight = container.offsetHeight;
1495
- var containerTop = container.getBoundingClientRect().top + windowTop;
1496
- var containerBottom = containerTop + containerHeight;
1497
- var elementHeight = element.offsetHeight;
1498
- var elementTop = element.getBoundingClientRect().top + windowTop;
1499
- var elementBottom = elementTop + elementHeight;
1500
- var newScrollTop = elementTop - containerTop + container.scrollTop;
1501
- var middleOffset = containerHeight / 2 - elementHeight / 2;
1502
- if (elementTop < containerTop) {
1503
- // scroll up
1504
- if (middle) {
1505
- newScrollTop -= middleOffset;
1506
- }
1507
- container.scrollTop = newScrollTop;
1508
- } else if (elementBottom > containerBottom) {
1509
- // scroll down
1510
- if (middle) {
1511
- newScrollTop += middleOffset;
1512
- }
1513
- var heightDifference = containerHeight - elementHeight;
1514
- container.scrollTop = newScrollTop - heightDifference;
1515
- }
1350
+ this._trigger("close:countrydropdown");
1351
+ }
1352
+ // check if an element is visible within it's container, else scroll until it is
1353
+ _scrollTo(element, middle) {
1354
+ const container = this.countryList;
1355
+ // windowTop from https://stackoverflow.com/a/14384091/217866
1356
+ const windowTop = document.documentElement.scrollTop;
1357
+ const containerHeight = container.offsetHeight;
1358
+ const containerTop = container.getBoundingClientRect().top + windowTop;
1359
+ const containerBottom = containerTop + containerHeight;
1360
+ const elementHeight = element.offsetHeight;
1361
+ const elementTop = element.getBoundingClientRect().top + windowTop;
1362
+ const elementBottom = elementTop + elementHeight;
1363
+ let newScrollTop = elementTop - containerTop + container.scrollTop;
1364
+ const middleOffset = containerHeight / 2 - elementHeight / 2;
1365
+ if (elementTop < containerTop) {
1366
+ // scroll up
1367
+ if (middle) {
1368
+ newScrollTop -= middleOffset;
1369
+ }
1370
+ container.scrollTop = newScrollTop;
1371
+ } else if (elementBottom > containerBottom) {
1372
+ // scroll down
1373
+ if (middle) {
1374
+ newScrollTop += middleOffset;
1375
+ }
1376
+ const heightDifference = containerHeight - elementHeight;
1377
+ container.scrollTop = newScrollTop - heightDifference;
1516
1378
  }
1517
- }, {
1518
- key: "_updateDialCode",
1519
- value: function _updateDialCode(newDialCodeBare) {
1520
- var inputVal = this.telInput.value;
1521
- // save having to pass this every time
1522
- var newDialCode = "+".concat(newDialCodeBare);
1523
- var newNumber;
1524
- if (inputVal.charAt(0) === "+") {
1525
- // there's a plus so we're dealing with a replacement
1526
- var prevDialCode = this._getDialCode(inputVal);
1527
- if (prevDialCode) {
1528
- // current number contains a valid dial code, so replace it
1529
- newNumber = inputVal.replace(prevDialCode, newDialCode);
1530
- } else {
1531
- // current number contains an invalid dial code, so ditch it
1532
- // (no way to determine where the invalid dial code ends and the rest of the number begins)
1533
- newNumber = newDialCode;
1534
- }
1535
- this.telInput.value = newNumber;
1379
+ }
1380
+ // replace any existing dial code with the new one
1381
+ // Note: called from _selectListItem and setCountry
1382
+ _updateDialCode(newDialCodeBare) {
1383
+ const inputVal = this.telInput.value;
1384
+ // save having to pass this every time
1385
+ const newDialCode = `+${newDialCodeBare}`;
1386
+ let newNumber;
1387
+ if (inputVal.charAt(0) === "+") {
1388
+ // there's a plus so we're dealing with a replacement
1389
+ const prevDialCode = this._getDialCode(inputVal);
1390
+ if (prevDialCode) {
1391
+ // current number contains a valid dial code, so replace it
1392
+ newNumber = inputVal.replace(prevDialCode, newDialCode);
1393
+ } else {
1394
+ // current number contains an invalid dial code, so ditch it
1395
+ // (no way to determine where the invalid dial code ends and the rest of the number begins)
1396
+ newNumber = newDialCode;
1536
1397
  }
1398
+ this.telInput.value = newNumber;
1537
1399
  }
1538
- }, {
1539
- key: "_getDialCode",
1540
- value: function _getDialCode(number, includeAreaCode) {
1541
- var dialCode = "";
1542
- // only interested in international numbers (starting with a plus)
1543
- if (number.charAt(0) === "+") {
1544
- var numericChars = "";
1545
- // iterate over chars
1546
- for (var i = 0; i < number.length; i++) {
1547
- var c = number.charAt(i);
1548
- // if char is number (https://stackoverflow.com/a/8935649/217866)
1549
- if (!isNaN(parseInt(c, 10))) {
1550
- numericChars += c;
1551
- // if current numericChars make a valid dial code
1552
- if (includeAreaCode) {
1553
- if (this.dialCodeToIso2Map[numericChars]) {
1554
- // store the actual raw string (useful for matching later)
1555
- dialCode = number.substr(0, i + 1);
1556
- }
1557
- } else {
1558
- if (this.dialCodes[numericChars]) {
1559
- dialCode = number.substr(0, i + 1);
1560
- // if we're just looking for a dial code, we can break as soon as we find one
1561
- break;
1562
- }
1400
+ }
1401
+ // try and extract a valid international dial code from a full telephone number
1402
+ // Note: returns the raw string inc plus character and any whitespace/dots etc
1403
+ _getDialCode(number, includeAreaCode) {
1404
+ let dialCode = "";
1405
+ // only interested in international numbers (starting with a plus)
1406
+ if (number.charAt(0) === "+") {
1407
+ let numericChars = "";
1408
+ // iterate over chars
1409
+ for (let i = 0; i < number.length; i++) {
1410
+ const c = number.charAt(i);
1411
+ // if char is number (https://stackoverflow.com/a/8935649/217866)
1412
+ if (!isNaN(parseInt(c, 10))) {
1413
+ numericChars += c;
1414
+ // if current numericChars make a valid dial code
1415
+ if (includeAreaCode) {
1416
+ if (this.dialCodeToIso2Map[numericChars]) {
1417
+ // store the actual raw string (useful for matching later)
1418
+ dialCode = number.substr(0, i + 1);
1563
1419
  }
1564
- // stop searching as soon as we can - in this case when we hit max len
1565
- if (numericChars.length === this.dialCodeMaxLen) {
1420
+ } else {
1421
+ if (this.dialCodes[numericChars]) {
1422
+ dialCode = number.substr(0, i + 1);
1423
+ // if we're just looking for a dial code, we can break as soon as we find one
1566
1424
  break;
1567
1425
  }
1568
1426
  }
1427
+ // stop searching as soon as we can - in this case when we hit max len
1428
+ if (numericChars.length === this.dialCodeMaxLen) {
1429
+ break;
1430
+ }
1569
1431
  }
1570
1432
  }
1571
- return dialCode;
1572
- }
1573
- }, {
1574
- key: "_getFullNumber",
1575
- value: function _getFullNumber() {
1576
- var val = this.telInput.value.trim();
1577
- var dialCode = this.selectedCountryData.dialCode;
1578
- var prefix;
1579
- var numericVal = this._getNumeric(val);
1580
- if (this.options.showSelectedDialCode && !this.options.nationalMode && val.charAt(0) !== "+" && dialCode && numericVal) {
1581
- // when using showSelectedDialCode, it is visible so is effectively part of the typed number
1582
- prefix = "+".concat(dialCode);
1583
- } else {
1584
- prefix = "";
1585
- }
1586
- return prefix + val;
1587
- }
1588
- }, {
1589
- key: "_beforeSetNumber",
1590
- value: function _beforeSetNumber(fullNumber) {
1591
- var number = fullNumber;
1592
- if (this.options.showSelectedDialCode) {
1593
- var dialCode = this._getDialCode(number);
1594
- // if there is a valid dial code
1595
- if (dialCode) {
1596
- // in case _getDialCode returned an area code as well
1597
- dialCode = "+".concat(this.selectedCountryData.dialCode);
1598
- // a lot of numbers will have a space separating the dial code and the main number, and
1599
- // some NANP numbers will have a hyphen e.g. +1 684-733-1234 - in both cases we want to get
1600
- // rid of it
1601
- // NOTE: don't just trim all non-numerics as may want to preserve an open parenthesis etc
1602
- var start = number[dialCode.length] === " " || number[dialCode.length] === "-" ? dialCode.length + 1 : dialCode.length;
1603
- number = number.substr(start);
1604
- }
1605
- }
1606
- return this._cap(number);
1607
- }
1608
- }, {
1609
- key: "_triggerCountryChange",
1610
- value: function _triggerCountryChange() {
1611
- this._trigger("countrychange");
1612
- }
1613
- }, {
1614
- key: "_formatNumberAsYouType",
1615
- value: function _formatNumberAsYouType() {
1616
- var val = this._getFullNumber();
1617
- var result = window.intlTelInputUtils ? intlTelInputUtils.formatNumberAsYouType(val, this.selectedCountryData.iso2) : val;
1618
- // if showSelectedDialCode and they haven't (re)typed the dial code in the input as well, then remove the dial code
1619
- var dialCode = this.selectedCountryData.dialCode;
1620
- if (this.options.showSelectedDialCode && !this.options.nationalMode && this.telInput.value.charAt(0) !== "+" && result.includes("+".concat(dialCode))) {
1621
- var afterDialCode = result.split("+".concat(dialCode))[1] || "";
1622
- return afterDialCode.trim();
1623
- }
1624
- return result;
1625
- }
1626
- }, {
1627
- key: "handleAutoCountry",
1628
- value: function handleAutoCountry() {
1629
- if (this.options.initialCountry === "auto") {
1630
- // we must set this even if there is an initial val in the input: in case the initial val is
1631
- // invalid and they delete it - they should see their auto country
1632
- this.defaultCountry = window.intlTelInputGlobals.autoCountry;
1633
- // if there's no initial value in the input, then update the flag
1634
- if (!this.telInput.value) {
1635
- this.setCountry(this.defaultCountry);
1636
- }
1637
- this.resolveAutoCountryPromise();
1638
- }
1639
1433
  }
1640
- }, {
1641
- key: "handleUtils",
1642
- value: function handleUtils() {
1643
- // if the request was successful
1644
- if (window.intlTelInputUtils) {
1645
- // if there's an initial value in the input, then format it
1646
- if (this.telInput.value) {
1647
- this._updateValFromNumber(this.telInput.value);
1648
- }
1649
- if (this.selectedCountryData.iso2) {
1650
- this._updatePlaceholder();
1651
- this._updateMaxLength();
1652
- }
1434
+ return dialCode;
1435
+ }
1436
+ // get the input val, adding the dial code if showSelectedDialCode is enabled
1437
+ _getFullNumber() {
1438
+ const val = this.telInput.value.trim();
1439
+ const {
1440
+ dialCode
1441
+ } = this.selectedCountryData;
1442
+ let prefix;
1443
+ const numericVal = this._getNumeric(val);
1444
+ if (this.options.showSelectedDialCode && !this.options.nationalMode && val.charAt(0) !== "+" && dialCode && numericVal) {
1445
+ // when using showSelectedDialCode, it is visible so is effectively part of the typed number
1446
+ prefix = `+${dialCode}`;
1447
+ } else {
1448
+ prefix = "";
1449
+ }
1450
+ return prefix + val;
1451
+ }
1452
+ // remove the dial code if showSelectedDialCode is enabled
1453
+ // also cap the length if the input has a maxlength attribute
1454
+ _beforeSetNumber(fullNumber) {
1455
+ let number = fullNumber;
1456
+ if (this.options.showSelectedDialCode) {
1457
+ let dialCode = this._getDialCode(number);
1458
+ // if there is a valid dial code
1459
+ if (dialCode) {
1460
+ // in case _getDialCode returned an area code as well
1461
+ dialCode = `+${this.selectedCountryData.dialCode}`;
1462
+ // a lot of numbers will have a space separating the dial code and the main number, and
1463
+ // some NANP numbers will have a hyphen e.g. +1 684-733-1234 - in both cases we want to get
1464
+ // rid of it
1465
+ // NOTE: don't just trim all non-numerics as may want to preserve an open parenthesis etc
1466
+ const start = number[dialCode.length] === " " || number[dialCode.length] === "-" ? dialCode.length + 1 : dialCode.length;
1467
+ number = number.substr(start);
1653
1468
  }
1654
- this.resolveUtilsScriptPromise();
1655
1469
  }
1656
- }, {
1657
- key: "destroy",
1658
- value: function destroy() {
1659
- if (this.options.allowDropdown) {
1660
- // make sure the dropdown is closed (and unbind listeners)
1661
- this._closeDropdown();
1662
- this.selectedFlag.removeEventListener("click", this._handleClickSelectedFlag);
1663
- this.flagsContainer.removeEventListener("keydown", this._handleFlagsContainerKeydown);
1664
- // label click hack
1665
- var label = this.telInput.closest("label");
1666
- if (label) {
1667
- label.removeEventListener("click", this._handleLabelClick);
1668
- }
1669
- }
1670
- // unbind hiddenInput listeners
1671
- var form = this.telInput.form;
1672
- if (this._handleHiddenInputSubmit && form) {
1673
- form.removeEventListener("submit", this._handleHiddenInputSubmit);
1674
- }
1675
- // unbind key events, and cut/paste events
1676
- this.telInput.removeEventListener("input", this._handleInputEvent);
1677
- if (this._handleKeydownEvent) {
1678
- this.telInput.removeEventListener("keydown", this._handleKeydownEvent);
1679
- }
1680
- // remove attribute of id instance: data-intl-tel-input-id
1681
- this.telInput.removeAttribute("data-intl-tel-input-id");
1682
- // remove markup (but leave the original input)
1683
- var wrapper = this.telInput.parentNode;
1684
- wrapper.parentNode.insertBefore(this.telInput, wrapper);
1685
- wrapper.parentNode.removeChild(wrapper);
1686
- delete window.intlTelInputGlobals.instances[this.id];
1687
- }
1688
- }, {
1689
- key: "getExtension",
1690
- value: function getExtension() {
1691
- if (window.intlTelInputUtils) {
1692
- return intlTelInputUtils.getExtension(this._getFullNumber(), this.selectedCountryData.iso2);
1693
- }
1694
- return "";
1695
- }
1696
- }, {
1697
- key: "getNumber",
1698
- value: function getNumber(format) {
1699
- if (window.intlTelInputUtils) {
1700
- var iso2 = this.selectedCountryData.iso2;
1701
- return intlTelInputUtils.formatNumber(this._getFullNumber(), iso2, format);
1702
- }
1703
- return "";
1470
+ return this._cap(number);
1471
+ }
1472
+ // trigger the 'countrychange' event
1473
+ _triggerCountryChange() {
1474
+ this._trigger("countrychange");
1475
+ }
1476
+ // format the number as the user types
1477
+ _formatNumberAsYouType() {
1478
+ const val = this._getFullNumber();
1479
+ const result = window.intlTelInputUtils ? intlTelInputUtils.formatNumberAsYouType(val, this.selectedCountryData.iso2) : val;
1480
+ // if showSelectedDialCode and they haven't (re)typed the dial code in the input as well, then remove the dial code
1481
+ const {
1482
+ dialCode
1483
+ } = this.selectedCountryData;
1484
+ if (this.options.showSelectedDialCode && !this.options.nationalMode && this.telInput.value.charAt(0) !== "+" && result.includes(`+${dialCode}`)) {
1485
+ const afterDialCode = result.split(`+${dialCode}`)[1] || "";
1486
+ return afterDialCode.trim();
1487
+ }
1488
+ return result;
1489
+ }
1490
+ /**************************
1491
+ * SECRET PUBLIC METHODS
1492
+ **************************/
1493
+ // this is called when the geoip call returns
1494
+ handleAutoCountry() {
1495
+ if (this.options.initialCountry === "auto") {
1496
+ // we must set this even if there is an initial val in the input: in case the initial val is
1497
+ // invalid and they delete it - they should see their auto country
1498
+ this.defaultCountry = window.intlTelInputGlobals.autoCountry;
1499
+ // if there's no initial value in the input, then update the flag
1500
+ if (!this.telInput.value) {
1501
+ this.setCountry(this.defaultCountry);
1502
+ }
1503
+ this.resolveAutoCountryPromise();
1704
1504
  }
1705
- }, {
1706
- key: "getNumberType",
1707
- value: function getNumberType() {
1708
- if (window.intlTelInputUtils) {
1709
- return intlTelInputUtils.getNumberType(this._getFullNumber(), this.selectedCountryData.iso2);
1710
- }
1711
- return -99;
1712
- }
1713
- }, {
1714
- key: "getSelectedCountryData",
1715
- value: function getSelectedCountryData() {
1716
- return this.selectedCountryData;
1717
- }
1718
- }, {
1719
- key: "getValidationError",
1720
- value: function getValidationError() {
1721
- if (window.intlTelInputUtils) {
1722
- var iso2 = this.selectedCountryData.iso2;
1723
- return intlTelInputUtils.getValidationError(this._getFullNumber(), iso2);
1724
- }
1725
- return -99;
1726
- }
1727
- }, {
1728
- key: "isValidNumber",
1729
- value: function isValidNumber() {
1730
- var mobileOnly = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : true;
1731
- var val = this._getFullNumber();
1732
- // return false for any alpha chars
1733
- if (/(?:[A-Za-z\xAA\xB5\xBA\xC0-\xD6\xD8-\xF6\xF8-\u02C1\u02C6-\u02D1\u02E0-\u02E4\u02EC\u02EE\u0370-\u0374\u0376\u0377\u037A-\u037D\u037F\u0386\u0388-\u038A\u038C\u038E-\u03A1\u03A3-\u03F5\u03F7-\u0481\u048A-\u052F\u0531-\u0556\u0559\u0560-\u0588\u05D0-\u05EA\u05EF-\u05F2\u0620-\u064A\u066E\u066F\u0671-\u06D3\u06D5\u06E5\u06E6\u06EE\u06EF\u06FA-\u06FC\u06FF\u0710\u0712-\u072F\u074D-\u07A5\u07B1\u07CA-\u07EA\u07F4\u07F5\u07FA\u0800-\u0815\u081A\u0824\u0828\u0840-\u0858\u0860-\u086A\u0870-\u0887\u0889-\u088E\u08A0-\u08C9\u0904-\u0939\u093D\u0950\u0958-\u0961\u0971-\u0980\u0985-\u098C\u098F\u0990\u0993-\u09A8\u09AA-\u09B0\u09B2\u09B6-\u09B9\u09BD\u09CE\u09DC\u09DD\u09DF-\u09E1\u09F0\u09F1\u09FC\u0A05-\u0A0A\u0A0F\u0A10\u0A13-\u0A28\u0A2A-\u0A30\u0A32\u0A33\u0A35\u0A36\u0A38\u0A39\u0A59-\u0A5C\u0A5E\u0A72-\u0A74\u0A85-\u0A8D\u0A8F-\u0A91\u0A93-\u0AA8\u0AAA-\u0AB0\u0AB2\u0AB3\u0AB5-\u0AB9\u0ABD\u0AD0\u0AE0\u0AE1\u0AF9\u0B05-\u0B0C\u0B0F\u0B10\u0B13-\u0B28\u0B2A-\u0B30\u0B32\u0B33\u0B35-\u0B39\u0B3D\u0B5C\u0B5D\u0B5F-\u0B61\u0B71\u0B83\u0B85-\u0B8A\u0B8E-\u0B90\u0B92-\u0B95\u0B99\u0B9A\u0B9C\u0B9E\u0B9F\u0BA3\u0BA4\u0BA8-\u0BAA\u0BAE-\u0BB9\u0BD0\u0C05-\u0C0C\u0C0E-\u0C10\u0C12-\u0C28\u0C2A-\u0C39\u0C3D\u0C58-\u0C5A\u0C5D\u0C60\u0C61\u0C80\u0C85-\u0C8C\u0C8E-\u0C90\u0C92-\u0CA8\u0CAA-\u0CB3\u0CB5-\u0CB9\u0CBD\u0CDD\u0CDE\u0CE0\u0CE1\u0CF1\u0CF2\u0D04-\u0D0C\u0D0E-\u0D10\u0D12-\u0D3A\u0D3D\u0D4E\u0D54-\u0D56\u0D5F-\u0D61\u0D7A-\u0D7F\u0D85-\u0D96\u0D9A-\u0DB1\u0DB3-\u0DBB\u0DBD\u0DC0-\u0DC6\u0E01-\u0E30\u0E32\u0E33\u0E40-\u0E46\u0E81\u0E82\u0E84\u0E86-\u0E8A\u0E8C-\u0EA3\u0EA5\u0EA7-\u0EB0\u0EB2\u0EB3\u0EBD\u0EC0-\u0EC4\u0EC6\u0EDC-\u0EDF\u0F00\u0F40-\u0F47\u0F49-\u0F6C\u0F88-\u0F8C\u1000-\u102A\u103F\u1050-\u1055\u105A-\u105D\u1061\u1065\u1066\u106E-\u1070\u1075-\u1081\u108E\u10A0-\u10C5\u10C7\u10CD\u10D0-\u10FA\u10FC-\u1248\u124A-\u124D\u1250-\u1256\u1258\u125A-\u125D\u1260-\u1288\u128A-\u128D\u1290-\u12B0\u12B2-\u12B5\u12B8-\u12BE\u12C0\u12C2-\u12C5\u12C8-\u12D6\u12D8-\u1310\u1312-\u1315\u1318-\u135A\u1380-\u138F\u13A0-\u13F5\u13F8-\u13FD\u1401-\u166C\u166F-\u167F\u1681-\u169A\u16A0-\u16EA\u16F1-\u16F8\u1700-\u1711\u171F-\u1731\u1740-\u1751\u1760-\u176C\u176E-\u1770\u1780-\u17B3\u17D7\u17DC\u1820-\u1878\u1880-\u1884\u1887-\u18A8\u18AA\u18B0-\u18F5\u1900-\u191E\u1950-\u196D\u1970-\u1974\u1980-\u19AB\u19B0-\u19C9\u1A00-\u1A16\u1A20-\u1A54\u1AA7\u1B05-\u1B33\u1B45-\u1B4C\u1B83-\u1BA0\u1BAE\u1BAF\u1BBA-\u1BE5\u1C00-\u1C23\u1C4D-\u1C4F\u1C5A-\u1C7D\u1C80-\u1C88\u1C90-\u1CBA\u1CBD-\u1CBF\u1CE9-\u1CEC\u1CEE-\u1CF3\u1CF5\u1CF6\u1CFA\u1D00-\u1DBF\u1E00-\u1F15\u1F18-\u1F1D\u1F20-\u1F45\u1F48-\u1F4D\u1F50-\u1F57\u1F59\u1F5B\u1F5D\u1F5F-\u1F7D\u1F80-\u1FB4\u1FB6-\u1FBC\u1FBE\u1FC2-\u1FC4\u1FC6-\u1FCC\u1FD0-\u1FD3\u1FD6-\u1FDB\u1FE0-\u1FEC\u1FF2-\u1FF4\u1FF6-\u1FFC\u2071\u207F\u2090-\u209C\u2102\u2107\u210A-\u2113\u2115\u2119-\u211D\u2124\u2126\u2128\u212A-\u212D\u212F-\u2139\u213C-\u213F\u2145-\u2149\u214E\u2183\u2184\u2C00-\u2CE4\u2CEB-\u2CEE\u2CF2\u2CF3\u2D00-\u2D25\u2D27\u2D2D\u2D30-\u2D67\u2D6F\u2D80-\u2D96\u2DA0-\u2DA6\u2DA8-\u2DAE\u2DB0-\u2DB6\u2DB8-\u2DBE\u2DC0-\u2DC6\u2DC8-\u2DCE\u2DD0-\u2DD6\u2DD8-\u2DDE\u2E2F\u3005\u3006\u3031-\u3035\u303B\u303C\u3041-\u3096\u309D-\u309F\u30A1-\u30FA\u30FC-\u30FF\u3105-\u312F\u3131-\u318E\u31A0-\u31BF\u31F0-\u31FF\u3400-\u4DBF\u4E00-\uA48C\uA4D0-\uA4FD\uA500-\uA60C\uA610-\uA61F\uA62A\uA62B\uA640-\uA66E\uA67F-\uA69D\uA6A0-\uA6E5\uA717-\uA71F\uA722-\uA788\uA78B-\uA7CA\uA7D0\uA7D1\uA7D3\uA7D5-\uA7D9\uA7F2-\uA801\uA803-\uA805\uA807-\uA80A\uA80C-\uA822\uA840-\uA873\uA882-\uA8B3\uA8F2-\uA8F7\uA8FB\uA8FD\uA8FE\uA90A-\uA925\uA930-\uA946\uA960-\uA97C\uA984-\uA9B2\uA9CF\uA9E0-\uA9E4\uA9E6-\uA9EF\uA9FA-\uA9FE\uAA00-\uAA28\uAA40-\uAA42\uAA44-\uAA4B\uAA60-\uAA76\uAA7A\uAA7E-\uAAAF\uAAB1\uAAB5\uAAB6\uAAB9-\uAABD\uAAC0\uAAC2\uAADB-\uAADD\uAAE0-\uAAEA\uAAF2-\uAAF4\uAB01-\uAB06\uAB09-\uAB0E\uAB11-\uAB16\uAB20-\uAB26\uAB28-\uAB2E\uAB30-\uAB5A\uAB5C-\uAB69\uAB70-\uABE2\uAC00-\uD7A3\uD7B0-\uD7C6\uD7CB-\uD7FB\uF900-\uFA6D\uFA70-\uFAD9\uFB00-\uFB06\uFB13-\uFB17\uFB1D\uFB1F-\uFB28\uFB2A-\uFB36\uFB38-\uFB3C\uFB3E\uFB40\uFB41\uFB43\uFB44\uFB46-\uFBB1\uFBD3-\uFD3D\uFD50-\uFD8F\uFD92-\uFDC7\uFDF0-\uFDFB\uFE70-\uFE74\uFE76-\uFEFC\uFF21-\uFF3A\uFF41-\uFF5A\uFF66-\uFFBE\uFFC2-\uFFC7\uFFCA-\uFFCF\uFFD2-\uFFD7\uFFDA-\uFFDC]|\uD800[\uDC00-\uDC0B\uDC0D-\uDC26\uDC28-\uDC3A\uDC3C\uDC3D\uDC3F-\uDC4D\uDC50-\uDC5D\uDC80-\uDCFA\uDE80-\uDE9C\uDEA0-\uDED0\uDF00-\uDF1F\uDF2D-\uDF40\uDF42-\uDF49\uDF50-\uDF75\uDF80-\uDF9D\uDFA0-\uDFC3\uDFC8-\uDFCF]|\uD801[\uDC00-\uDC9D\uDCB0-\uDCD3\uDCD8-\uDCFB\uDD00-\uDD27\uDD30-\uDD63\uDD70-\uDD7A\uDD7C-\uDD8A\uDD8C-\uDD92\uDD94\uDD95\uDD97-\uDDA1\uDDA3-\uDDB1\uDDB3-\uDDB9\uDDBB\uDDBC\uDE00-\uDF36\uDF40-\uDF55\uDF60-\uDF67\uDF80-\uDF85\uDF87-\uDFB0\uDFB2-\uDFBA]|\uD802[\uDC00-\uDC05\uDC08\uDC0A-\uDC35\uDC37\uDC38\uDC3C\uDC3F-\uDC55\uDC60-\uDC76\uDC80-\uDC9E\uDCE0-\uDCF2\uDCF4\uDCF5\uDD00-\uDD15\uDD20-\uDD39\uDD80-\uDDB7\uDDBE\uDDBF\uDE00\uDE10-\uDE13\uDE15-\uDE17\uDE19-\uDE35\uDE60-\uDE7C\uDE80-\uDE9C\uDEC0-\uDEC7\uDEC9-\uDEE4\uDF00-\uDF35\uDF40-\uDF55\uDF60-\uDF72\uDF80-\uDF91]|\uD803[\uDC00-\uDC48\uDC80-\uDCB2\uDCC0-\uDCF2\uDD00-\uDD23\uDE80-\uDEA9\uDEB0\uDEB1\uDF00-\uDF1C\uDF27\uDF30-\uDF45\uDF70-\uDF81\uDFB0-\uDFC4\uDFE0-\uDFF6]|\uD804[\uDC03-\uDC37\uDC71\uDC72\uDC75\uDC83-\uDCAF\uDCD0-\uDCE8\uDD03-\uDD26\uDD44\uDD47\uDD50-\uDD72\uDD76\uDD83-\uDDB2\uDDC1-\uDDC4\uDDDA\uDDDC\uDE00-\uDE11\uDE13-\uDE2B\uDE3F\uDE40\uDE80-\uDE86\uDE88\uDE8A-\uDE8D\uDE8F-\uDE9D\uDE9F-\uDEA8\uDEB0-\uDEDE\uDF05-\uDF0C\uDF0F\uDF10\uDF13-\uDF28\uDF2A-\uDF30\uDF32\uDF33\uDF35-\uDF39\uDF3D\uDF50\uDF5D-\uDF61]|\uD805[\uDC00-\uDC34\uDC47-\uDC4A\uDC5F-\uDC61\uDC80-\uDCAF\uDCC4\uDCC5\uDCC7\uDD80-\uDDAE\uDDD8-\uDDDB\uDE00-\uDE2F\uDE44\uDE80-\uDEAA\uDEB8\uDF00-\uDF1A\uDF40-\uDF46]|\uD806[\uDC00-\uDC2B\uDCA0-\uDCDF\uDCFF-\uDD06\uDD09\uDD0C-\uDD13\uDD15\uDD16\uDD18-\uDD2F\uDD3F\uDD41\uDDA0-\uDDA7\uDDAA-\uDDD0\uDDE1\uDDE3\uDE00\uDE0B-\uDE32\uDE3A\uDE50\uDE5C-\uDE89\uDE9D\uDEB0-\uDEF8]|\uD807[\uDC00-\uDC08\uDC0A-\uDC2E\uDC40\uDC72-\uDC8F\uDD00-\uDD06\uDD08\uDD09\uDD0B-\uDD30\uDD46\uDD60-\uDD65\uDD67\uDD68\uDD6A-\uDD89\uDD98\uDEE0-\uDEF2\uDF02\uDF04-\uDF10\uDF12-\uDF33\uDFB0]|\uD808[\uDC00-\uDF99]|\uD809[\uDC80-\uDD43]|\uD80B[\uDF90-\uDFF0]|[\uD80C\uD81C-\uD820\uD822\uD840-\uD868\uD86A-\uD86C\uD86F-\uD872\uD874-\uD879\uD880-\uD883\uD885-\uD887][\uDC00-\uDFFF]|\uD80D[\uDC00-\uDC2F\uDC41-\uDC46]|\uD811[\uDC00-\uDE46]|\uD81A[\uDC00-\uDE38\uDE40-\uDE5E\uDE70-\uDEBE\uDED0-\uDEED\uDF00-\uDF2F\uDF40-\uDF43\uDF63-\uDF77\uDF7D-\uDF8F]|\uD81B[\uDE40-\uDE7F\uDF00-\uDF4A\uDF50\uDF93-\uDF9F\uDFE0\uDFE1\uDFE3]|\uD821[\uDC00-\uDFF7]|\uD823[\uDC00-\uDCD5\uDD00-\uDD08]|\uD82B[\uDFF0-\uDFF3\uDFF5-\uDFFB\uDFFD\uDFFE]|\uD82C[\uDC00-\uDD22\uDD32\uDD50-\uDD52\uDD55\uDD64-\uDD67\uDD70-\uDEFB]|\uD82F[\uDC00-\uDC6A\uDC70-\uDC7C\uDC80-\uDC88\uDC90-\uDC99]|\uD835[\uDC00-\uDC54\uDC56-\uDC9C\uDC9E\uDC9F\uDCA2\uDCA5\uDCA6\uDCA9-\uDCAC\uDCAE-\uDCB9\uDCBB\uDCBD-\uDCC3\uDCC5-\uDD05\uDD07-\uDD0A\uDD0D-\uDD14\uDD16-\uDD1C\uDD1E-\uDD39\uDD3B-\uDD3E\uDD40-\uDD44\uDD46\uDD4A-\uDD50\uDD52-\uDEA5\uDEA8-\uDEC0\uDEC2-\uDEDA\uDEDC-\uDEFA\uDEFC-\uDF14\uDF16-\uDF34\uDF36-\uDF4E\uDF50-\uDF6E\uDF70-\uDF88\uDF8A-\uDFA8\uDFAA-\uDFC2\uDFC4-\uDFCB]|\uD837[\uDF00-\uDF1E\uDF25-\uDF2A]|\uD838[\uDC30-\uDC6D\uDD00-\uDD2C\uDD37-\uDD3D\uDD4E\uDE90-\uDEAD\uDEC0-\uDEEB]|\uD839[\uDCD0-\uDCEB\uDFE0-\uDFE6\uDFE8-\uDFEB\uDFED\uDFEE\uDFF0-\uDFFE]|\uD83A[\uDC00-\uDCC4\uDD00-\uDD43\uDD4B]|\uD83B[\uDE00-\uDE03\uDE05-\uDE1F\uDE21\uDE22\uDE24\uDE27\uDE29-\uDE32\uDE34-\uDE37\uDE39\uDE3B\uDE42\uDE47\uDE49\uDE4B\uDE4D-\uDE4F\uDE51\uDE52\uDE54\uDE57\uDE59\uDE5B\uDE5D\uDE5F\uDE61\uDE62\uDE64\uDE67-\uDE6A\uDE6C-\uDE72\uDE74-\uDE77\uDE79-\uDE7C\uDE7E\uDE80-\uDE89\uDE8B-\uDE9B\uDEA1-\uDEA3\uDEA5-\uDEA9\uDEAB-\uDEBB]|\uD869[\uDC00-\uDEDF\uDF00-\uDFFF]|\uD86D[\uDC00-\uDF39\uDF40-\uDFFF]|\uD86E[\uDC00-\uDC1D\uDC20-\uDFFF]|\uD873[\uDC00-\uDEA1\uDEB0-\uDFFF]|\uD87A[\uDC00-\uDFE0\uDFF0-\uDFFF]|\uD87B[\uDC00-\uDE5D]|\uD87E[\uDC00-\uDE1D]|\uD884[\uDC00-\uDF4A\uDF50-\uDFFF]|\uD888[\uDC00-\uDFAF])/.test(val)) {
1734
- return false;
1735
- }
1736
- return window.intlTelInputUtils ? intlTelInputUtils.isPossibleNumber(val, this.selectedCountryData.iso2, mobileOnly) : null;
1737
- }
1738
- }, {
1739
- key: "isValidNumberPrecise",
1740
- value: function isValidNumberPrecise() {
1741
- var val = this._getFullNumber();
1742
- // return false for any alpha chars
1743
- if (/(?:[A-Za-z\xAA\xB5\xBA\xC0-\xD6\xD8-\xF6\xF8-\u02C1\u02C6-\u02D1\u02E0-\u02E4\u02EC\u02EE\u0370-\u0374\u0376\u0377\u037A-\u037D\u037F\u0386\u0388-\u038A\u038C\u038E-\u03A1\u03A3-\u03F5\u03F7-\u0481\u048A-\u052F\u0531-\u0556\u0559\u0560-\u0588\u05D0-\u05EA\u05EF-\u05F2\u0620-\u064A\u066E\u066F\u0671-\u06D3\u06D5\u06E5\u06E6\u06EE\u06EF\u06FA-\u06FC\u06FF\u0710\u0712-\u072F\u074D-\u07A5\u07B1\u07CA-\u07EA\u07F4\u07F5\u07FA\u0800-\u0815\u081A\u0824\u0828\u0840-\u0858\u0860-\u086A\u0870-\u0887\u0889-\u088E\u08A0-\u08C9\u0904-\u0939\u093D\u0950\u0958-\u0961\u0971-\u0980\u0985-\u098C\u098F\u0990\u0993-\u09A8\u09AA-\u09B0\u09B2\u09B6-\u09B9\u09BD\u09CE\u09DC\u09DD\u09DF-\u09E1\u09F0\u09F1\u09FC\u0A05-\u0A0A\u0A0F\u0A10\u0A13-\u0A28\u0A2A-\u0A30\u0A32\u0A33\u0A35\u0A36\u0A38\u0A39\u0A59-\u0A5C\u0A5E\u0A72-\u0A74\u0A85-\u0A8D\u0A8F-\u0A91\u0A93-\u0AA8\u0AAA-\u0AB0\u0AB2\u0AB3\u0AB5-\u0AB9\u0ABD\u0AD0\u0AE0\u0AE1\u0AF9\u0B05-\u0B0C\u0B0F\u0B10\u0B13-\u0B28\u0B2A-\u0B30\u0B32\u0B33\u0B35-\u0B39\u0B3D\u0B5C\u0B5D\u0B5F-\u0B61\u0B71\u0B83\u0B85-\u0B8A\u0B8E-\u0B90\u0B92-\u0B95\u0B99\u0B9A\u0B9C\u0B9E\u0B9F\u0BA3\u0BA4\u0BA8-\u0BAA\u0BAE-\u0BB9\u0BD0\u0C05-\u0C0C\u0C0E-\u0C10\u0C12-\u0C28\u0C2A-\u0C39\u0C3D\u0C58-\u0C5A\u0C5D\u0C60\u0C61\u0C80\u0C85-\u0C8C\u0C8E-\u0C90\u0C92-\u0CA8\u0CAA-\u0CB3\u0CB5-\u0CB9\u0CBD\u0CDD\u0CDE\u0CE0\u0CE1\u0CF1\u0CF2\u0D04-\u0D0C\u0D0E-\u0D10\u0D12-\u0D3A\u0D3D\u0D4E\u0D54-\u0D56\u0D5F-\u0D61\u0D7A-\u0D7F\u0D85-\u0D96\u0D9A-\u0DB1\u0DB3-\u0DBB\u0DBD\u0DC0-\u0DC6\u0E01-\u0E30\u0E32\u0E33\u0E40-\u0E46\u0E81\u0E82\u0E84\u0E86-\u0E8A\u0E8C-\u0EA3\u0EA5\u0EA7-\u0EB0\u0EB2\u0EB3\u0EBD\u0EC0-\u0EC4\u0EC6\u0EDC-\u0EDF\u0F00\u0F40-\u0F47\u0F49-\u0F6C\u0F88-\u0F8C\u1000-\u102A\u103F\u1050-\u1055\u105A-\u105D\u1061\u1065\u1066\u106E-\u1070\u1075-\u1081\u108E\u10A0-\u10C5\u10C7\u10CD\u10D0-\u10FA\u10FC-\u1248\u124A-\u124D\u1250-\u1256\u1258\u125A-\u125D\u1260-\u1288\u128A-\u128D\u1290-\u12B0\u12B2-\u12B5\u12B8-\u12BE\u12C0\u12C2-\u12C5\u12C8-\u12D6\u12D8-\u1310\u1312-\u1315\u1318-\u135A\u1380-\u138F\u13A0-\u13F5\u13F8-\u13FD\u1401-\u166C\u166F-\u167F\u1681-\u169A\u16A0-\u16EA\u16F1-\u16F8\u1700-\u1711\u171F-\u1731\u1740-\u1751\u1760-\u176C\u176E-\u1770\u1780-\u17B3\u17D7\u17DC\u1820-\u1878\u1880-\u1884\u1887-\u18A8\u18AA\u18B0-\u18F5\u1900-\u191E\u1950-\u196D\u1970-\u1974\u1980-\u19AB\u19B0-\u19C9\u1A00-\u1A16\u1A20-\u1A54\u1AA7\u1B05-\u1B33\u1B45-\u1B4C\u1B83-\u1BA0\u1BAE\u1BAF\u1BBA-\u1BE5\u1C00-\u1C23\u1C4D-\u1C4F\u1C5A-\u1C7D\u1C80-\u1C88\u1C90-\u1CBA\u1CBD-\u1CBF\u1CE9-\u1CEC\u1CEE-\u1CF3\u1CF5\u1CF6\u1CFA\u1D00-\u1DBF\u1E00-\u1F15\u1F18-\u1F1D\u1F20-\u1F45\u1F48-\u1F4D\u1F50-\u1F57\u1F59\u1F5B\u1F5D\u1F5F-\u1F7D\u1F80-\u1FB4\u1FB6-\u1FBC\u1FBE\u1FC2-\u1FC4\u1FC6-\u1FCC\u1FD0-\u1FD3\u1FD6-\u1FDB\u1FE0-\u1FEC\u1FF2-\u1FF4\u1FF6-\u1FFC\u2071\u207F\u2090-\u209C\u2102\u2107\u210A-\u2113\u2115\u2119-\u211D\u2124\u2126\u2128\u212A-\u212D\u212F-\u2139\u213C-\u213F\u2145-\u2149\u214E\u2183\u2184\u2C00-\u2CE4\u2CEB-\u2CEE\u2CF2\u2CF3\u2D00-\u2D25\u2D27\u2D2D\u2D30-\u2D67\u2D6F\u2D80-\u2D96\u2DA0-\u2DA6\u2DA8-\u2DAE\u2DB0-\u2DB6\u2DB8-\u2DBE\u2DC0-\u2DC6\u2DC8-\u2DCE\u2DD0-\u2DD6\u2DD8-\u2DDE\u2E2F\u3005\u3006\u3031-\u3035\u303B\u303C\u3041-\u3096\u309D-\u309F\u30A1-\u30FA\u30FC-\u30FF\u3105-\u312F\u3131-\u318E\u31A0-\u31BF\u31F0-\u31FF\u3400-\u4DBF\u4E00-\uA48C\uA4D0-\uA4FD\uA500-\uA60C\uA610-\uA61F\uA62A\uA62B\uA640-\uA66E\uA67F-\uA69D\uA6A0-\uA6E5\uA717-\uA71F\uA722-\uA788\uA78B-\uA7CA\uA7D0\uA7D1\uA7D3\uA7D5-\uA7D9\uA7F2-\uA801\uA803-\uA805\uA807-\uA80A\uA80C-\uA822\uA840-\uA873\uA882-\uA8B3\uA8F2-\uA8F7\uA8FB\uA8FD\uA8FE\uA90A-\uA925\uA930-\uA946\uA960-\uA97C\uA984-\uA9B2\uA9CF\uA9E0-\uA9E4\uA9E6-\uA9EF\uA9FA-\uA9FE\uAA00-\uAA28\uAA40-\uAA42\uAA44-\uAA4B\uAA60-\uAA76\uAA7A\uAA7E-\uAAAF\uAAB1\uAAB5\uAAB6\uAAB9-\uAABD\uAAC0\uAAC2\uAADB-\uAADD\uAAE0-\uAAEA\uAAF2-\uAAF4\uAB01-\uAB06\uAB09-\uAB0E\uAB11-\uAB16\uAB20-\uAB26\uAB28-\uAB2E\uAB30-\uAB5A\uAB5C-\uAB69\uAB70-\uABE2\uAC00-\uD7A3\uD7B0-\uD7C6\uD7CB-\uD7FB\uF900-\uFA6D\uFA70-\uFAD9\uFB00-\uFB06\uFB13-\uFB17\uFB1D\uFB1F-\uFB28\uFB2A-\uFB36\uFB38-\uFB3C\uFB3E\uFB40\uFB41\uFB43\uFB44\uFB46-\uFBB1\uFBD3-\uFD3D\uFD50-\uFD8F\uFD92-\uFDC7\uFDF0-\uFDFB\uFE70-\uFE74\uFE76-\uFEFC\uFF21-\uFF3A\uFF41-\uFF5A\uFF66-\uFFBE\uFFC2-\uFFC7\uFFCA-\uFFCF\uFFD2-\uFFD7\uFFDA-\uFFDC]|\uD800[\uDC00-\uDC0B\uDC0D-\uDC26\uDC28-\uDC3A\uDC3C\uDC3D\uDC3F-\uDC4D\uDC50-\uDC5D\uDC80-\uDCFA\uDE80-\uDE9C\uDEA0-\uDED0\uDF00-\uDF1F\uDF2D-\uDF40\uDF42-\uDF49\uDF50-\uDF75\uDF80-\uDF9D\uDFA0-\uDFC3\uDFC8-\uDFCF]|\uD801[\uDC00-\uDC9D\uDCB0-\uDCD3\uDCD8-\uDCFB\uDD00-\uDD27\uDD30-\uDD63\uDD70-\uDD7A\uDD7C-\uDD8A\uDD8C-\uDD92\uDD94\uDD95\uDD97-\uDDA1\uDDA3-\uDDB1\uDDB3-\uDDB9\uDDBB\uDDBC\uDE00-\uDF36\uDF40-\uDF55\uDF60-\uDF67\uDF80-\uDF85\uDF87-\uDFB0\uDFB2-\uDFBA]|\uD802[\uDC00-\uDC05\uDC08\uDC0A-\uDC35\uDC37\uDC38\uDC3C\uDC3F-\uDC55\uDC60-\uDC76\uDC80-\uDC9E\uDCE0-\uDCF2\uDCF4\uDCF5\uDD00-\uDD15\uDD20-\uDD39\uDD80-\uDDB7\uDDBE\uDDBF\uDE00\uDE10-\uDE13\uDE15-\uDE17\uDE19-\uDE35\uDE60-\uDE7C\uDE80-\uDE9C\uDEC0-\uDEC7\uDEC9-\uDEE4\uDF00-\uDF35\uDF40-\uDF55\uDF60-\uDF72\uDF80-\uDF91]|\uD803[\uDC00-\uDC48\uDC80-\uDCB2\uDCC0-\uDCF2\uDD00-\uDD23\uDE80-\uDEA9\uDEB0\uDEB1\uDF00-\uDF1C\uDF27\uDF30-\uDF45\uDF70-\uDF81\uDFB0-\uDFC4\uDFE0-\uDFF6]|\uD804[\uDC03-\uDC37\uDC71\uDC72\uDC75\uDC83-\uDCAF\uDCD0-\uDCE8\uDD03-\uDD26\uDD44\uDD47\uDD50-\uDD72\uDD76\uDD83-\uDDB2\uDDC1-\uDDC4\uDDDA\uDDDC\uDE00-\uDE11\uDE13-\uDE2B\uDE3F\uDE40\uDE80-\uDE86\uDE88\uDE8A-\uDE8D\uDE8F-\uDE9D\uDE9F-\uDEA8\uDEB0-\uDEDE\uDF05-\uDF0C\uDF0F\uDF10\uDF13-\uDF28\uDF2A-\uDF30\uDF32\uDF33\uDF35-\uDF39\uDF3D\uDF50\uDF5D-\uDF61]|\uD805[\uDC00-\uDC34\uDC47-\uDC4A\uDC5F-\uDC61\uDC80-\uDCAF\uDCC4\uDCC5\uDCC7\uDD80-\uDDAE\uDDD8-\uDDDB\uDE00-\uDE2F\uDE44\uDE80-\uDEAA\uDEB8\uDF00-\uDF1A\uDF40-\uDF46]|\uD806[\uDC00-\uDC2B\uDCA0-\uDCDF\uDCFF-\uDD06\uDD09\uDD0C-\uDD13\uDD15\uDD16\uDD18-\uDD2F\uDD3F\uDD41\uDDA0-\uDDA7\uDDAA-\uDDD0\uDDE1\uDDE3\uDE00\uDE0B-\uDE32\uDE3A\uDE50\uDE5C-\uDE89\uDE9D\uDEB0-\uDEF8]|\uD807[\uDC00-\uDC08\uDC0A-\uDC2E\uDC40\uDC72-\uDC8F\uDD00-\uDD06\uDD08\uDD09\uDD0B-\uDD30\uDD46\uDD60-\uDD65\uDD67\uDD68\uDD6A-\uDD89\uDD98\uDEE0-\uDEF2\uDF02\uDF04-\uDF10\uDF12-\uDF33\uDFB0]|\uD808[\uDC00-\uDF99]|\uD809[\uDC80-\uDD43]|\uD80B[\uDF90-\uDFF0]|[\uD80C\uD81C-\uD820\uD822\uD840-\uD868\uD86A-\uD86C\uD86F-\uD872\uD874-\uD879\uD880-\uD883\uD885-\uD887][\uDC00-\uDFFF]|\uD80D[\uDC00-\uDC2F\uDC41-\uDC46]|\uD811[\uDC00-\uDE46]|\uD81A[\uDC00-\uDE38\uDE40-\uDE5E\uDE70-\uDEBE\uDED0-\uDEED\uDF00-\uDF2F\uDF40-\uDF43\uDF63-\uDF77\uDF7D-\uDF8F]|\uD81B[\uDE40-\uDE7F\uDF00-\uDF4A\uDF50\uDF93-\uDF9F\uDFE0\uDFE1\uDFE3]|\uD821[\uDC00-\uDFF7]|\uD823[\uDC00-\uDCD5\uDD00-\uDD08]|\uD82B[\uDFF0-\uDFF3\uDFF5-\uDFFB\uDFFD\uDFFE]|\uD82C[\uDC00-\uDD22\uDD32\uDD50-\uDD52\uDD55\uDD64-\uDD67\uDD70-\uDEFB]|\uD82F[\uDC00-\uDC6A\uDC70-\uDC7C\uDC80-\uDC88\uDC90-\uDC99]|\uD835[\uDC00-\uDC54\uDC56-\uDC9C\uDC9E\uDC9F\uDCA2\uDCA5\uDCA6\uDCA9-\uDCAC\uDCAE-\uDCB9\uDCBB\uDCBD-\uDCC3\uDCC5-\uDD05\uDD07-\uDD0A\uDD0D-\uDD14\uDD16-\uDD1C\uDD1E-\uDD39\uDD3B-\uDD3E\uDD40-\uDD44\uDD46\uDD4A-\uDD50\uDD52-\uDEA5\uDEA8-\uDEC0\uDEC2-\uDEDA\uDEDC-\uDEFA\uDEFC-\uDF14\uDF16-\uDF34\uDF36-\uDF4E\uDF50-\uDF6E\uDF70-\uDF88\uDF8A-\uDFA8\uDFAA-\uDFC2\uDFC4-\uDFCB]|\uD837[\uDF00-\uDF1E\uDF25-\uDF2A]|\uD838[\uDC30-\uDC6D\uDD00-\uDD2C\uDD37-\uDD3D\uDD4E\uDE90-\uDEAD\uDEC0-\uDEEB]|\uD839[\uDCD0-\uDCEB\uDFE0-\uDFE6\uDFE8-\uDFEB\uDFED\uDFEE\uDFF0-\uDFFE]|\uD83A[\uDC00-\uDCC4\uDD00-\uDD43\uDD4B]|\uD83B[\uDE00-\uDE03\uDE05-\uDE1F\uDE21\uDE22\uDE24\uDE27\uDE29-\uDE32\uDE34-\uDE37\uDE39\uDE3B\uDE42\uDE47\uDE49\uDE4B\uDE4D-\uDE4F\uDE51\uDE52\uDE54\uDE57\uDE59\uDE5B\uDE5D\uDE5F\uDE61\uDE62\uDE64\uDE67-\uDE6A\uDE6C-\uDE72\uDE74-\uDE77\uDE79-\uDE7C\uDE7E\uDE80-\uDE89\uDE8B-\uDE9B\uDEA1-\uDEA3\uDEA5-\uDEA9\uDEAB-\uDEBB]|\uD869[\uDC00-\uDEDF\uDF00-\uDFFF]|\uD86D[\uDC00-\uDF39\uDF40-\uDFFF]|\uD86E[\uDC00-\uDC1D\uDC20-\uDFFF]|\uD873[\uDC00-\uDEA1\uDEB0-\uDFFF]|\uD87A[\uDC00-\uDFE0\uDFF0-\uDFFF]|\uD87B[\uDC00-\uDE5D]|\uD87E[\uDC00-\uDE1D]|\uD884[\uDC00-\uDF4A\uDF50-\uDFFF]|\uD888[\uDC00-\uDFAF])/.test(val)) {
1744
- return false;
1505
+ }
1506
+ // this is called when the utils request completes
1507
+ handleUtils() {
1508
+ // if the request was successful
1509
+ if (window.intlTelInputUtils) {
1510
+ // if there's an initial value in the input, then format it
1511
+ if (this.telInput.value) {
1512
+ this._updateValFromNumber(this.telInput.value);
1745
1513
  }
1746
- return window.intlTelInputUtils ? intlTelInputUtils.isValidNumber(val, this.selectedCountryData.iso2) : null;
1747
- }
1748
- }, {
1749
- key: "setCountry",
1750
- value: function setCountry(iso2) {
1751
- var iso2Lower = iso2.toLowerCase();
1752
- // check if already selected
1753
- if (this.selectedCountryData.iso2 !== iso2Lower) {
1754
- this._setCountry(iso2Lower);
1755
- this._updateDialCode(this.selectedCountryData.dialCode);
1756
- this._triggerCountryChange();
1514
+ if (this.selectedCountryData.iso2) {
1515
+ this._updatePlaceholder();
1516
+ this._updateMaxLength();
1757
1517
  }
1758
1518
  }
1759
- }, {
1760
- key: "setNumber",
1761
- value: function setNumber(number) {
1762
- // we must update the flag first, which updates this.selectedCountryData, which is used for
1763
- // formatting the number before displaying it
1764
- var flagChanged = this._updateFlagFromNumber(number);
1765
- this._updateValFromNumber(number);
1766
- if (flagChanged) {
1767
- this._triggerCountryChange();
1768
- }
1519
+ this.resolveUtilsScriptPromise();
1520
+ }
1521
+ /********************
1522
+ * PUBLIC METHODS
1523
+ ********************/
1524
+ // remove plugin
1525
+ destroy() {
1526
+ if (this.options.allowDropdown) {
1527
+ // make sure the dropdown is closed (and unbind listeners)
1528
+ this._closeDropdown();
1529
+ this.selectedFlag.removeEventListener("click", this._handleClickSelectedFlag);
1530
+ this.flagsContainer.removeEventListener("keydown", this._handleFlagsContainerKeydown);
1531
+ // label click hack
1532
+ const label = this.telInput.closest("label");
1533
+ if (label) {
1534
+ label.removeEventListener("click", this._handleLabelClick);
1535
+ }
1536
+ }
1537
+ // unbind hiddenInput listeners
1538
+ const {
1539
+ form
1540
+ } = this.telInput;
1541
+ if (this._handleHiddenInputSubmit && form) {
1542
+ form.removeEventListener("submit", this._handleHiddenInputSubmit);
1543
+ }
1544
+ // unbind key events, and cut/paste events
1545
+ this.telInput.removeEventListener("input", this._handleInputEvent);
1546
+ if (this._handleKeydownEvent) {
1547
+ this.telInput.removeEventListener("keydown", this._handleKeydownEvent);
1548
+ }
1549
+ // remove attribute of id instance: data-intl-tel-input-id
1550
+ this.telInput.removeAttribute("data-intl-tel-input-id");
1551
+ // remove markup (but leave the original input)
1552
+ const wrapper = this.telInput.parentNode;
1553
+ wrapper.parentNode.insertBefore(this.telInput, wrapper);
1554
+ wrapper.parentNode.removeChild(wrapper);
1555
+ delete window.intlTelInputGlobals.instances[this.id];
1556
+ }
1557
+ // get the extension from the current number
1558
+ getExtension() {
1559
+ if (window.intlTelInputUtils) {
1560
+ return intlTelInputUtils.getExtension(this._getFullNumber(), this.selectedCountryData.iso2);
1769
1561
  }
1770
- }, {
1771
- key: "setPlaceholderNumberType",
1772
- value: function setPlaceholderNumberType(type) {
1773
- this.options.placeholderNumberType = type;
1774
- this._updatePlaceholder();
1562
+ return "";
1563
+ }
1564
+ // format the number to the given format
1565
+ getNumber(format) {
1566
+ if (window.intlTelInputUtils) {
1567
+ const {
1568
+ iso2
1569
+ } = this.selectedCountryData;
1570
+ return intlTelInputUtils.formatNumber(this._getFullNumber(), iso2, format);
1571
+ }
1572
+ return "";
1573
+ }
1574
+ // get the type of the entered number e.g. landline/mobile
1575
+ getNumberType() {
1576
+ if (window.intlTelInputUtils) {
1577
+ return intlTelInputUtils.getNumberType(this._getFullNumber(), this.selectedCountryData.iso2);
1775
1578
  }
1776
- } ]);
1777
- return Iti;
1778
- }();
1579
+ return -99;
1580
+ }
1581
+ // get the country data for the currently selected flag
1582
+ getSelectedCountryData() {
1583
+ return this.selectedCountryData;
1584
+ }
1585
+ // get the validation error
1586
+ getValidationError() {
1587
+ if (window.intlTelInputUtils) {
1588
+ const {
1589
+ iso2
1590
+ } = this.selectedCountryData;
1591
+ return intlTelInputUtils.getValidationError(this._getFullNumber(), iso2);
1592
+ }
1593
+ return -99;
1594
+ }
1595
+ // validate the input val - assumes the global function isPossibleNumber (from utilsScript)
1596
+ isValidNumber(mobileOnly = true) {
1597
+ const val = this._getFullNumber();
1598
+ // return false for any alpha chars
1599
+ if (/\p{L}/u.test(val)) {
1600
+ return false;
1601
+ }
1602
+ return window.intlTelInputUtils ? intlTelInputUtils.isPossibleNumber(val, this.selectedCountryData.iso2, mobileOnly) : null;
1603
+ }
1604
+ // validate the input val (precise) - assumes the global function isValidNumber (from utilsScript)
1605
+ isValidNumberPrecise() {
1606
+ const val = this._getFullNumber();
1607
+ // return false for any alpha chars
1608
+ if (/\p{L}/u.test(val)) {
1609
+ return false;
1610
+ }
1611
+ return window.intlTelInputUtils ? intlTelInputUtils.isValidNumber(val, this.selectedCountryData.iso2) : null;
1612
+ }
1613
+ // update the selected flag, and update the input val accordingly
1614
+ setCountry(iso2) {
1615
+ const iso2Lower = iso2.toLowerCase();
1616
+ // check if already selected
1617
+ if (this.selectedCountryData.iso2 !== iso2Lower) {
1618
+ this._setCountry(iso2Lower);
1619
+ this._updateDialCode(this.selectedCountryData.dialCode);
1620
+ this._triggerCountryChange();
1621
+ }
1622
+ }
1623
+ // set the input value and update the flag
1624
+ setNumber(number) {
1625
+ // we must update the flag first, which updates this.selectedCountryData, which is used for
1626
+ // formatting the number before displaying it
1627
+ const flagChanged = this._updateFlagFromNumber(number);
1628
+ this._updateValFromNumber(number);
1629
+ if (flagChanged) {
1630
+ this._triggerCountryChange();
1631
+ }
1632
+ }
1633
+ // set the placeholder number typ
1634
+ setPlaceholderNumberType(type) {
1635
+ this.options.placeholderNumberType = type;
1636
+ this._updatePlaceholder();
1637
+ }
1638
+ }
1779
1639
  /********************
1780
1640
  * STATIC METHODS
1781
1641
  ********************/
1782
1642
  // get the country data object
1783
- intlTelInputGlobals.getCountryData = function() {
1784
- return allCountries;
1785
- };
1643
+ intlTelInputGlobals.getCountryData = () => allCountries;
1786
1644
  // inject a <script> element to load utils.js
1787
- var injectScript = function injectScript(path, handleSuccess, handleFailure) {
1645
+ const injectScript = (path, handleSuccess, handleFailure) => {
1788
1646
  // inject a new script element into the page
1789
- var script = document.createElement("script");
1790
- script.onload = function() {
1647
+ const script = document.createElement("script");
1648
+ script.onload = () => {
1791
1649
  forEachInstance("handleUtils");
1792
1650
  if (handleSuccess) {
1793
1651
  handleSuccess();
1794
1652
  }
1795
1653
  };
1796
- script.onerror = function() {
1654
+ script.onerror = () => {
1797
1655
  forEachInstance("rejectUtilsScriptPromise");
1798
1656
  if (handleFailure) {
1799
1657
  handleFailure();
@@ -1805,7 +1663,7 @@
1805
1663
  document.body.appendChild(script);
1806
1664
  };
1807
1665
  // load the utils script
1808
- intlTelInputGlobals.loadUtils = function(path) {
1666
+ intlTelInputGlobals.loadUtils = path => {
1809
1667
  // 2 options:
1810
1668
  // 1) not already started loading (start)
1811
1669
  // 2) already started loading (do nothing - just wait for the onload callback to fire, which will
@@ -1813,16 +1671,14 @@
1813
1671
  if (!window.intlTelInputUtils && !window.intlTelInputGlobals.startedLoadingUtilsScript) {
1814
1672
  // only do this once
1815
1673
  window.intlTelInputGlobals.startedLoadingUtilsScript = true;
1816
- return new Promise(function(resolve, reject) {
1817
- return injectScript(path, resolve, reject);
1818
- });
1674
+ return new Promise((resolve, reject) => injectScript(path, resolve, reject));
1819
1675
  }
1820
1676
  return null;
1821
1677
  };
1822
1678
  // default options
1823
1679
  intlTelInputGlobals.defaults = defaults;
1824
1680
  // version
1825
- intlTelInputGlobals.version = "20.2.0";
1681
+ intlTelInputGlobals.version = "20.3.0";
1826
1682
  // convenience wrapper
1827
1683
  return function(input, options) {
1828
1684
  var iti = new Iti(input, options);