intl-tel-input 20.1.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.1.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) {
@@ -42,138 +41,21 @@
42
41
  nodeById: {}
43
42
  };
44
43
  }
45
- "use strict";
46
- function ownKeys(object, enumerableOnly) {
47
- var keys = Object.keys(object);
48
- if (Object.getOwnPropertySymbols) {
49
- var symbols = Object.getOwnPropertySymbols(object);
50
- enumerableOnly && (symbols = symbols.filter(function(sym) {
51
- return Object.getOwnPropertyDescriptor(object, sym).enumerable;
52
- })), keys.push.apply(keys, symbols);
53
- }
54
- return keys;
55
- }
56
- function _objectSpread(target) {
57
- for (var i = 1; i < arguments.length; i++) {
58
- var source = null != arguments[i] ? arguments[i] : {};
59
- i % 2 ? ownKeys(Object(source), !0).forEach(function(key) {
60
- _defineProperty(target, key, source[key]);
61
- }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function(key) {
62
- Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
63
- });
64
- }
65
- return target;
66
- }
67
- function _defineProperty(obj, key, value) {
68
- key = _toPropertyKey(key);
69
- if (key in obj) {
70
- Object.defineProperty(obj, key, {
71
- value: value,
72
- enumerable: true,
73
- configurable: true,
74
- writable: true
75
- });
76
- } else {
77
- obj[key] = value;
78
- }
79
- return obj;
80
- }
81
- function _slicedToArray(arr, i) {
82
- return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest();
83
- }
84
- function _nonIterableRest() {
85
- throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
86
- }
87
- function _unsupportedIterableToArray(o, minLen) {
88
- if (!o) return;
89
- if (typeof o === "string") return _arrayLikeToArray(o, minLen);
90
- var n = Object.prototype.toString.call(o).slice(8, -1);
91
- if (n === "Object" && o.constructor) n = o.constructor.name;
92
- if (n === "Map" || n === "Set") return Array.from(o);
93
- if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
94
- }
95
- function _arrayLikeToArray(arr, len) {
96
- if (len == null || len > arr.length) len = arr.length;
97
- for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i];
98
- return arr2;
99
- }
100
- function _iterableToArrayLimit(arr, i) {
101
- var _i = null == arr ? null : "undefined" != typeof Symbol && arr[Symbol.iterator] || arr["@@iterator"];
102
- if (null != _i) {
103
- var _s, _e, _x, _r, _arr = [], _n = !0, _d = !1;
104
- try {
105
- if (_x = (_i = _i.call(arr)).next, 0 === i) {
106
- if (Object(_i) !== _i) return;
107
- _n = !1;
108
- } else for (;!(_n = (_s = _x.call(_i)).done) && (_arr.push(_s.value), _arr.length !== i); _n = !0) ;
109
- } catch (err) {
110
- _d = !0, _e = err;
111
- } finally {
112
- try {
113
- if (!_n && null != _i["return"] && (_r = _i["return"](), Object(_r) !== _r)) return;
114
- } finally {
115
- if (_d) throw _e;
116
- }
117
- }
118
- return _arr;
119
- }
120
- }
121
- function _arrayWithHoles(arr) {
122
- if (Array.isArray(arr)) return arr;
123
- }
124
- function _classCallCheck(instance, Constructor) {
125
- if (!(instance instanceof Constructor)) {
126
- throw new TypeError("Cannot call a class as a function");
127
- }
128
- }
129
- function _defineProperties(target, props) {
130
- for (var i = 0; i < props.length; i++) {
131
- var descriptor = props[i];
132
- descriptor.enumerable = descriptor.enumerable || false;
133
- descriptor.configurable = true;
134
- if ("value" in descriptor) descriptor.writable = true;
135
- Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor);
136
- }
137
- }
138
- function _createClass(Constructor, protoProps, staticProps) {
139
- if (protoProps) _defineProperties(Constructor.prototype, protoProps);
140
- if (staticProps) _defineProperties(Constructor, staticProps);
141
- Object.defineProperty(Constructor, "prototype", {
142
- writable: false
143
- });
144
- return Constructor;
145
- }
146
- function _toPropertyKey(arg) {
147
- var key = _toPrimitive(arg, "string");
148
- return typeof key === "symbol" ? key : String(key);
149
- }
150
- function _toPrimitive(input, hint) {
151
- if (typeof input !== "object" || input === null) return input;
152
- var prim = input[Symbol.toPrimitive];
153
- if (prim !== undefined) {
154
- var res = prim.call(input, hint || "default");
155
- if (typeof res !== "object") return res;
156
- throw new TypeError("@@toPrimitive must return a primitive value.");
157
- }
158
- return (hint === "string" ? String : Number)(input);
159
- }
160
- var intlTelInputGlobals = {
161
- getInstance: function getInstance(input) {
162
- var id = input.getAttribute("data-intl-tel-input-id");
44
+ const intlTelInputGlobals = {
45
+ getInstance: input => {
46
+ const id = input.getAttribute("data-intl-tel-input-id");
163
47
  return window.intlTelInputGlobals.instances[id];
164
48
  },
165
49
  instances: {},
166
50
  // using a global like this allows us to mock it in the tests
167
- documentReady: function documentReady() {
168
- return document.readyState === "complete";
169
- }
51
+ documentReady: () => document.readyState === "complete"
170
52
  };
171
53
  if (typeof window === "object") {
172
54
  window.intlTelInputGlobals = intlTelInputGlobals;
173
55
  }
174
56
  // these vars persist through all instances of the plugin
175
- var id = 0;
176
- var defaults = {
57
+ let id = 0;
58
+ const defaults = {
177
59
  // whether or not to allow the dropdown
178
60
  allowDropdown: true,
179
61
  // add a placeholder in the input with an example number for the selected country
@@ -214,6 +96,8 @@
214
96
  showFlags: true,
215
97
  // display the international dial code next to the selected flag
216
98
  showSelectedDialCode: false,
99
+ // only allow certain chars e.g. a plus followed by numeric digits, and cap at max valid length
100
+ strictMode: false,
217
101
  // use full screen popup instead of dropdown for country list
218
102
  useFullscreenPopup: typeof navigator !== "undefined" && typeof window !== "undefined" ? // we cannot just test screen size as some smartphones/website meta tags will report desktop
219
103
  // resolutions
@@ -223,20 +107,18 @@
223
107
  utilsScript: ""
224
108
  };
225
109
  // https://en.wikipedia.org/wiki/List_of_North_American_Numbering_Plan_area_codes#Non-geographic_area_codes
226
- var regionlessNanpNumbers = [ "800", "822", "833", "844", "855", "866", "877", "880", "881", "882", "883", "884", "885", "886", "887", "888", "889" ];
110
+ const regionlessNanpNumbers = [ "800", "822", "833", "844", "855", "866", "877", "880", "881", "882", "883", "884", "885", "886", "887", "888", "889" ];
227
111
  // run a method on each instance of the plugin
228
- var forEachInstance = function forEachInstance(method) {
229
- var instances = window.intlTelInputGlobals.instances;
230
- Object.values(instances).forEach(function(instance) {
231
- return instance[method]();
232
- });
112
+ const forEachInstance = method => {
113
+ const {
114
+ instances
115
+ } = window.intlTelInputGlobals;
116
+ Object.values(instances).forEach(instance => instance[method]());
233
117
  };
234
118
  // this is our plugin class that we will create an instance of
235
119
  // eslint-disable-next-line no-unused-vars
236
- var Iti = /*#__PURE__*/ function() {
237
- function Iti(input) {
238
- var customOptions = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
239
- _classCallCheck(this, Iti);
120
+ class Iti {
121
+ constructor(input, customOptions = {}) {
240
122
  this.id = id++;
241
123
  this.telInput = input;
242
124
  this.activeItem = null;
@@ -245,1513 +127,1536 @@
245
127
  this.options = Object.assign({}, defaults, customOptions);
246
128
  this.hadInitialPlaceholder = Boolean(input.getAttribute("placeholder"));
247
129
  }
248
- _createClass(Iti, [ {
249
- key: "_init",
250
- value: function _init() {
251
- var _this = this;
252
- // if showing fullscreen popup, do not fix the width
253
- if (this.options.useFullscreenPopup) {
254
- this.options.fixDropdownWidth = false;
255
- }
256
- // when search enabled, we must fix the width else it would change with different results
257
- if (this.options.countrySearch && !this.options.useFullscreenPopup) {
258
- this.options.fixDropdownWidth = true;
259
- }
260
- // force showFlags=true if there's a dropdown and we're not displaying the dial code,
261
- // as otherwise you just have a down arrow on it's own which doesn't make sense
262
- var forceShowFlags = this.options.allowDropdown && !this.options.showSelectedDialCode;
263
- if (!this.options.showFlags && forceShowFlags) {
264
- this.options.showFlags = true;
265
- }
266
- // on mobile, we want a full screen dropdown, so we must append it to the body
267
- if (this.options.useFullscreenPopup && !this.options.dropdownContainer) {
268
- this.options.dropdownContainer = document.body;
269
- }
270
- // check if input has one parent with RTL
271
- this.isRTL = !!this.telInput.closest("[dir=rtl]");
272
- // these promises get resolved when their individual requests complete
273
- // this way the dev can do something like iti.promise.then(...) to know when all requests are
274
- // complete
275
- var autoCountryPromise = new Promise(function(resolve, reject) {
276
- _this.resolveAutoCountryPromise = resolve;
277
- _this.rejectAutoCountryPromise = reject;
278
- });
279
- var utilsScriptPromise = new Promise(function(resolve, reject) {
280
- _this.resolveUtilsScriptPromise = resolve;
281
- _this.rejectUtilsScriptPromise = reject;
282
- });
283
- this.promise = Promise.all([ autoCountryPromise, utilsScriptPromise ]);
284
- // in various situations there could be no country selected initially, but we need to be able
285
- // to assume this variable exists
286
- this.selectedCountryData = {};
287
- // process all the data: onlyCountries, excludeCountries, preferredCountries etc
288
- this._processCountryData();
289
- // generate the markup
290
- this._generateMarkup();
291
- // set the initial state of the input value and the selected flag
292
- this._setInitialState();
293
- // start all of the event listeners: input keydown, selectedFlag click
294
- this._initListeners();
295
- // utils script, and auto country
296
- this._initRequests();
297
- }
298
- }, {
299
- key: "_processCountryData",
300
- value: function _processCountryData() {
301
- // process onlyCountries or excludeCountries array if present
302
- this._processAllCountries();
303
- // generate this.dialCodes and this.dialCodeToIso2Map
304
- this._processDialCodes();
305
- // process the preferredCountries
306
- this._processPreferredCountries();
307
- // translate country names according to i18n option
308
- this._translateCountryNames();
309
- // sort countries by name
310
- if (this.options.onlyCountries.length || this.options.i18n) {
311
- this.countries.sort(this._countryNameSort);
312
- }
130
+ _init() {
131
+ // if showing fullscreen popup, do not fix the width
132
+ if (this.options.useFullscreenPopup) {
133
+ this.options.fixDropdownWidth = false;
134
+ }
135
+ // when search enabled, we must fix the width else it would change with different results
136
+ if (this.options.countrySearch && !this.options.useFullscreenPopup) {
137
+ this.options.fixDropdownWidth = true;
138
+ }
139
+ // force showFlags=true if there's a dropdown and we're not displaying the dial code,
140
+ // as otherwise you just have a down arrow on it's own which doesn't make sense
141
+ const forceShowFlags = this.options.allowDropdown && !this.options.showSelectedDialCode;
142
+ if (!this.options.showFlags && forceShowFlags) {
143
+ this.options.showFlags = true;
144
+ }
145
+ // on mobile, we want a full screen dropdown, so we must append it to the body
146
+ if (this.options.useFullscreenPopup && !this.options.dropdownContainer) {
147
+ this.options.dropdownContainer = document.body;
148
+ }
149
+ // check if input has one parent with RTL
150
+ this.isRTL = !!this.telInput.closest("[dir=rtl]");
151
+ // these promises get resolved when their individual requests complete
152
+ // this way the dev can do something like iti.promise.then(...) to know when all requests are
153
+ // complete
154
+ const autoCountryPromise = new Promise((resolve, reject) => {
155
+ this.resolveAutoCountryPromise = resolve;
156
+ this.rejectAutoCountryPromise = reject;
157
+ });
158
+ const utilsScriptPromise = new Promise((resolve, reject) => {
159
+ this.resolveUtilsScriptPromise = resolve;
160
+ this.rejectUtilsScriptPromise = reject;
161
+ });
162
+ this.promise = Promise.all([ autoCountryPromise, utilsScriptPromise ]);
163
+ // in various situations there could be no country selected initially, but we need to be able
164
+ // to assume this variable exists
165
+ this.selectedCountryData = {};
166
+ // process all the data: onlyCountries, excludeCountries, preferredCountries etc
167
+ this._processCountryData();
168
+ // generate the markup
169
+ this._generateMarkup();
170
+ // set the initial state of the input value and the selected flag
171
+ this._setInitialState();
172
+ // start all of the event listeners: input keydown, selectedFlag click
173
+ this._initListeners();
174
+ // utils script, and auto country
175
+ this._initRequests();
176
+ }
177
+ /********************
178
+ * PRIVATE METHODS
179
+ ********************/
180
+ // prepare all of the country data, including onlyCountries, excludeCountries and
181
+ // preferredCountries options
182
+ _processCountryData() {
183
+ // process onlyCountries or excludeCountries array if present
184
+ this._processAllCountries();
185
+ // generate this.dialCodes and this.dialCodeToIso2Map
186
+ this._processDialCodes();
187
+ // process the preferredCountries
188
+ this._processPreferredCountries();
189
+ // translate country names according to i18n option
190
+ this._translateCountryNames();
191
+ // sort countries by name
192
+ if (this.options.onlyCountries.length || this.options.i18n) {
193
+ this.countries.sort(this._countryNameSort);
313
194
  }
314
- }, {
315
- key: "_addToDialCodeMap",
316
- value: function _addToDialCodeMap(iso2, dialCode, priority) {
317
- if (dialCode.length > this.dialCodeMaxLen) {
318
- this.dialCodeMaxLen = dialCode.length;
319
- }
320
- if (!this.dialCodeToIso2Map.hasOwnProperty(dialCode)) {
321
- this.dialCodeToIso2Map[dialCode] = [];
322
- }
323
- // bail if we already have this country for this dialCode
324
- for (var i = 0; i < this.dialCodeToIso2Map[dialCode].length; i++) {
325
- if (this.dialCodeToIso2Map[dialCode][i] === iso2) {
326
- return;
327
- }
328
- }
329
- // check for undefined as 0 is falsy
330
- var index = priority !== undefined ? priority : this.dialCodeToIso2Map[dialCode].length;
331
- this.dialCodeToIso2Map[dialCode][index] = iso2;
332
- }
333
- }, {
334
- key: "_processAllCountries",
335
- value: function _processAllCountries() {
336
- if (this.options.onlyCountries.length) {
337
- var lowerCaseOnlyCountries = this.options.onlyCountries.map(function(country) {
338
- return country.toLowerCase();
339
- });
340
- this.countries = allCountries.filter(function(country) {
341
- return lowerCaseOnlyCountries.indexOf(country.iso2) > -1;
342
- });
343
- } else if (this.options.excludeCountries.length) {
344
- var lowerCaseExcludeCountries = this.options.excludeCountries.map(function(country) {
345
- return country.toLowerCase();
346
- });
347
- this.countries = allCountries.filter(function(country) {
348
- return lowerCaseExcludeCountries.indexOf(country.iso2) === -1;
349
- });
350
- } else {
351
- this.countries = allCountries;
352
- }
195
+ }
196
+ // add a dial code to this.dialCodeToIso2Map
197
+ _addToDialCodeMap(iso2, dialCode, priority) {
198
+ if (dialCode.length > this.dialCodeMaxLen) {
199
+ this.dialCodeMaxLen = dialCode.length;
353
200
  }
354
- }, {
355
- key: "_translateCountryNames",
356
- value: function _translateCountryNames() {
357
- for (var i = 0; i < this.countries.length; i++) {
358
- var iso2 = this.countries[i].iso2.toLowerCase();
359
- if (this.options.i18n.hasOwnProperty(iso2)) {
360
- this.countries[i].name = this.options.i18n[iso2];
361
- }
362
- }
201
+ if (!this.dialCodeToIso2Map.hasOwnProperty(dialCode)) {
202
+ this.dialCodeToIso2Map[dialCode] = [];
363
203
  }
364
- }, {
365
- key: "_countryNameSort",
366
- value: function _countryNameSort(a, b) {
367
- if (a.name < b.name) {
368
- return -1;
369
- }
370
- if (a.name > b.name) {
371
- return 1;
204
+ // bail if we already have this country for this dialCode
205
+ for (let i = 0; i < this.dialCodeToIso2Map[dialCode].length; i++) {
206
+ if (this.dialCodeToIso2Map[dialCode][i] === iso2) {
207
+ return;
372
208
  }
373
- return 0;
374
209
  }
375
- }, {
376
- key: "_processDialCodes",
377
- value: function _processDialCodes() {
378
- // here we store just dial codes, where the key is the dial code, and the value is true
379
- // e.g. { 1: true, 7: true, 20: true, ... }
380
- this.dialCodes = {};
381
- this.dialCodeMaxLen = 0;
382
- // here we map dialCodes (inc both dialCode and dialCode+areaCode) to iso2 codes
383
- /* e.g.
384
- * {
385
- * 1: [ 'us', 'ca', ... ], # all NANP countries
386
- * 12: [ 'us', 'ca', ... ], # subset of NANP countries
387
- * 120: [ 'us', 'ca' ], # just US and Canada
388
- * 1204: [ 'ca' ], # only Canada
389
- * ...
390
- * }
391
- */
392
- this.dialCodeToIso2Map = {};
393
- // first: add dial codes
394
- for (var i = 0; i < this.countries.length; i++) {
395
- var c = this.countries[i];
396
- if (!this.dialCodes[c.dialCode]) {
397
- this.dialCodes[c.dialCode] = true;
398
- }
399
- this._addToDialCodeMap(c.iso2, c.dialCode, c.priority);
210
+ // check for undefined as 0 is falsy
211
+ const index = priority !== undefined ? priority : this.dialCodeToIso2Map[dialCode].length;
212
+ this.dialCodeToIso2Map[dialCode][index] = iso2;
213
+ }
214
+ // process onlyCountries or excludeCountries array if present
215
+ _processAllCountries() {
216
+ if (this.options.onlyCountries.length) {
217
+ const lowerCaseOnlyCountries = this.options.onlyCountries.map(country => country.toLowerCase());
218
+ this.countries = allCountries.filter(country => lowerCaseOnlyCountries.indexOf(country.iso2) > -1);
219
+ } else if (this.options.excludeCountries.length) {
220
+ const lowerCaseExcludeCountries = this.options.excludeCountries.map(country => country.toLowerCase());
221
+ this.countries = allCountries.filter(country => lowerCaseExcludeCountries.indexOf(country.iso2) === -1);
222
+ } else {
223
+ this.countries = allCountries;
224
+ }
225
+ }
226
+ // Translate Countries by object literal provided on config
227
+ _translateCountryNames() {
228
+ for (let i = 0; i < this.countries.length; i++) {
229
+ const iso2 = this.countries[i].iso2.toLowerCase();
230
+ if (this.options.i18n.hasOwnProperty(iso2)) {
231
+ this.countries[i].name = this.options.i18n[iso2];
400
232
  }
401
- // next: add area codes
402
- // this is a second loop over countries, to make sure we have all of the "root" countries
403
- // already in the map, so that we can access them, as each time we add an area code substring
404
- // to the map, we also need to include the "root" country's code, as that also matches
405
- for (var _i = 0; _i < this.countries.length; _i++) {
406
- var _c = this.countries[_i];
407
- // area codes
408
- if (_c.areaCodes) {
409
- var rootIso2Code = this.dialCodeToIso2Map[_c.dialCode][0];
410
- // for each area code
411
- for (var j = 0; j < _c.areaCodes.length; j++) {
412
- var areaCode = _c.areaCodes[j];
413
- // for each digit in the area code to add all partial matches as well
414
- for (var k = 1; k < areaCode.length; k++) {
415
- var partialDialCode = _c.dialCode + areaCode.substr(0, k);
416
- // start with the root country, as that also matches this dial code
417
- this._addToDialCodeMap(rootIso2Code, partialDialCode);
418
- this._addToDialCodeMap(_c.iso2, partialDialCode);
419
- }
420
- // add the full area code
421
- this._addToDialCodeMap(_c.iso2, _c.dialCode + areaCode);
233
+ }
234
+ }
235
+ // sort by country name
236
+ _countryNameSort(a, b) {
237
+ if (a.name < b.name) {
238
+ return -1;
239
+ }
240
+ if (a.name > b.name) {
241
+ return 1;
242
+ }
243
+ return 0;
244
+ }
245
+ // generate this.dialCodes and this.dialCodeToIso2Map
246
+ _processDialCodes() {
247
+ // here we store just dial codes, where the key is the dial code, and the value is true
248
+ // e.g. { 1: true, 7: true, 20: true, ... }
249
+ this.dialCodes = {};
250
+ this.dialCodeMaxLen = 0;
251
+ // here we map dialCodes (inc both dialCode and dialCode+areaCode) to iso2 codes
252
+ /* e.g.
253
+ * {
254
+ * 1: [ 'us', 'ca', ... ], # all NANP countries
255
+ * 12: [ 'us', 'ca', ... ], # subset of NANP countries
256
+ * 120: [ 'us', 'ca' ], # just US and Canada
257
+ * 1204: [ 'ca' ], # only Canada
258
+ * ...
259
+ * }
260
+ */
261
+ this.dialCodeToIso2Map = {};
262
+ // first: add dial codes
263
+ for (let i = 0; i < this.countries.length; i++) {
264
+ const c = this.countries[i];
265
+ if (!this.dialCodes[c.dialCode]) {
266
+ this.dialCodes[c.dialCode] = true;
267
+ }
268
+ this._addToDialCodeMap(c.iso2, c.dialCode, c.priority);
269
+ }
270
+ // next: add area codes
271
+ // this is a second loop over countries, to make sure we have all of the "root" countries
272
+ // already in the map, so that we can access them, as each time we add an area code substring
273
+ // to the map, we also need to include the "root" country's code, as that also matches
274
+ for (let i = 0; i < this.countries.length; i++) {
275
+ const c = this.countries[i];
276
+ // area codes
277
+ if (c.areaCodes) {
278
+ const rootIso2Code = this.dialCodeToIso2Map[c.dialCode][0];
279
+ // for each area code
280
+ for (let j = 0; j < c.areaCodes.length; j++) {
281
+ const areaCode = c.areaCodes[j];
282
+ // for each digit in the area code to add all partial matches as well
283
+ for (let k = 1; k < areaCode.length; k++) {
284
+ const partialDialCode = c.dialCode + areaCode.substr(0, k);
285
+ // start with the root country, as that also matches this dial code
286
+ this._addToDialCodeMap(rootIso2Code, partialDialCode);
287
+ this._addToDialCodeMap(c.iso2, partialDialCode);
422
288
  }
289
+ // add the full area code
290
+ this._addToDialCodeMap(c.iso2, c.dialCode + areaCode);
423
291
  }
424
292
  }
425
293
  }
426
- }, {
427
- key: "_processPreferredCountries",
428
- value: function _processPreferredCountries() {
429
- this.preferredCountries = [];
430
- for (var i = 0; i < this.options.preferredCountries.length; i++) {
431
- var iso2 = this.options.preferredCountries[i].toLowerCase();
432
- var countryData = this._getCountryData(iso2, true);
433
- if (countryData) {
434
- this.preferredCountries.push(countryData);
435
- }
294
+ }
295
+ // process preferred countries - iterate through the preferences, fetching the country data for
296
+ // each one
297
+ _processPreferredCountries() {
298
+ this.preferredCountries = [];
299
+ for (let i = 0; i < this.options.preferredCountries.length; i++) {
300
+ const iso2 = this.options.preferredCountries[i].toLowerCase();
301
+ const countryData = this._getCountryData(iso2, true);
302
+ if (countryData) {
303
+ this.preferredCountries.push(countryData);
436
304
  }
437
305
  }
438
- }, {
439
- key: "_createEl",
440
- value: function _createEl(name, attrs, container) {
441
- var el = document.createElement(name);
442
- if (attrs) {
443
- Object.entries(attrs).forEach(function(_ref) {
444
- var _ref2 = _slicedToArray(_ref, 2), key = _ref2[0], value = _ref2[1];
445
- return el.setAttribute(key, value);
446
- });
447
- }
448
- if (container) {
449
- container.appendChild(el);
450
- }
451
- return el;
452
- }
453
- }, {
454
- key: "_generateMarkup",
455
- value: function _generateMarkup() {
456
- this.telInput.classList.add("iti__tel-input");
457
- // if autocomplete does not exist on the element and its form, then
458
- // prevent autocomplete as there's no safe, cross-browser event we can react to, so it can
459
- // easily put the plugin in an inconsistent state e.g. the wrong flag selected for the
460
- // autocompleted number, which on submit could mean wrong number is saved
461
- if (!this.telInput.hasAttribute("autocomplete") && !(this.telInput.form && this.telInput.form.hasAttribute("autocomplete"))) {
462
- this.telInput.setAttribute("autocomplete", "off");
463
- }
464
- 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;
465
- // containers (mostly for positioning)
466
- var parentClass = "iti";
467
- if (allowDropdown) {
468
- parentClass += " iti--allow-dropdown";
469
- }
470
- if (showSelectedDialCode) {
471
- parentClass += " iti--show-selected-dial-code";
472
- }
473
- if (showFlags) {
474
- parentClass += " iti--show-flags";
475
- }
476
- if (containerClass) {
477
- parentClass += " ".concat(containerClass);
478
- }
479
- if (!useFullscreenPopup) {
480
- parentClass += " iti--inline-dropdown";
481
- }
482
- var wrapper = this._createEl("div", {
483
- "class": parentClass
484
- });
485
- this.telInput.parentNode.insertBefore(wrapper, this.telInput);
486
- // if we're showing flags or dial codes, we need the flags container etc
487
- if (showFlags || showSelectedDialCode) {
488
- this.flagsContainer = this._createEl("div", {
489
- "class": "iti__flag-container"
490
- }, wrapper);
491
- // selected flag (displayed on left of input while allowDropdown is enabled, otherwise to right)
492
- // when countrySearch disabled: using Aria tags for "Select-Only Combobox Example"
493
- // https://www.w3.org/WAI/ARIA/apg/patterns/combobox/examples/combobox-select-only/
494
- this.selectedFlag = this._createEl("div", _objectSpread({
495
- "class": "iti__selected-flag"
496
- }, allowDropdown && _objectSpread({
306
+ }
307
+ // create a DOM element
308
+ _createEl(name, attrs, container) {
309
+ const el = document.createElement(name);
310
+ if (attrs) {
311
+ Object.entries(attrs).forEach(([ key, value ]) => el.setAttribute(key, value));
312
+ }
313
+ if (container) {
314
+ container.appendChild(el);
315
+ }
316
+ return el;
317
+ }
318
+ // generate all of the markup for the plugin: the selected flag overlay, and the dropdown
319
+ _generateMarkup() {
320
+ this.telInput.classList.add("iti__tel-input");
321
+ // if autocomplete does not exist on the element and its form, then
322
+ // prevent autocomplete as there's no safe, cross-browser event we can react to, so it can
323
+ // easily put the plugin in an inconsistent state e.g. the wrong flag selected for the
324
+ // autocompleted number, which on submit could mean wrong number is saved
325
+ if (!this.telInput.hasAttribute("autocomplete") && !(this.telInput.form && this.telInput.form.hasAttribute("autocomplete"))) {
326
+ this.telInput.setAttribute("autocomplete", "off");
327
+ }
328
+ const {
329
+ allowDropdown,
330
+ showSelectedDialCode,
331
+ showFlags,
332
+ containerClass,
333
+ hiddenInput,
334
+ dropdownContainer,
335
+ fixDropdownWidth,
336
+ useFullscreenPopup,
337
+ countrySearch,
338
+ i18n
339
+ } = this.options;
340
+ // containers (mostly for positioning)
341
+ let parentClass = "iti";
342
+ if (allowDropdown) {
343
+ parentClass += " iti--allow-dropdown";
344
+ }
345
+ if (showSelectedDialCode) {
346
+ parentClass += " iti--show-selected-dial-code";
347
+ }
348
+ if (showFlags) {
349
+ parentClass += " iti--show-flags";
350
+ }
351
+ if (containerClass) {
352
+ parentClass += ` ${containerClass}`;
353
+ }
354
+ if (!useFullscreenPopup) {
355
+ parentClass += " iti--inline-dropdown";
356
+ }
357
+ const wrapper = this._createEl("div", {
358
+ class: parentClass
359
+ });
360
+ this.telInput.parentNode.insertBefore(wrapper, this.telInput);
361
+ // if we're showing flags or dial codes, we need the flags container etc
362
+ if (showFlags || showSelectedDialCode) {
363
+ this.flagsContainer = this._createEl("div", {
364
+ class: "iti__flag-container"
365
+ }, wrapper);
366
+ // selected flag (displayed on left of input while allowDropdown is enabled, otherwise to right)
367
+ // when countrySearch disabled: using Aria tags for "Select-Only Combobox Example"
368
+ // https://www.w3.org/WAI/ARIA/apg/patterns/combobox/examples/combobox-select-only/
369
+ this.selectedFlag = this._createEl("div", {
370
+ class: "iti__selected-flag",
371
+ ...allowDropdown && {
497
372
  role: "button",
498
373
  "aria-expanded": "false",
499
374
  "aria-label": this.options.i18n.selectedCountryAriaLabel || "Selected country",
500
375
  "aria-haspopup": countrySearch ? "true" : "listbox",
501
- "aria-controls": countrySearch ? "iti-".concat(this.id, "__dropdown-content") : "iti-".concat(this.id, "__country-listbox")
502
- }, countrySearch || {
503
- role: "combobox"
504
- })), this.flagsContainer);
505
- // we now include the selected flag element even when showFlags is disabled,
506
- // as need to show globe icon for showSelectedDialCode empty state
507
- this.selectedFlagInner = this._createEl("div", null, this.selectedFlag);
508
- this.selectedFlagA11yText = this._createEl("span", {
509
- "class": "iti__a11y-text"
510
- }, this.selectedFlagInner);
376
+ "aria-controls": countrySearch ? `iti-${this.id}__dropdown-content` : `iti-${this.id}__country-listbox`,
377
+ ...countrySearch || {
378
+ role: "combobox"
379
+ }
380
+ }
381
+ }, this.flagsContainer);
382
+ // we now include the selected flag element even when showFlags is disabled,
383
+ // as need to show globe icon for showSelectedDialCode empty state
384
+ this.selectedFlagInner = this._createEl("div", null, this.selectedFlag);
385
+ this.selectedFlagA11yText = this._createEl("span", {
386
+ class: "iti__a11y-text"
387
+ }, this.selectedFlagInner);
388
+ }
389
+ wrapper.appendChild(this.telInput);
390
+ if (this.selectedFlag && this.telInput.disabled) {
391
+ this.selectedFlag.setAttribute("aria-disabled", "true");
392
+ }
393
+ if (showSelectedDialCode) {
394
+ this.selectedDialCode = this._createEl("div", {
395
+ class: "iti__selected-dial-code"
396
+ }, this.selectedFlag);
397
+ }
398
+ if (allowDropdown) {
399
+ if (!this.telInput.disabled) {
400
+ // make element focusable and tab navigable
401
+ this.selectedFlag.setAttribute("tabindex", "0");
402
+ }
403
+ this.dropdownArrow = this._createEl("div", {
404
+ class: "iti__arrow",
405
+ "aria-hidden": "true"
406
+ }, this.selectedFlag);
407
+ const extraClasses = fixDropdownWidth ? "" : "iti--flexible-dropdown-width";
408
+ this.dropdownContent = this._createEl("div", {
409
+ id: `iti-${this.id}__dropdown-content`,
410
+ class: `iti__dropdown-content iti__hide ${extraClasses}`
411
+ });
412
+ if (countrySearch) {
413
+ this.searchInput = this._createEl("input", {
414
+ type: "text",
415
+ class: "iti__search-input",
416
+ placeholder: i18n.searchPlaceholder || "Search",
417
+ role: "combobox",
418
+ "aria-expanded": "true",
419
+ "aria-label": i18n.searchPlaceholder || "Search",
420
+ "aria-controls": `iti-${this.id}__country-listbox`,
421
+ "aria-autocomplete": "list",
422
+ autocomplete: "off"
423
+ }, this.dropdownContent);
424
+ this.searchResultsA11yText = this._createEl("span", {
425
+ class: "iti__a11y-text"
426
+ }, this.dropdownContent);
511
427
  }
512
- wrapper.appendChild(this.telInput);
513
- if (this.selectedFlag && this.telInput.disabled) {
514
- this.selectedFlag.setAttribute("aria-disabled", "true");
428
+ // country list: preferred countries, then divider, then all countries
429
+ this.countryList = this._createEl("ul", {
430
+ class: "iti__country-list",
431
+ id: `iti-${this.id}__country-listbox`,
432
+ role: "listbox",
433
+ "aria-label": i18n.countryListAriaLabel || "List of countries"
434
+ }, this.dropdownContent);
435
+ if (this.preferredCountries.length && !countrySearch) {
436
+ this._appendListItems(this.preferredCountries, "iti__preferred", true);
437
+ this._createEl("li", {
438
+ class: "iti__divider",
439
+ "aria-hidden": "true"
440
+ }, this.countryList);
515
441
  }
516
- if (showSelectedDialCode) {
517
- this.selectedDialCode = this._createEl("div", {
518
- "class": "iti__selected-dial-code"
519
- }, this.selectedFlag);
442
+ this._appendListItems(this.countries, "iti__standard");
443
+ if (countrySearch) {
444
+ this._updateSearchResultsText();
520
445
  }
521
- if (allowDropdown) {
522
- if (!this.telInput.disabled) {
523
- // make element focusable and tab navigable
524
- this.selectedFlag.setAttribute("tabindex", "0");
525
- }
526
- this.dropdownArrow = this._createEl("div", {
527
- "class": "iti__arrow",
528
- "aria-hidden": "true"
529
- }, this.selectedFlag);
530
- var extraClasses = fixDropdownWidth ? "" : "iti--flexible-dropdown-width";
531
- this.dropdownContent = this._createEl("div", {
532
- id: "iti-".concat(this.id, "__dropdown-content"),
533
- "class": "iti__dropdown-content iti__hide ".concat(extraClasses)
534
- });
535
- if (countrySearch) {
536
- this.searchInput = this._createEl("input", {
537
- type: "text",
538
- "class": "iti__search-input",
539
- placeholder: i18n.searchPlaceholder || "Search",
540
- role: "combobox",
541
- "aria-expanded": "true",
542
- "aria-label": i18n.searchPlaceholder || "Search",
543
- "aria-controls": "iti-".concat(this.id, "__country-listbox"),
544
- "aria-autocomplete": "list",
545
- autocomplete: "off"
546
- }, this.dropdownContent);
547
- this.searchResultsA11yText = this._createEl("span", {
548
- "class": "iti__a11y-text"
549
- }, this.dropdownContent);
550
- }
551
- // country list: preferred countries, then divider, then all countries
552
- this.countryList = this._createEl("ul", {
553
- "class": "iti__country-list",
554
- id: "iti-".concat(this.id, "__country-listbox"),
555
- role: "listbox",
556
- "aria-label": i18n.countryListAriaLabel || "List of countries"
557
- }, this.dropdownContent);
558
- if (this.preferredCountries.length && !countrySearch) {
559
- this._appendListItems(this.preferredCountries, "iti__preferred", true);
560
- this._createEl("li", {
561
- "class": "iti__divider",
562
- "aria-hidden": "true"
563
- }, this.countryList);
564
- }
565
- this._appendListItems(this.countries, "iti__standard");
566
- if (countrySearch) {
567
- this._updateSearchResultsText();
568
- }
569
- // create dropdownContainer markup
570
- if (dropdownContainer) {
571
- var dropdownClasses = "iti iti--container";
572
- if (useFullscreenPopup) {
573
- dropdownClasses += " iti--fullscreen-popup";
574
- } else {
575
- dropdownClasses += " iti--inline-dropdown";
576
- }
577
- if (countrySearch) {
578
- dropdownClasses += " iti--country-search";
579
- }
580
- this.dropdown = this._createEl("div", {
581
- "class": dropdownClasses
582
- });
583
- this.dropdown.appendChild(this.dropdownContent);
446
+ // create dropdownContainer markup
447
+ if (dropdownContainer) {
448
+ let dropdownClasses = "iti iti--container";
449
+ if (useFullscreenPopup) {
450
+ dropdownClasses += " iti--fullscreen-popup";
584
451
  } else {
585
- this.flagsContainer.appendChild(this.dropdownContent);
586
- }
587
- }
588
- if (hiddenInput) {
589
- var telInputName = this.telInput.getAttribute("name");
590
- var names = hiddenInput(telInputName);
591
- if (names.phone) {
592
- // Create hidden input for the full international number
593
- this.hiddenInput = this._createEl("input", {
594
- type: "hidden",
595
- name: names.phone
596
- });
597
- wrapper.appendChild(this.hiddenInput);
452
+ dropdownClasses += " iti--inline-dropdown";
598
453
  }
599
- if (names.country) {
600
- // Create hidden input for the selected country iso2 code
601
- this.hiddenInputCountry = this._createEl("input", {
602
- type: "hidden",
603
- name: names.country
604
- });
605
- wrapper.appendChild(this.hiddenInputCountry);
454
+ if (countrySearch) {
455
+ dropdownClasses += " iti--country-search";
606
456
  }
457
+ this.dropdown = this._createEl("div", {
458
+ class: dropdownClasses
459
+ });
460
+ this.dropdown.appendChild(this.dropdownContent);
461
+ } else {
462
+ this.flagsContainer.appendChild(this.dropdownContent);
607
463
  }
608
464
  }
609
- }, {
610
- key: "_appendListItems",
611
- value: function _appendListItems(countries, className, preferred) {
612
- for (var i = 0; i < countries.length; i++) {
613
- var c = countries[i];
614
- var idSuffix = preferred ? "-preferred" : "";
615
- var listItem = this._createEl("li", {
616
- id: "iti-".concat(this.id, "__item-").concat(c.iso2).concat(idSuffix),
617
- "class": "iti__country ".concat(className),
618
- tabindex: "-1",
619
- role: "option",
620
- "data-dial-code": c.dialCode,
621
- "data-country-code": c.iso2,
622
- "aria-selected": "false"
623
- }, this.countryList);
624
- // store this for later use e.g. country search filtering
625
- c.nodeById[this.id] = listItem;
626
- var content = "";
627
- // add the flag
628
- if (this.options.showFlags) {
629
- content += "<div class='iti__flag-box'><div class='iti__flag iti__".concat(c.iso2, "'></div></div>");
630
- }
631
- // and the country name and dial code
632
- content += "<span class='iti__country-name'>".concat(c.name, "</span>");
633
- content += "<span class='iti__dial-code'>+".concat(c.dialCode, "</span>");
634
- listItem.insertAdjacentHTML("beforeend", content);
465
+ if (hiddenInput) {
466
+ const telInputName = this.telInput.getAttribute("name");
467
+ const names = hiddenInput(telInputName);
468
+ if (names.phone) {
469
+ // Create hidden input for the full international number
470
+ this.hiddenInput = this._createEl("input", {
471
+ type: "hidden",
472
+ name: names.phone
473
+ });
474
+ wrapper.appendChild(this.hiddenInput);
475
+ }
476
+ if (names.country) {
477
+ // Create hidden input for the selected country iso2 code
478
+ this.hiddenInputCountry = this._createEl("input", {
479
+ type: "hidden",
480
+ name: names.country
481
+ });
482
+ wrapper.appendChild(this.hiddenInputCountry);
635
483
  }
636
484
  }
637
- }, {
638
- key: "_setInitialState",
639
- value: function _setInitialState() {
640
- var overrideAutoCountry = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
641
- // fix firefox bug: when first load page (with input with value set to number with intl dial
642
- // code) and initialising plugin removes the dial code from the input, then refresh page,
643
- // and we try to init plugin again but this time on number without dial code so get grey flag
644
- var attributeValue = this.telInput.getAttribute("value");
645
- var inputValue = this.telInput.value;
646
- var useAttribute = attributeValue && attributeValue.charAt(0) === "+" && (!inputValue || inputValue.charAt(0) !== "+");
647
- var val = useAttribute ? attributeValue : inputValue;
648
- var dialCode = this._getDialCode(val);
649
- var isRegionlessNanp = this._isRegionlessNanp(val);
650
- var initialCountry = this.options.initialCountry;
651
- // if we already have a dial code, and it's not a regionlessNanp, we can go ahead and set the
652
- // flag, else fall back to the default country
653
- if (dialCode && !isRegionlessNanp) {
654
- this._updateFlagFromNumber(val);
655
- } else if (initialCountry !== "auto" || overrideAutoCountry) {
656
- var lowerInitialCountry = initialCountry ? initialCountry.toLowerCase() : "";
657
- var isValidInitialCountry = lowerInitialCountry && this._getCountryData(lowerInitialCountry, true);
658
- // see if we should select a flag
659
- if (isValidInitialCountry) {
660
- this._setCountry(lowerInitialCountry);
485
+ }
486
+ // for each of the passed countries: add a country <li> to the countryList <ul> container
487
+ _appendListItems(countries, className, preferred) {
488
+ for (let i = 0; i < countries.length; i++) {
489
+ const c = countries[i];
490
+ const idSuffix = preferred ? "-preferred" : "";
491
+ const listItem = this._createEl("li", {
492
+ id: `iti-${this.id}__item-${c.iso2}${idSuffix}`,
493
+ class: `iti__country ${className}`,
494
+ tabindex: "-1",
495
+ role: "option",
496
+ "data-dial-code": c.dialCode,
497
+ "data-country-code": c.iso2,
498
+ "aria-selected": "false"
499
+ }, this.countryList);
500
+ // store this for later use e.g. country search filtering
501
+ c.nodeById[this.id] = listItem;
502
+ let content = "";
503
+ // add the flag
504
+ if (this.options.showFlags) {
505
+ content += `<div class='iti__flag-box'><div class='iti__flag iti__${c.iso2}'></div></div>`;
506
+ }
507
+ // and the country name and dial code
508
+ content += `<span class='iti__country-name'>${c.name}</span>`;
509
+ content += `<span class='iti__dial-code'>+${c.dialCode}</span>`;
510
+ listItem.insertAdjacentHTML("beforeend", content);
511
+ }
512
+ }
513
+ // set the initial state of the input value and the selected flag by:
514
+ // 1. extracting a dial code from the given number
515
+ // 2. using explicit initialCountry
516
+ // 3. picking the first preferred country
517
+ // 4. picking the first country
518
+ _setInitialState(overrideAutoCountry = false) {
519
+ // fix firefox bug: when first load page (with input with value set to number with intl dial
520
+ // code) and initialising plugin removes the dial code from the input, then refresh page,
521
+ // and we try to init plugin again but this time on number without dial code so get grey flag
522
+ const attributeValue = this.telInput.getAttribute("value");
523
+ const inputValue = this.telInput.value;
524
+ const useAttribute = attributeValue && attributeValue.charAt(0) === "+" && (!inputValue || inputValue.charAt(0) !== "+");
525
+ const val = useAttribute ? attributeValue : inputValue;
526
+ const dialCode = this._getDialCode(val);
527
+ const isRegionlessNanp = this._isRegionlessNanp(val);
528
+ const {
529
+ initialCountry
530
+ } = this.options;
531
+ // if we already have a dial code, and it's not a regionlessNanp, we can go ahead and set the
532
+ // flag, else fall back to the default country
533
+ if (dialCode && !isRegionlessNanp) {
534
+ this._updateFlagFromNumber(val);
535
+ } else if (initialCountry !== "auto" || overrideAutoCountry) {
536
+ const lowerInitialCountry = initialCountry ? initialCountry.toLowerCase() : "";
537
+ const isValidInitialCountry = lowerInitialCountry && this._getCountryData(lowerInitialCountry, true);
538
+ // see if we should select a flag
539
+ if (isValidInitialCountry) {
540
+ this._setCountry(lowerInitialCountry);
541
+ } else {
542
+ if (dialCode && isRegionlessNanp) {
543
+ // has intl dial code, is regionless nanp, and no initialCountry, so default to US
544
+ this._setCountry("us");
661
545
  } else {
662
- if (dialCode && isRegionlessNanp) {
663
- // has intl dial code, is regionless nanp, and no initialCountry, so default to US
664
- this._setCountry("us");
665
- } else {
666
- // display the empty state (globe icon)
667
- this._setCountry();
668
- }
546
+ // display the empty state (globe icon)
547
+ this._setCountry();
669
548
  }
670
549
  }
671
- // NOTE: if initialCountry is set to auto, that will be handled separately
672
- // format - note this wont be run after _updateDialCode as that's only called if no val
673
- if (val) {
674
- this._updateValFromNumber(val);
675
- }
676
550
  }
677
- }, {
678
- key: "_initListeners",
679
- value: function _initListeners() {
680
- this._initKeyListeners();
681
- if (this.options.allowDropdown) {
682
- this._initDropdownListeners();
683
- }
684
- if ((this.hiddenInput || this.hiddenInputCountry) && this.telInput.form) {
685
- this._initHiddenInputListener();
686
- }
551
+ // NOTE: if initialCountry is set to auto, that will be handled separately
552
+ // format - note this wont be run after _updateDialCode as that's only called if no val
553
+ if (val) {
554
+ this._updateValFromNumber(val);
687
555
  }
688
- }, {
689
- key: "_initHiddenInputListener",
690
- value: function _initHiddenInputListener() {
691
- var _this2 = this;
692
- this._handleHiddenInputSubmit = function() {
693
- if (_this2.hiddenInput) {
694
- _this2.hiddenInput.value = _this2.getNumber();
695
- }
696
- if (_this2.hiddenInputCountry) {
697
- _this2.hiddenInputCountry.value = _this2.getSelectedCountryData().iso2;
698
- }
699
- };
700
- this.telInput.form.addEventListener("submit", this._handleHiddenInputSubmit);
701
- }
702
- }, {
703
- key: "_initDropdownListeners",
704
- value: function _initDropdownListeners() {
705
- var _this3 = this;
706
- // hack for input nested inside label (which is valid markup): clicking the selected-flag to
707
- // open the dropdown would then automatically trigger a 2nd click on the input which would
708
- // close it again
709
- this._handleLabelClick = function(e) {
710
- // if the dropdown is closed, then focus the input, else ignore the click
711
- if (_this3.dropdownContent.classList.contains("iti__hide")) {
712
- _this3.telInput.focus();
713
- } else {
714
- e.preventDefault();
715
- }
716
- };
717
- var label = this.telInput.closest("label");
718
- if (label) {
719
- label.addEventListener("click", this._handleLabelClick);
556
+ }
557
+ // initialise the main event listeners: input keyup, and click selected flag
558
+ _initListeners() {
559
+ this._initTelInputListeners();
560
+ if (this.options.allowDropdown) {
561
+ this._initDropdownListeners();
562
+ }
563
+ if ((this.hiddenInput || this.hiddenInputCountry) && this.telInput.form) {
564
+ this._initHiddenInputListener();
565
+ }
566
+ }
567
+ // update hidden input on form submit
568
+ _initHiddenInputListener() {
569
+ this._handleHiddenInputSubmit = () => {
570
+ if (this.hiddenInput) {
571
+ this.hiddenInput.value = this.getNumber();
720
572
  }
721
- // toggle country dropdown on click
722
- this._handleClickSelectedFlag = function() {
723
- // only intercept this event if we're opening the dropdown
724
- // else let it bubble up to the top ("click-off-to-close" listener)
725
- // we cannot just stopPropagation as it may be needed to close another instance
726
- if (_this3.dropdownContent.classList.contains("iti__hide") && !_this3.telInput.disabled && !_this3.telInput.readOnly) {
727
- _this3._openDropdown();
728
- }
729
- };
730
- this.selectedFlag.addEventListener("click", this._handleClickSelectedFlag);
731
- // open dropdown if selected flag is focused and they press up/down/space/enter
732
- this._handleFlagsContainerKeydown = function(e) {
733
- var isDropdownHidden = _this3.dropdownContent.classList.contains("iti__hide");
734
- if (isDropdownHidden && [ "ArrowUp", "ArrowDown", " ", "Enter" ].includes(e.key)) {
735
- // prevent form from being submitted if "ENTER" was pressed
736
- e.preventDefault();
737
- // prevent event from being handled again by document
738
- e.stopPropagation();
739
- _this3._openDropdown();
740
- }
741
- // allow navigation from dropdown to input on TAB
742
- if (e.key === "Tab") {
743
- _this3._closeDropdown();
744
- }
745
- };
746
- this.flagsContainer.addEventListener("keydown", this._handleFlagsContainerKeydown);
747
- }
748
- }, {
749
- key: "_initRequests",
750
- value: function _initRequests() {
751
- var _this4 = this;
752
- // if the user has specified the path to the utils script, fetch it on window.load, else resolve
753
- if (this.options.utilsScript && !window.intlTelInputUtils) {
754
- // if the plugin is being initialised after the window.load event has already been fired
755
- if (window.intlTelInputGlobals.documentReady()) {
756
- window.intlTelInputGlobals.loadUtils(this.options.utilsScript);
757
- } else {
758
- // wait until the load event so we don't block any other requests e.g. the flags image
759
- window.addEventListener("load", function() {
760
- window.intlTelInputGlobals.loadUtils(_this4.options.utilsScript);
761
- });
762
- }
573
+ if (this.hiddenInputCountry) {
574
+ this.hiddenInputCountry.value = this.getSelectedCountryData().iso2;
575
+ }
576
+ };
577
+ this.telInput.form.addEventListener("submit", this._handleHiddenInputSubmit);
578
+ }
579
+ // initialise the dropdown listeners
580
+ _initDropdownListeners() {
581
+ // hack for input nested inside label (which is valid markup): clicking the selected-flag to
582
+ // open the dropdown would then automatically trigger a 2nd click on the input which would
583
+ // close it again
584
+ this._handleLabelClick = e => {
585
+ // if the dropdown is closed, then focus the input, else ignore the click
586
+ if (this.dropdownContent.classList.contains("iti__hide")) {
587
+ this.telInput.focus();
763
588
  } else {
764
- this.resolveUtilsScriptPromise();
589
+ e.preventDefault();
590
+ }
591
+ };
592
+ const label = this.telInput.closest("label");
593
+ if (label) {
594
+ label.addEventListener("click", this._handleLabelClick);
595
+ }
596
+ // toggle country dropdown on click
597
+ this._handleClickSelectedFlag = () => {
598
+ // only intercept this event if we're opening the dropdown
599
+ // else let it bubble up to the top ("click-off-to-close" listener)
600
+ // we cannot just stopPropagation as it may be needed to close another instance
601
+ if (this.dropdownContent.classList.contains("iti__hide") && !this.telInput.disabled && !this.telInput.readOnly) {
602
+ this._openDropdown();
603
+ }
604
+ };
605
+ this.selectedFlag.addEventListener("click", this._handleClickSelectedFlag);
606
+ // open dropdown if selected flag is focused and they press up/down/space/enter
607
+ this._handleFlagsContainerKeydown = e => {
608
+ const isDropdownHidden = this.dropdownContent.classList.contains("iti__hide");
609
+ if (isDropdownHidden && [ "ArrowUp", "ArrowDown", " ", "Enter" ].includes(e.key)) {
610
+ // prevent form from being submitted if "ENTER" was pressed
611
+ e.preventDefault();
612
+ // prevent event from being handled again by document
613
+ e.stopPropagation();
614
+ this._openDropdown();
615
+ }
616
+ // allow navigation from dropdown to input on TAB
617
+ if (e.key === "Tab") {
618
+ this._closeDropdown();
765
619
  }
766
- // dont bother with IP lookup if we already have a selected country
767
- if (this.options.initialCountry === "auto" && !this.selectedCountryData.iso2) {
768
- this._loadAutoCountry();
620
+ };
621
+ this.flagsContainer.addEventListener("keydown", this._handleFlagsContainerKeydown);
622
+ }
623
+ // init many requests: utils script / geo ip lookup
624
+ _initRequests() {
625
+ // if the user has specified the path to the utils script, fetch it on window.load, else resolve
626
+ if (this.options.utilsScript && !window.intlTelInputUtils) {
627
+ // if the plugin is being initialised after the window.load event has already been fired
628
+ if (window.intlTelInputGlobals.documentReady()) {
629
+ window.intlTelInputGlobals.loadUtils(this.options.utilsScript);
769
630
  } else {
770
- this.resolveAutoCountryPromise();
631
+ // wait until the load event so we don't block any other requests e.g. the flags image
632
+ window.addEventListener("load", () => {
633
+ window.intlTelInputGlobals.loadUtils(this.options.utilsScript);
634
+ });
771
635
  }
636
+ } else {
637
+ this.resolveUtilsScriptPromise();
772
638
  }
773
- }, {
774
- key: "_loadAutoCountry",
775
- value: function _loadAutoCountry() {
776
- var _this5 = this;
777
- // 3 options:
778
- // 1) already loaded (we're done)
779
- // 2) not already started loading (start)
780
- // 3) already started loading (do nothing - just wait for loading callback to fire)
781
- if (window.intlTelInputGlobals.autoCountry) {
782
- this.handleAutoCountry();
783
- } else if (!window.intlTelInputGlobals.startedLoadingAutoCountry) {
784
- // don't do this twice!
785
- window.intlTelInputGlobals.startedLoadingAutoCountry = true;
786
- if (typeof this.options.geoIpLookup === "function") {
787
- this.options.geoIpLookup(function() {
788
- var iso2 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : "";
789
- var iso2Lower = iso2.toLowerCase();
790
- var isValidIso2 = iso2Lower && _this5._getCountryData(iso2Lower, true);
791
- if (isValidIso2) {
792
- window.intlTelInputGlobals.autoCountry = iso2Lower;
793
- // tell all instances the auto country is ready
794
- // TODO: this should just be the current instances
795
- // UPDATE: use setTimeout in case their geoIpLookup function calls this callback straight
796
- // away (e.g. if they have already done the geo ip lookup somewhere else). Using
797
- // setTimeout means that the current thread of execution will finish before executing
798
- // this, which allows the plugin to finish initialising.
799
- setTimeout(function() {
800
- return forEachInstance("handleAutoCountry");
801
- });
802
- } else {
803
- _this5._setInitialState(true);
804
- forEachInstance("rejectAutoCountryPromise");
805
- }
806
- }, function() {
807
- return forEachInstance("rejectAutoCountryPromise");
808
- });
809
- }
639
+ // dont bother with IP lookup if we already have a selected country
640
+ if (this.options.initialCountry === "auto" && !this.selectedCountryData.iso2) {
641
+ this._loadAutoCountry();
642
+ } else {
643
+ this.resolveAutoCountryPromise();
644
+ }
645
+ }
646
+ // perform the geo ip lookup
647
+ _loadAutoCountry() {
648
+ // 3 options:
649
+ // 1) already loaded (we're done)
650
+ // 2) not already started loading (start)
651
+ // 3) already started loading (do nothing - just wait for loading callback to fire)
652
+ if (window.intlTelInputGlobals.autoCountry) {
653
+ this.handleAutoCountry();
654
+ } else if (!window.intlTelInputGlobals.startedLoadingAutoCountry) {
655
+ // don't do this twice!
656
+ window.intlTelInputGlobals.startedLoadingAutoCountry = true;
657
+ if (typeof this.options.geoIpLookup === "function") {
658
+ this.options.geoIpLookup((iso2 = "") => {
659
+ const iso2Lower = iso2.toLowerCase();
660
+ const isValidIso2 = iso2Lower && this._getCountryData(iso2Lower, true);
661
+ if (isValidIso2) {
662
+ window.intlTelInputGlobals.autoCountry = iso2Lower;
663
+ // tell all instances the auto country is ready
664
+ // TODO: this should just be the current instances
665
+ // UPDATE: use setTimeout in case their geoIpLookup function calls this callback straight
666
+ // away (e.g. if they have already done the geo ip lookup somewhere else). Using
667
+ // setTimeout means that the current thread of execution will finish before executing
668
+ // this, which allows the plugin to finish initialising.
669
+ setTimeout(() => forEachInstance("handleAutoCountry"));
670
+ } else {
671
+ this._setInitialState(true);
672
+ forEachInstance("rejectAutoCountryPromise");
673
+ }
674
+ }, () => forEachInstance("rejectAutoCountryPromise"));
810
675
  }
811
676
  }
812
- }, {
813
- key: "_initKeyListeners",
814
- value: function _initKeyListeners() {
815
- var _this6 = this;
816
- var userOverrideFormatting = false;
817
- // update flag on input event
818
- this._handleKeyEvent = function(e) {
819
- if (_this6._updateFlagFromNumber(_this6.telInput.value)) {
820
- _this6._triggerCountryChange();
821
- }
822
- // if user types their own formatting char (not a plus or a numeric), then set the override
823
- var isFormattingChar = e && e.data && /[^+0-9]/.test(e.data);
824
- var isPaste = e && e.inputType === "insertFromPaste" && _this6.telInput.value;
825
- if (isFormattingChar || isPaste) {
826
- userOverrideFormatting = true;
827
- } else if (!/[^+0-9]/.test(_this6.telInput.value)) {
828
- userOverrideFormatting = false;
829
- }
830
- // handle FAYT, unless userOverrideFormatting or it's a paste event
831
- if (_this6.options.formatAsYouType && !userOverrideFormatting && !isPaste) {
832
- // maintain caret position after reformatting
833
- var currentCaretPos = _this6.telInput.selectionStart;
834
- var valueBeforeCaret = _this6.telInput.value.substring(0, currentCaretPos);
835
- var relevantCharsBeforeCaret = valueBeforeCaret.replace(/[^+0-9]/g, "").length;
836
- var isDeleteForwards = e && e.inputType === "deleteContentForward";
837
- var formattedValue = _this6._formatNumberAsYouType();
838
- var newCaretPos = _this6._translateCursorPosition(relevantCharsBeforeCaret, formattedValue, currentCaretPos, isDeleteForwards);
839
- _this6.telInput.value = formattedValue;
840
- _this6.telInput.setSelectionRange(newCaretPos, newCaretPos);
677
+ }
678
+ // initialize the tel input listeners
679
+ _initTelInputListeners() {
680
+ const {
681
+ strictMode,
682
+ formatAsYouType
683
+ } = this.options;
684
+ let userOverrideFormatting = false;
685
+ // update flag on input event
686
+ this._handleInputEvent = e => {
687
+ if (this._updateFlagFromNumber(this.telInput.value)) {
688
+ this._triggerCountryChange();
689
+ }
690
+ // if user types their own formatting char (not a plus or a numeric), or they paste something, then set the override
691
+ const isFormattingChar = e && e.data && /[^+0-9]/.test(e.data);
692
+ const isPaste = e && e.inputType === "insertFromPaste" && this.telInput.value;
693
+ if (isFormattingChar || isPaste && !strictMode) {
694
+ userOverrideFormatting = true;
695
+ }
696
+ // if user removes all formatting chars, then reset the override
697
+ else if (!/[^+0-9]/.test(this.telInput.value)) {
698
+ userOverrideFormatting = false;
699
+ }
700
+ // handle FAYT, unless userOverrideFormatting
701
+ if (formatAsYouType && !userOverrideFormatting) {
702
+ // maintain caret position after reformatting
703
+ const currentCaretPos = this.telInput.selectionStart;
704
+ const valueBeforeCaret = this.telInput.value.substring(0, currentCaretPos);
705
+ const relevantCharsBeforeCaret = valueBeforeCaret.replace(/[^+0-9]/g, "").length;
706
+ const isDeleteForwards = e && e.inputType === "deleteContentForward";
707
+ const formattedValue = this._formatNumberAsYouType();
708
+ const newCaretPos = this._translateCursorPosition(relevantCharsBeforeCaret, formattedValue, currentCaretPos, isDeleteForwards);
709
+ this.telInput.value = formattedValue;
710
+ this.telInput.setSelectionRange(newCaretPos, newCaretPos);
711
+ }
712
+ };
713
+ // this handles individual key presses as well as cut/paste events
714
+ // the advantage of the "input" event over "keyup" etc is that "input" only fires when the value changes,
715
+ // whereas "keyup" fires even for shift key, arrow key presses etc
716
+ this.telInput.addEventListener("input", this._handleInputEvent);
717
+ if (strictMode) {
718
+ this._handleKeydownEvent = e => {
719
+ // only ignore actual character presses, rather than ctrl, alt, shift, command, arrow keys, delete/backspace, cut/copy/paste etc
720
+ if (e.key.length === 1 && !e.altKey && !e.ctrlKey && !e.metaKey) {
721
+ const isInitialPlus = this.telInput.selectionStart === 0 && e.key === "+";
722
+ const isNumeric = /^[0-9]$/.test(e.key);
723
+ const isAllowedChar = isInitialPlus || isNumeric;
724
+ const fullNumber = this._getFullNumber();
725
+ const coreNumber = intlTelInputUtils.getCoreNumber(fullNumber, this.selectedCountryData.iso2);
726
+ const hasReachedMaxLength = this.maxCoreNumberLength && coreNumber.length >= this.maxCoreNumberLength;
727
+ if (!isAllowedChar || hasReachedMaxLength) {
728
+ e.preventDefault();
729
+ }
841
730
  }
842
731
  };
843
- // this handles individual key presses as well as cut/paste events
844
- // the advantage of the "input" event over "keyup" etc is that "input" only fires when the value changes,
845
- // whereas "keyup" fires even for arrow key presses etc
846
- this.telInput.addEventListener("input", this._handleKeyEvent);
847
- }
848
- }, {
849
- key: "_translateCursorPosition",
850
- value: function _translateCursorPosition(relevantChars, formattedValue, prevCaretPos, isDeleteForwards) {
851
- // if the first char is a formatting char, and they backspace delete it:
852
- // cursor should stay at the start (pos 0), rather than stick to the first digit (pos 1)
853
- if (prevCaretPos === 0 && !isDeleteForwards) {
854
- return 0;
732
+ this.telInput.addEventListener("keydown", this._handleKeydownEvent);
733
+ }
734
+ }
735
+ // iterate through the formattedValue until hit the right number of relevant chars
736
+ _translateCursorPosition(relevantChars, formattedValue, prevCaretPos, isDeleteForwards) {
737
+ // if the first char is a formatting char, and they backspace delete it:
738
+ // cursor should stay at the start (pos 0), rather than stick to the first digit (pos 1)
739
+ if (prevCaretPos === 0 && !isDeleteForwards) {
740
+ return 0;
741
+ }
742
+ let count = 0;
743
+ for (let i = 0; i < formattedValue.length; i++) {
744
+ if (/[+0-9]/.test(formattedValue[i])) {
745
+ count++;
855
746
  }
856
- var count = 0;
857
- for (var i = 0; i < formattedValue.length; i++) {
858
- if (/[+0-9]/.test(formattedValue[i])) {
859
- count++;
860
- }
861
- // normal case: stop when you hit the right number of relevant chars
862
- // (cursor will be just after the final relevant char)
863
- if (count === relevantChars && !isDeleteForwards) {
864
- return i + 1;
865
- }
866
- // spacial case: delete forwards (fn + delete on a mac):
867
- // wait until hit one extra relevant char, and put the cursor just before it (after any formatting chars)
868
- if (isDeleteForwards && count === relevantChars + 1) {
869
- return i;
870
- }
747
+ // normal case: stop when you hit the right number of relevant chars
748
+ // (cursor will be just after the final relevant char)
749
+ if (count === relevantChars && !isDeleteForwards) {
750
+ return i + 1;
871
751
  }
872
- return formattedValue.length;
873
- }
874
- }, {
875
- key: "_cap",
876
- value: function _cap(number) {
877
- var max = this.telInput.getAttribute("maxlength");
878
- return max && number.length > max ? number.substr(0, max) : number;
879
- }
880
- }, {
881
- key: "_removeEmptyDialCode",
882
- value: function _removeEmptyDialCode() {
883
- if (this.telInput.value.charAt(0) === "+") {
884
- var numeric = this._getNumeric(this.telInput.value);
885
- // if just a plus, or if just a dial code
886
- if (!numeric || this.selectedCountryData.dialCode === numeric) {
887
- this.telInput.value = "";
888
- }
752
+ // spacial case: delete forwards (fn + delete on a mac):
753
+ // wait until hit one extra relevant char, and put the cursor just before it (after any formatting chars)
754
+ if (isDeleteForwards && count === relevantChars + 1) {
755
+ return i;
889
756
  }
890
757
  }
891
- }, {
892
- key: "_getNumeric",
893
- value: function _getNumeric(s) {
894
- return s.replace(/\D/g, "");
895
- }
896
- }, {
897
- key: "_trigger",
898
- value: function _trigger(name) {
899
- var e = new Event(name, {
900
- bubbles: true,
901
- cancelable: true
902
- });
903
- this.telInput.dispatchEvent(e);
904
- }
905
- }, {
906
- key: "_openDropdown",
907
- value: function _openDropdown() {
908
- var _this$options2 = this.options, fixDropdownWidth = _this$options2.fixDropdownWidth, countrySearch = _this$options2.countrySearch;
909
- if (fixDropdownWidth) {
910
- this.dropdownContent.style.width = "".concat(this.telInput.offsetWidth, "px");
911
- }
912
- this.dropdownContent.classList.remove("iti__hide");
913
- this.selectedFlag.setAttribute("aria-expanded", "true");
914
- this._setDropdownPosition();
915
- // if we have previously selected a country (and countrySearch is disabled), then highlight that item and scroll to it
916
- // else highlight the first item and scroll to top (even if countrySearch is disabled e.g. on init, showing globe icon)
917
- if (this.activeItem && !countrySearch) {
918
- // update highlighting and scroll to active list item
919
- this._highlightListItem(this.activeItem, false);
920
- this._scrollTo(this.activeItem, true);
921
- } else {
922
- // start by highlighting the first item in the list
923
- var firstElementChild = this.countryList.firstElementChild;
924
- if (firstElementChild) {
925
- this._highlightListItem(firstElementChild, false);
926
- this.countryList.scrollTop = 0;
927
- }
928
- if (countrySearch) {
929
- this.searchInput.focus();
930
- }
931
- }
932
- // bind all the dropdown-related listeners: mouseover, click, click-off, keydown
933
- this._bindDropdownListeners();
934
- // update the arrow
935
- this.dropdownArrow.classList.add("iti__arrow--up");
936
- this._trigger("open:countrydropdown");
937
- }
938
- }, {
939
- key: "_toggleClass",
940
- value: function _toggleClass(el, className, shouldHaveClass) {
941
- if (shouldHaveClass && !el.classList.contains(className)) {
942
- el.classList.add(className);
943
- } else if (!shouldHaveClass && el.classList.contains(className)) {
944
- el.classList.remove(className);
758
+ return formattedValue.length;
759
+ }
760
+ // adhere to the input's maxlength attr
761
+ _cap(number) {
762
+ const max = this.telInput.getAttribute("maxlength");
763
+ return max && number.length > max ? number.substr(0, max) : number;
764
+ }
765
+ // clear the input if it just contains a dial code
766
+ _removeEmptyDialCode() {
767
+ if (this.telInput.value.charAt(0) === "+") {
768
+ const numeric = this._getNumeric(this.telInput.value);
769
+ // if just a plus, or if just a dial code
770
+ if (!numeric || this.selectedCountryData.dialCode === numeric) {
771
+ this.telInput.value = "";
945
772
  }
946
773
  }
947
- }, {
948
- key: "_setDropdownPosition",
949
- value: function _setDropdownPosition() {
950
- var _this7 = this;
774
+ }
775
+ // extract the numeric digits from the given string
776
+ _getNumeric(s) {
777
+ return s.replace(/\D/g, "");
778
+ }
779
+ // trigger a custom event on the input
780
+ _trigger(name) {
781
+ const e = new Event(name, {
782
+ bubbles: true,
783
+ cancelable: true
784
+ });
785
+ this.telInput.dispatchEvent(e);
786
+ }
787
+ // open the dropdown
788
+ _openDropdown() {
789
+ const {
790
+ fixDropdownWidth,
791
+ countrySearch
792
+ } = this.options;
793
+ if (fixDropdownWidth) {
794
+ this.dropdownContent.style.width = `${this.telInput.offsetWidth}px`;
795
+ }
796
+ this.dropdownContent.classList.remove("iti__hide");
797
+ this.selectedFlag.setAttribute("aria-expanded", "true");
798
+ this._setDropdownPosition();
799
+ // if we have previously selected a country (and countrySearch is disabled), then highlight that item and scroll to it
800
+ // else highlight the first item and scroll to top (even if countrySearch is disabled e.g. on init, showing globe icon)
801
+ if (this.activeItem && !countrySearch) {
802
+ // update highlighting and scroll to active list item
803
+ this._highlightListItem(this.activeItem, false);
804
+ this._scrollTo(this.activeItem, true);
805
+ } else {
806
+ // start by highlighting the first item in the list
807
+ const {
808
+ firstElementChild
809
+ } = this.countryList;
810
+ if (firstElementChild) {
811
+ this._highlightListItem(firstElementChild, false);
812
+ this.countryList.scrollTop = 0;
813
+ }
814
+ if (countrySearch) {
815
+ this.searchInput.focus();
816
+ }
817
+ }
818
+ // bind all the dropdown-related listeners: mouseover, click, click-off, keydown
819
+ this._bindDropdownListeners();
820
+ // update the arrow
821
+ this.dropdownArrow.classList.add("iti__arrow--up");
822
+ this._trigger("open:countrydropdown");
823
+ }
824
+ // make sure the el has the className or not, depending on the value of shouldHaveClass
825
+ _toggleClass(el, className, shouldHaveClass) {
826
+ if (shouldHaveClass && !el.classList.contains(className)) {
827
+ el.classList.add(className);
828
+ } else if (!shouldHaveClass && el.classList.contains(className)) {
829
+ el.classList.remove(className);
830
+ }
831
+ }
832
+ // decide if should position dropdown above or below input (depends on position within viewport, and scroll)
833
+ _setDropdownPosition() {
834
+ if (this.options.dropdownContainer) {
835
+ this.options.dropdownContainer.appendChild(this.dropdown);
836
+ }
837
+ if (!this.options.useFullscreenPopup) {
838
+ const pos = this.telInput.getBoundingClientRect();
839
+ // windowTop from https://stackoverflow.com/a/14384091/217866
840
+ const windowTop = document.documentElement.scrollTop;
841
+ const inputTop = pos.top + windowTop;
842
+ const dropdownHeight = this.dropdownContent.offsetHeight;
843
+ // dropdownFitsBelow = (dropdownBottom < windowBottom)
844
+ const dropdownFitsBelow = inputTop + this.telInput.offsetHeight + dropdownHeight < windowTop + window.innerHeight;
845
+ const dropdownFitsAbove = inputTop - dropdownHeight > windowTop;
846
+ // dont allow positioning above when country search enabled as the search box jumps around as you filter countries
847
+ const positionDropdownAboveInput = !this.options.countrySearch && !dropdownFitsBelow && dropdownFitsAbove;
848
+ // by default, the dropdown will be below the input. If we want to position it above the
849
+ // input, we add the dropup class.
850
+ this._toggleClass(this.dropdownContent, "iti__dropdown-content--dropup", positionDropdownAboveInput);
851
+ // if dropdownContainer is enabled, calculate postion
951
852
  if (this.options.dropdownContainer) {
952
- this.options.dropdownContainer.appendChild(this.dropdown);
953
- }
954
- if (!this.options.useFullscreenPopup) {
955
- var pos = this.telInput.getBoundingClientRect();
956
- // windowTop from https://stackoverflow.com/a/14384091/217866
957
- var windowTop = document.documentElement.scrollTop;
958
- var inputTop = pos.top + windowTop;
959
- var dropdownHeight = this.dropdownContent.offsetHeight;
960
- // dropdownFitsBelow = (dropdownBottom < windowBottom)
961
- var dropdownFitsBelow = inputTop + this.telInput.offsetHeight + dropdownHeight < windowTop + window.innerHeight;
962
- var dropdownFitsAbove = inputTop - dropdownHeight > windowTop;
963
- // dont allow positioning above when country search enabled as the search box jumps around as you filter countries
964
- var positionDropdownAboveInput = !this.options.countrySearch && !dropdownFitsBelow && dropdownFitsAbove;
965
- // by default, the dropdown will be below the input. If we want to position it above the
966
- // input, we add the dropup class.
967
- this._toggleClass(this.dropdownContent, "iti__dropdown-content--dropup", positionDropdownAboveInput);
968
- // if dropdownContainer is enabled, calculate postion
969
- if (this.options.dropdownContainer) {
970
- // if we want to position the dropdown below the input, we need to add the input height to the top value
971
- var extraTop = positionDropdownAboveInput ? 0 : this.telInput.offsetHeight;
972
- // calculate placement
973
- this.dropdown.style.top = "".concat(inputTop + extraTop, "px");
974
- this.dropdown.style.left = "".concat(pos.left + document.body.scrollLeft, "px");
975
- // close menu on window scroll
976
- this._handleWindowScroll = function() {
977
- return _this7._closeDropdown();
978
- };
979
- window.addEventListener("scroll", this._handleWindowScroll);
980
- }
853
+ // if we want to position the dropdown below the input, we need to add the input height to the top value
854
+ const extraTop = positionDropdownAboveInput ? 0 : this.telInput.offsetHeight;
855
+ // calculate placement
856
+ this.dropdown.style.top = `${inputTop + extraTop}px`;
857
+ this.dropdown.style.left = `${pos.left + document.body.scrollLeft}px`;
858
+ // close menu on window scroll
859
+ this._handleWindowScroll = () => this._closeDropdown();
860
+ window.addEventListener("scroll", this._handleWindowScroll);
981
861
  }
982
862
  }
983
- }, {
984
- key: "_bindDropdownListeners",
985
- value: function _bindDropdownListeners() {
986
- var _this8 = this;
987
- // when mouse over a list item, just highlight that one
988
- // we add the class "highlight", so if they hit "enter" we know which one to select
989
- this._handleMouseoverCountryList = function(e) {
990
- // handle event delegation, as we're listening for this event on the countryList
991
- var listItem = e.target.closest(".iti__country");
992
- if (listItem) {
993
- _this8._highlightListItem(listItem, false);
863
+ }
864
+ // we only bind dropdown listeners when the dropdown is open
865
+ _bindDropdownListeners() {
866
+ // when mouse over a list item, just highlight that one
867
+ // we add the class "highlight", so if they hit "enter" we know which one to select
868
+ this._handleMouseoverCountryList = e => {
869
+ // handle event delegation, as we're listening for this event on the countryList
870
+ const listItem = e.target.closest(".iti__country");
871
+ if (listItem) {
872
+ this._highlightListItem(listItem, false);
873
+ }
874
+ };
875
+ this.countryList.addEventListener("mouseover", this._handleMouseoverCountryList);
876
+ // listen for country selection
877
+ this._handleClickCountryList = e => {
878
+ const listItem = e.target.closest(".iti__country");
879
+ if (listItem) {
880
+ this._selectListItem(listItem);
881
+ }
882
+ };
883
+ this.countryList.addEventListener("click", this._handleClickCountryList);
884
+ // click off to close
885
+ // (except when this initial opening click is bubbling up)
886
+ // we cannot just stopPropagation as it may be needed to close another instance
887
+ let isOpening = true;
888
+ this._handleClickOffToClose = () => {
889
+ if (!isOpening) {
890
+ this._closeDropdown();
891
+ }
892
+ isOpening = false;
893
+ };
894
+ document.documentElement.addEventListener("click", this._handleClickOffToClose);
895
+ // listen for up/down scrolling, enter to select, or escape to close
896
+ // use keydown as keypress doesn't fire for non-char keys and we want to catch if they
897
+ // just hit down and hold it to scroll down (no keyup event).
898
+ // listen on the document because that's where key events are triggered if no input has focus
899
+ let query = "";
900
+ let queryTimer = null;
901
+ this._handleKeydownOnDropdown = e => {
902
+ // prevent down key from scrolling the whole page,
903
+ // and enter key from submitting a form etc
904
+ if ([ "ArrowUp", "ArrowDown", "Enter", "Escape" ].includes(e.key)) {
905
+ e.preventDefault();
906
+ e.stopPropagation();
907
+ // up and down to navigate
908
+ if (e.key === "ArrowUp" || e.key === "ArrowDown") {
909
+ this._handleUpDownKey(e.key);
994
910
  }
995
- };
996
- this.countryList.addEventListener("mouseover", this._handleMouseoverCountryList);
997
- // listen for country selection
998
- this._handleClickCountryList = function(e) {
999
- var listItem = e.target.closest(".iti__country");
1000
- if (listItem) {
1001
- _this8._selectListItem(listItem);
911
+ // enter to select
912
+ else if (e.key === "Enter") {
913
+ this._handleEnterKey();
1002
914
  }
1003
- };
1004
- this.countryList.addEventListener("click", this._handleClickCountryList);
1005
- // click off to close
1006
- // (except when this initial opening click is bubbling up)
1007
- // we cannot just stopPropagation as it may be needed to close another instance
1008
- var isOpening = true;
1009
- this._handleClickOffToClose = function() {
1010
- if (!isOpening) {
1011
- _this8._closeDropdown();
915
+ // esc to close
916
+ else if (e.key === "Escape") {
917
+ this._closeDropdown();
1012
918
  }
1013
- isOpening = false;
1014
- };
1015
- document.documentElement.addEventListener("click", this._handleClickOffToClose);
1016
- // listen for up/down scrolling, enter to select, or escape to close
1017
- // use keydown as keypress doesn't fire for non-char keys and we want to catch if they
1018
- // just hit down and hold it to scroll down (no keyup event).
1019
- // listen on the document because that's where key events are triggered if no input has focus
1020
- var query = "";
1021
- var queryTimer = null;
1022
- this._handleKeydownOnDropdown = function(e) {
1023
- // prevent down key from scrolling the whole page,
1024
- // and enter key from submitting a form etc
1025
- if ([ "ArrowUp", "ArrowDown", "Enter", "Escape" ].includes(e.key)) {
1026
- e.preventDefault();
1027
- e.stopPropagation();
1028
- // up and down to navigate
1029
- if (e.key === "ArrowUp" || e.key === "ArrowDown") {
1030
- _this8._handleUpDownKey(e.key);
1031
- } else if (e.key === "Enter") {
1032
- _this8._handleEnterKey();
1033
- } else if (e.key === "Escape") {
1034
- _this8._closeDropdown();
1035
- }
919
+ }
920
+ // alpha chars to perform search
921
+ // regex allows one latin alpha char or space, based on https://stackoverflow.com/a/26900132/217866)
922
+ if (!this.options.countrySearch && /^[a-zA-ZÀ-ÿа-яА-Я ]$/.test(e.key)) {
923
+ e.stopPropagation();
924
+ // jump to countries that start with the query string
925
+ if (queryTimer) {
926
+ clearTimeout(queryTimer);
1036
927
  }
1037
- // alpha chars to perform search
1038
- // regex allows one latin alpha char or space, based on https://stackoverflow.com/a/26900132/217866)
1039
- if (!_this8.options.countrySearch && /^[a-zA-ZÀ-ÿа-яА-Я ]$/.test(e.key)) {
1040
- e.stopPropagation();
1041
- // jump to countries that start with the query string
1042
- if (queryTimer) {
1043
- clearTimeout(queryTimer);
1044
- }
1045
- query += e.key.toLowerCase();
1046
- _this8._searchForCountry(query);
1047
- // if the timer hits 1 second, reset the query
1048
- queryTimer = setTimeout(function() {
1049
- query = "";
1050
- }, 1e3);
928
+ query += e.key.toLowerCase();
929
+ this._searchForCountry(query);
930
+ // if the timer hits 1 second, reset the query
931
+ queryTimer = setTimeout(() => {
932
+ query = "";
933
+ }, 1e3);
934
+ }
935
+ };
936
+ document.addEventListener("keydown", this._handleKeydownOnDropdown);
937
+ if (this.options.countrySearch) {
938
+ const doFilter = () => {
939
+ const inputQuery = this.searchInput.value.trim();
940
+ if (inputQuery) {
941
+ this._filterCountries(inputQuery);
942
+ } else {
943
+ this._filterCountries("", true);
1051
944
  }
1052
945
  };
1053
- document.addEventListener("keydown", this._handleKeydownOnDropdown);
1054
- if (this.options.countrySearch) {
1055
- var doFilter = function doFilter() {
1056
- var inputQuery = _this8.searchInput.value.trim();
1057
- if (inputQuery) {
1058
- _this8._filterCountries(inputQuery);
1059
- } else {
1060
- _this8._filterCountries("", true);
1061
- }
1062
- };
1063
- var keyupTimer = null;
1064
- this._handleSearchChange = function() {
1065
- // filtering country nodes is expensive (lots of DOM manipulation), so rate limit it
1066
- if (keyupTimer) {
1067
- clearTimeout(keyupTimer);
1068
- }
1069
- keyupTimer = setTimeout(function() {
1070
- doFilter();
1071
- keyupTimer = null;
1072
- }, 100);
1073
- };
1074
- this.searchInput.addEventListener("input", this._handleSearchChange);
1075
- // stop propagation on search input click, so doesn't trigger click-off-to-close listener
1076
- this.searchInput.addEventListener("click", function(e) {
1077
- return e.stopPropagation();
1078
- });
1079
- }
1080
- }
1081
- }, {
1082
- key: "_normaliseString",
1083
- value: function _normaliseString() {
1084
- var s = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : "";
1085
- return s.normalize("NFD").replace(/[\u0300-\u036f]/g, "").toLowerCase();
1086
- }
1087
- }, {
1088
- key: "_filterCountries",
1089
- value: function _filterCountries(query) {
1090
- var isReset = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
1091
- var isFirst = true;
1092
- this.countryList.innerHTML = "";
1093
- var normalisedQuery = this._normaliseString(query);
1094
- for (var i = 0; i < this.countries.length; i++) {
1095
- var c = this.countries[i];
1096
- var normalisedCountryName = this._normaliseString(c.name);
1097
- var fullDialCode = "+".concat(c.dialCode);
1098
- if (isReset || normalisedCountryName.includes(normalisedQuery) || fullDialCode.includes(normalisedQuery) || c.iso2.includes(normalisedQuery)) {
1099
- this.countryList.appendChild(c.nodeById[this.id]);
1100
- // highlight the first item
1101
- if (isFirst) {
1102
- this._highlightListItem(c.nodeById[this.id], false);
1103
- isFirst = false;
1104
- }
946
+ let keyupTimer = null;
947
+ this._handleSearchChange = () => {
948
+ // filtering country nodes is expensive (lots of DOM manipulation), so rate limit it
949
+ if (keyupTimer) {
950
+ clearTimeout(keyupTimer);
1105
951
  }
1106
- }
1107
- // scroll to top (useful if user had previously scrolled down)
1108
- this.countryList.scrollTop = 0;
1109
- this._updateSearchResultsText();
1110
- }
1111
- }, {
1112
- key: "_updateSearchResultsText",
1113
- value: function _updateSearchResultsText() {
1114
- var i18n = this.options.i18n;
1115
- var count = this.countryList.childElementCount;
1116
- var searchText;
1117
- if (count === 0) {
1118
- searchText = i18n.zeroSearchResults || "No results found";
1119
- } else if (count === 1) {
1120
- searchText = i18n.oneSearchResult || "1 result found";
1121
- } else {
1122
- // eslint-disable-next-line no-template-curly-in-string
1123
- searchText = i18n.multipleSearchResults ? i18n.multipleSearchResults.replace("${count}", count) : "".concat(count, " results found");
1124
- }
1125
- this.searchResultsA11yText.textContent = searchText;
1126
- }
1127
- }, {
1128
- key: "_handleUpDownKey",
1129
- value: function _handleUpDownKey(key) {
1130
- var next = key === "ArrowUp" ? this.highlightedItem.previousElementSibling : this.highlightedItem.nextElementSibling;
1131
- if (next) {
1132
- // skip the divider
1133
- if (next.classList.contains("iti__divider")) {
1134
- next = key === "ArrowUp" ? next.previousElementSibling : next.nextElementSibling;
952
+ keyupTimer = setTimeout(() => {
953
+ doFilter();
954
+ keyupTimer = null;
955
+ }, 100);
956
+ };
957
+ this.searchInput.addEventListener("input", this._handleSearchChange);
958
+ // stop propagation on search input click, so doesn't trigger click-off-to-close listener
959
+ this.searchInput.addEventListener("click", e => e.stopPropagation());
960
+ }
961
+ }
962
+ // turns "Réunion" into "Reunion"
963
+ // from https://stackoverflow.com/a/37511463
964
+ _normaliseString(s = "") {
965
+ return s.normalize("NFD").replace(/[\u0300-\u036f]/g, "").toLowerCase();
966
+ }
967
+ _filterCountries(query, isReset = false) {
968
+ let isFirst = true;
969
+ this.countryList.innerHTML = "";
970
+ const normalisedQuery = this._normaliseString(query);
971
+ for (let i = 0; i < this.countries.length; i++) {
972
+ const c = this.countries[i];
973
+ const normalisedCountryName = this._normaliseString(c.name);
974
+ const fullDialCode = `+${c.dialCode}`;
975
+ if (isReset || normalisedCountryName.includes(normalisedQuery) || fullDialCode.includes(normalisedQuery) || c.iso2.includes(normalisedQuery)) {
976
+ this.countryList.appendChild(c.nodeById[this.id]);
977
+ // highlight the first item
978
+ if (isFirst) {
979
+ this._highlightListItem(c.nodeById[this.id], false);
980
+ isFirst = false;
1135
981
  }
1136
- } else if (this.countryList.childElementCount > 1) {
1137
- // otherwise, we must be at the end, so loop round again
1138
- next = key === "ArrowUp" ? this.countryList.lastElementChild : this.countryList.firstElementChild;
1139
- }
1140
- if (next) {
1141
- // make sure the next item is visible
1142
- // (before calling focus(), which can cause the next item to scroll to the middle of the dropdown, which is jarring)
1143
- this._scrollTo(next, false);
1144
- // if country search enabled, dont lose focus from the search input on up/down
1145
- var doFocus = !this.options.countrySearch;
1146
- this._highlightListItem(next, doFocus);
1147
982
  }
1148
983
  }
1149
- }, {
1150
- key: "_handleEnterKey",
1151
- value: function _handleEnterKey() {
1152
- if (this.highlightedItem) {
1153
- this._selectListItem(this.highlightedItem);
1154
- }
984
+ // scroll to top (useful if user had previously scrolled down)
985
+ this.countryList.scrollTop = 0;
986
+ this._updateSearchResultsText();
987
+ }
988
+ // update search results text (for a11y)
989
+ _updateSearchResultsText() {
990
+ const {
991
+ i18n
992
+ } = this.options;
993
+ const count = this.countryList.childElementCount;
994
+ let searchText;
995
+ if (count === 0) {
996
+ searchText = i18n.zeroSearchResults || "No results found";
997
+ } else if (count === 1) {
998
+ searchText = i18n.oneSearchResult || "1 result found";
999
+ } else {
1000
+ // eslint-disable-next-line no-template-curly-in-string
1001
+ searchText = i18n.multipleSearchResults ? i18n.multipleSearchResults.replace("${count}", count) : `${count} results found`;
1002
+ }
1003
+ this.searchResultsA11yText.textContent = searchText;
1004
+ }
1005
+ // highlight the next/prev item in the list (and ensure it is visible)
1006
+ _handleUpDownKey(key) {
1007
+ let next = key === "ArrowUp" ? this.highlightedItem.previousElementSibling : this.highlightedItem.nextElementSibling;
1008
+ if (next) {
1009
+ // skip the divider
1010
+ if (next.classList.contains("iti__divider")) {
1011
+ next = key === "ArrowUp" ? next.previousElementSibling : next.nextElementSibling;
1012
+ }
1013
+ } else if (this.countryList.childElementCount > 1) {
1014
+ // otherwise, we must be at the end, so loop round again
1015
+ next = key === "ArrowUp" ? this.countryList.lastElementChild : this.countryList.firstElementChild;
1016
+ }
1017
+ if (next) {
1018
+ // make sure the next item is visible
1019
+ // (before calling focus(), which can cause the next item to scroll to the middle of the dropdown, which is jarring)
1020
+ this._scrollTo(next, false);
1021
+ // if country search enabled, dont lose focus from the search input on up/down
1022
+ const doFocus = !this.options.countrySearch;
1023
+ this._highlightListItem(next, doFocus);
1155
1024
  }
1156
- }, {
1157
- key: "_searchForCountry",
1158
- value: function _searchForCountry(query) {
1159
- for (var i = 0; i < this.countries.length; i++) {
1160
- if (this._startsWith(this.countries[i].name, query)) {
1161
- var listItem = this.countries[i].nodeById[this.id];
1162
- // update highlighting and scroll
1163
- this._highlightListItem(listItem, false);
1164
- this._scrollTo(listItem, true);
1165
- break;
1166
- }
1167
- }
1025
+ }
1026
+ // select the currently highlighted item
1027
+ _handleEnterKey() {
1028
+ if (this.highlightedItem) {
1029
+ this._selectListItem(this.highlightedItem);
1168
1030
  }
1169
- }, {
1170
- key: "_startsWith",
1171
- value: function _startsWith(a, b) {
1172
- return a.substr(0, b.length).toLowerCase() === b;
1173
- }
1174
- }, {
1175
- key: "_updateValFromNumber",
1176
- value: function _updateValFromNumber(fullNumber) {
1177
- var number = fullNumber;
1178
- if (this.options.formatOnDisplay && window.intlTelInputUtils && this.selectedCountryData) {
1179
- var useNational = this.options.nationalMode || number.charAt(0) !== "+" && !this.options.showSelectedDialCode;
1180
- var _intlTelInputUtils$nu = intlTelInputUtils.numberFormat, NATIONAL = _intlTelInputUtils$nu.NATIONAL, INTERNATIONAL = _intlTelInputUtils$nu.INTERNATIONAL;
1181
- var format = useNational ? NATIONAL : INTERNATIONAL;
1182
- number = intlTelInputUtils.formatNumber(number, this.selectedCountryData.iso2, format);
1183
- }
1184
- number = this._beforeSetNumber(number);
1185
- this.telInput.value = number;
1186
- }
1187
- }, {
1188
- key: "_updateFlagFromNumber",
1189
- value: function _updateFlagFromNumber(fullNumber) {
1190
- var plusIndex = fullNumber.indexOf("+");
1191
- // if it contains a plus, discard any chars before it e.g. accidental space char.
1192
- // this keeps the selected country auto-updating correctly, which we want as
1193
- // libphonenumber's validation/getNumber methods will ignore these chars anyway
1194
- var number = plusIndex ? fullNumber.substring(plusIndex) : fullNumber;
1195
- // if we already have US/Canada selected, make sure the number starts
1196
- // with a +1 so _getDialCode will be able to extract the area code
1197
- // update: if we dont yet have selectedCountryData, but we're here (trying to update the flag
1198
- // from the number), that means we're initialising the plugin with a number that already has a
1199
- // dial code, so fine to ignore this bit
1200
- var selectedDialCode = this.selectedCountryData.dialCode;
1201
- var isNanp = selectedDialCode === "1";
1202
- if (number && isNanp && number.charAt(0) !== "+") {
1203
- if (number.charAt(0) !== "1") {
1204
- number = "1".concat(number);
1205
- }
1206
- number = "+".concat(number);
1207
- }
1208
- // if showSelectedDialCode enabled, then consider the selected dial code to be part of the number
1209
- if (this.options.showSelectedDialCode && selectedDialCode && number.charAt(0) !== "+") {
1210
- number = "+".concat(selectedDialCode).concat(number);
1031
+ }
1032
+ // find the first list item whose name starts with the query string
1033
+ _searchForCountry(query) {
1034
+ for (let i = 0; i < this.countries.length; i++) {
1035
+ if (this._startsWith(this.countries[i].name, query)) {
1036
+ const listItem = this.countries[i].nodeById[this.id];
1037
+ // update highlighting and scroll
1038
+ this._highlightListItem(listItem, false);
1039
+ this._scrollTo(listItem, true);
1040
+ break;
1211
1041
  }
1212
- // try and extract valid dial code from input
1213
- var dialCode = this._getDialCode(number, true);
1214
- var numeric = this._getNumeric(number);
1215
- var iso2 = null;
1216
- if (dialCode) {
1217
- var iso2Codes = this.dialCodeToIso2Map[this._getNumeric(dialCode)];
1218
- // check if the right country is already selected. this should be false if the number is
1219
- // longer than the matched dial code because in this case we need to make sure that if
1220
- // there are multiple country matches, that the first one is selected (note: we could
1221
- // just check that here, but it requires the same loop that we already have later)
1222
- var alreadySelected = iso2Codes.indexOf(this.selectedCountryData.iso2) !== -1 && numeric.length <= dialCode.length - 1;
1223
- var isRegionlessNanpNumber = selectedDialCode === "1" && this._isRegionlessNanp(numeric);
1224
- // only update the flag if:
1225
- // A) NOT (we currently have a NANP flag selected, and the number is a regionlessNanp)
1226
- // AND
1227
- // B) the right country is not already selected
1228
- if (!isRegionlessNanpNumber && !alreadySelected) {
1229
- // if using onlyCountries option, iso2Codes[0] may be empty, so we must find the first
1230
- // non-empty index
1231
- for (var j = 0; j < iso2Codes.length; j++) {
1232
- if (iso2Codes[j]) {
1233
- iso2 = iso2Codes[j];
1234
- break;
1235
- }
1042
+ }
1043
+ }
1044
+ // check if string a starts with string b
1045
+ _startsWith(a, b) {
1046
+ return a.substr(0, b.length).toLowerCase() === b;
1047
+ }
1048
+ // update the input's value to the given val (format first if possible)
1049
+ // NOTE: this is called from _setInitialState, handleUtils and setNumber
1050
+ _updateValFromNumber(fullNumber) {
1051
+ let number = fullNumber;
1052
+ if (this.options.formatOnDisplay && window.intlTelInputUtils && this.selectedCountryData) {
1053
+ const useNational = this.options.nationalMode || number.charAt(0) !== "+" && !this.options.showSelectedDialCode;
1054
+ const {
1055
+ NATIONAL,
1056
+ INTERNATIONAL
1057
+ } = intlTelInputUtils.numberFormat;
1058
+ const format = useNational ? NATIONAL : INTERNATIONAL;
1059
+ number = intlTelInputUtils.formatNumber(number, this.selectedCountryData.iso2, format);
1060
+ }
1061
+ number = this._beforeSetNumber(number);
1062
+ this.telInput.value = number;
1063
+ }
1064
+ // check if need to select a new flag based on the given number
1065
+ // Note: called from _setInitialState, keyup handler, setNumber
1066
+ _updateFlagFromNumber(fullNumber) {
1067
+ const plusIndex = fullNumber.indexOf("+");
1068
+ // if it contains a plus, discard any chars before it e.g. accidental space char.
1069
+ // this keeps the selected country auto-updating correctly, which we want as
1070
+ // libphonenumber's validation/getNumber methods will ignore these chars anyway
1071
+ let number = plusIndex ? fullNumber.substring(plusIndex) : fullNumber;
1072
+ // if we already have US/Canada selected, make sure the number starts
1073
+ // with a +1 so _getDialCode will be able to extract the area code
1074
+ // update: if we dont yet have selectedCountryData, but we're here (trying to update the flag
1075
+ // from the number), that means we're initialising the plugin with a number that already has a
1076
+ // dial code, so fine to ignore this bit
1077
+ const selectedDialCode = this.selectedCountryData.dialCode;
1078
+ const isNanp = selectedDialCode === "1";
1079
+ if (number && isNanp && number.charAt(0) !== "+") {
1080
+ if (number.charAt(0) !== "1") {
1081
+ number = `1${number}`;
1082
+ }
1083
+ number = `+${number}`;
1084
+ }
1085
+ // if showSelectedDialCode enabled, then consider the selected dial code to be part of the number
1086
+ if (this.options.showSelectedDialCode && selectedDialCode && number.charAt(0) !== "+") {
1087
+ number = `+${selectedDialCode}${number}`;
1088
+ }
1089
+ // try and extract valid dial code from input
1090
+ const dialCode = this._getDialCode(number, true);
1091
+ const numeric = this._getNumeric(number);
1092
+ let iso2 = null;
1093
+ if (dialCode) {
1094
+ const iso2Codes = this.dialCodeToIso2Map[this._getNumeric(dialCode)];
1095
+ // check if the right country is already selected. this should be false if the number is
1096
+ // longer than the matched dial code because in this case we need to make sure that if
1097
+ // there are multiple country matches, that the first one is selected (note: we could
1098
+ // just check that here, but it requires the same loop that we already have later)
1099
+ const alreadySelected = iso2Codes.indexOf(this.selectedCountryData.iso2) !== -1 && numeric.length <= dialCode.length - 1;
1100
+ const isRegionlessNanpNumber = selectedDialCode === "1" && this._isRegionlessNanp(numeric);
1101
+ // only update the flag if:
1102
+ // A) NOT (we currently have a NANP flag selected, and the number is a regionlessNanp)
1103
+ // AND
1104
+ // B) the right country is not already selected
1105
+ if (!isRegionlessNanpNumber && !alreadySelected) {
1106
+ // if using onlyCountries option, iso2Codes[0] may be empty, so we must find the first
1107
+ // non-empty index
1108
+ for (let j = 0; j < iso2Codes.length; j++) {
1109
+ if (iso2Codes[j]) {
1110
+ iso2 = iso2Codes[j];
1111
+ break;
1236
1112
  }
1237
1113
  }
1238
- } else if (number.charAt(0) === "+" && numeric.length) {
1239
- // invalid dial code, so empty
1240
- // Note: use getNumeric here because the number has not been formatted yet, so could contain
1241
- // bad chars
1242
- iso2 = "";
1243
- } else if ((!number || number === "+") && !this.selectedCountryData.iso2) {
1244
- // if no selected flag, and user either clears the input, or just types a plus, then show default
1245
- iso2 = this.defaultCountry;
1246
1114
  }
1247
- if (iso2 !== null) {
1248
- return this._setCountry(iso2);
1115
+ } else if (number.charAt(0) === "+" && numeric.length) {
1116
+ // invalid dial code, so empty
1117
+ // Note: use getNumeric here because the number has not been formatted yet, so could contain
1118
+ // bad chars
1119
+ iso2 = "";
1120
+ } else if ((!number || number === "+") && !this.selectedCountryData.iso2) {
1121
+ // if no selected flag, and user either clears the input, or just types a plus, then show default
1122
+ iso2 = this.defaultCountry;
1123
+ }
1124
+ if (iso2 !== null) {
1125
+ return this._setCountry(iso2);
1126
+ }
1127
+ return false;
1128
+ }
1129
+ // check if the given number is a regionless NANP number (expects the number to contain an
1130
+ // international dial code)
1131
+ _isRegionlessNanp(number) {
1132
+ const numeric = this._getNumeric(number);
1133
+ if (numeric.charAt(0) === "1") {
1134
+ const areaCode = numeric.substr(1, 3);
1135
+ return regionlessNanpNumbers.indexOf(areaCode) !== -1;
1136
+ }
1137
+ return false;
1138
+ }
1139
+ // remove highlighting from other list items and highlight the given item
1140
+ _highlightListItem(listItem, shouldFocus) {
1141
+ const prevItem = this.highlightedItem;
1142
+ if (prevItem) {
1143
+ prevItem.classList.remove("iti__highlight");
1144
+ prevItem.setAttribute("aria-selected", "false");
1145
+ }
1146
+ this.highlightedItem = listItem;
1147
+ this.highlightedItem.classList.add("iti__highlight");
1148
+ this.highlightedItem.setAttribute("aria-selected", "true");
1149
+ this.selectedFlag.setAttribute("aria-activedescendant", listItem.getAttribute("id"));
1150
+ if (this.options.countrySearch) {
1151
+ this.searchInput.setAttribute("aria-activedescendant", listItem.getAttribute("id"));
1152
+ }
1153
+ if (shouldFocus) {
1154
+ this.highlightedItem.focus();
1155
+ }
1156
+ }
1157
+ // find the country data for the given iso2 code
1158
+ // the ignoreOnlyCountriesOption is only used during init() while parsing the onlyCountries array
1159
+ _getCountryData(iso2, allowFail) {
1160
+ for (let i = 0; i < this.countries.length; i++) {
1161
+ if (this.countries[i].iso2 === iso2) {
1162
+ return this.countries[i];
1249
1163
  }
1250
- return false;
1251
1164
  }
1252
- }, {
1253
- key: "_isRegionlessNanp",
1254
- value: function _isRegionlessNanp(number) {
1255
- var numeric = this._getNumeric(number);
1256
- if (numeric.charAt(0) === "1") {
1257
- var areaCode = numeric.substr(1, 3);
1258
- return regionlessNanpNumbers.indexOf(areaCode) !== -1;
1165
+ if (allowFail) {
1166
+ return null;
1167
+ }
1168
+ throw new Error(`No country data for '${iso2}'`);
1169
+ }
1170
+ // update the selected flag, dial code (if showSelectedDialCode), placeholder, title, and active list item
1171
+ // Note: called from _setInitialState, _updateFlagFromNumber, _selectListItem, setCountry
1172
+ _setCountry(iso2) {
1173
+ const {
1174
+ allowDropdown,
1175
+ showSelectedDialCode,
1176
+ showFlags,
1177
+ countrySearch,
1178
+ i18n
1179
+ } = this.options;
1180
+ const prevCountry = this.selectedCountryData.iso2 ? this.selectedCountryData : {};
1181
+ // do this first as it will throw an error and stop if iso2 is invalid
1182
+ this.selectedCountryData = iso2 ? this._getCountryData(iso2, false) : {};
1183
+ // update the defaultCountry - we only need the iso2 from now on, so just store that
1184
+ if (this.selectedCountryData.iso2) {
1185
+ this.defaultCountry = this.selectedCountryData.iso2;
1186
+ }
1187
+ // update the flag class and the a11y text
1188
+ if (this.selectedFlagInner) {
1189
+ let flagClass = "";
1190
+ let a11yText = "";
1191
+ if (iso2) {
1192
+ if (showFlags) {
1193
+ flagClass = `iti__flag iti__${iso2}`;
1194
+ a11yText = `${this.selectedCountryData.name} +${this.selectedCountryData.dialCode}`;
1195
+ }
1196
+ // if showFlags disabled and showSelectedDialCode is enabled, we dont show a flag or have any a11y text, as the displayed dial code is enough
1197
+ } else {
1198
+ flagClass = "iti__flag iti__globe";
1199
+ a11yText = i18n.noCountrySelected || "No country selected";
1200
+ }
1201
+ this.selectedFlagInner.className = flagClass;
1202
+ this.selectedFlagA11yText.textContent = a11yText;
1203
+ }
1204
+ this._setSelectedCountryFlagTitleAttribute(iso2, showSelectedDialCode);
1205
+ // update the selected dial code
1206
+ if (showSelectedDialCode) {
1207
+ const dialCode = this.selectedCountryData.dialCode ? `+${this.selectedCountryData.dialCode}` : "";
1208
+ this.selectedDialCode.innerHTML = dialCode;
1209
+ // offsetWidth is zero if input is in a hidden container during initialisation
1210
+ const selectedFlagWidth = this.selectedFlag.offsetWidth || this._getHiddenSelectedFlagWidth();
1211
+ // add 6px of padding after the grey selected-dial-code box, as this is what we use in the css
1212
+ if (this.isRTL) {
1213
+ this.telInput.style.paddingRight = `${selectedFlagWidth + 6}px`;
1214
+ } else {
1215
+ this.telInput.style.paddingLeft = `${selectedFlagWidth + 6}px`;
1259
1216
  }
1260
- return false;
1261
1217
  }
1262
- }, {
1263
- key: "_highlightListItem",
1264
- value: function _highlightListItem(listItem, shouldFocus) {
1265
- var prevItem = this.highlightedItem;
1218
+ // and the input's placeholder
1219
+ this._updatePlaceholder();
1220
+ // update the maximum valid number length
1221
+ this._updateMaxLength();
1222
+ // update the active list item (only if country search disabled, as country search doesn't store the active item)
1223
+ if (allowDropdown && !countrySearch) {
1224
+ const prevItem = this.activeItem;
1266
1225
  if (prevItem) {
1267
- prevItem.classList.remove("iti__highlight");
1226
+ prevItem.classList.remove("iti__active");
1268
1227
  prevItem.setAttribute("aria-selected", "false");
1269
1228
  }
1270
- this.highlightedItem = listItem;
1271
- this.highlightedItem.classList.add("iti__highlight");
1272
- this.highlightedItem.setAttribute("aria-selected", "true");
1273
- this.selectedFlag.setAttribute("aria-activedescendant", listItem.getAttribute("id"));
1274
- if (this.options.countrySearch) {
1275
- this.searchInput.setAttribute("aria-activedescendant", listItem.getAttribute("id"));
1276
- }
1277
- if (shouldFocus) {
1278
- this.highlightedItem.focus();
1229
+ if (iso2) {
1230
+ // check if there is a preferred item first, else fall back to standard
1231
+ const nextItem = this.countryList.querySelector(`#iti-${this.id}__item-${iso2}-preferred`) || this.countryList.querySelector(`#iti-${this.id}__item-${iso2}`);
1232
+ nextItem.setAttribute("aria-selected", "true");
1233
+ nextItem.classList.add("iti__active");
1234
+ this.activeItem = nextItem;
1279
1235
  }
1280
1236
  }
1281
- }, {
1282
- key: "_getCountryData",
1283
- value: function _getCountryData(iso2, allowFail) {
1284
- for (var i = 0; i < this.countries.length; i++) {
1285
- if (this.countries[i].iso2 === iso2) {
1286
- return this.countries[i];
1287
- }
1288
- }
1289
- if (allowFail) {
1290
- return null;
1291
- }
1292
- throw new Error("No country data for '".concat(iso2, "'"));
1293
- }
1294
- }, {
1295
- key: "_setCountry",
1296
- value: function _setCountry(iso2) {
1297
- var _this$options3 = this.options, allowDropdown = _this$options3.allowDropdown, showSelectedDialCode = _this$options3.showSelectedDialCode, showFlags = _this$options3.showFlags, countrySearch = _this$options3.countrySearch, i18n = _this$options3.i18n;
1298
- var prevCountry = this.selectedCountryData.iso2 ? this.selectedCountryData : {};
1299
- // do this first as it will throw an error and stop if iso2 is invalid
1300
- this.selectedCountryData = iso2 ? this._getCountryData(iso2, false) : {};
1301
- // update the defaultCountry - we only need the iso2 from now on, so just store that
1237
+ // return if the flag has changed or not
1238
+ return prevCountry.iso2 !== iso2;
1239
+ }
1240
+ // update the maximum valid number length for the currently selected country
1241
+ _updateMaxLength() {
1242
+ if (this.options.strictMode && window.intlTelInputUtils) {
1302
1243
  if (this.selectedCountryData.iso2) {
1303
- this.defaultCountry = this.selectedCountryData.iso2;
1304
- }
1305
- // update the flag class and the a11y text
1306
- if (this.selectedFlagInner) {
1307
- var flagClass = "";
1308
- var a11yText = "";
1309
- if (iso2) {
1310
- if (showFlags) {
1311
- flagClass = "iti__flag iti__".concat(iso2);
1312
- a11yText = "".concat(this.selectedCountryData.name, " +").concat(this.selectedCountryData.dialCode);
1313
- }
1314
- } else {
1315
- flagClass = "iti__flag iti__globe";
1316
- a11yText = i18n.noCountrySelected || "No country selected";
1317
- }
1318
- this.selectedFlagInner.className = flagClass;
1319
- this.selectedFlagA11yText.textContent = a11yText;
1320
- }
1321
- this._setSelectedCountryFlagTitleAttribute(iso2, showSelectedDialCode);
1322
- // update the selected dial code
1323
- if (showSelectedDialCode) {
1324
- var dialCode = this.selectedCountryData.dialCode ? "+".concat(this.selectedCountryData.dialCode) : "";
1325
- this.selectedDialCode.innerHTML = dialCode;
1326
- // offsetWidth is zero if input is in a hidden container during initialisation
1327
- var selectedFlagWidth = this.selectedFlag.offsetWidth || this._getHiddenSelectedFlagWidth();
1328
- // add 6px of padding after the grey selected-dial-code box, as this is what we use in the css
1329
- if (this.isRTL) {
1330
- this.telInput.style.paddingRight = "".concat(selectedFlagWidth + 6, "px");
1331
- } else {
1332
- this.telInput.style.paddingLeft = "".concat(selectedFlagWidth + 6, "px");
1333
- }
1334
- }
1335
- // and the input's placeholder
1336
- this._updatePlaceholder();
1337
- // update the active list item (only if country search disabled, as country search doesn't store the active item)
1338
- if (allowDropdown && !countrySearch) {
1339
- var prevItem = this.activeItem;
1340
- if (prevItem) {
1341
- prevItem.classList.remove("iti__active");
1342
- prevItem.setAttribute("aria-selected", "false");
1244
+ const numberType = intlTelInputUtils.numberType[this.options.placeholderNumberType];
1245
+ let exampleNumber = intlTelInputUtils.getExampleNumber(this.selectedCountryData.iso2, null, numberType, true);
1246
+ // see if adding more digits is still valid to get the true maximum valid length
1247
+ let validNumber = exampleNumber;
1248
+ while (intlTelInputUtils.isPossibleNumber(exampleNumber, this.selectedCountryData.iso2)) {
1249
+ validNumber = exampleNumber;
1250
+ exampleNumber += "0";
1343
1251
  }
1344
- if (iso2) {
1345
- // check if there is a preferred item first, else fall back to standard
1346
- var nextItem = this.countryList.querySelector("#iti-".concat(this.id, "__item-").concat(iso2, "-preferred")) || this.countryList.querySelector("#iti-".concat(this.id, "__item-").concat(iso2));
1347
- nextItem.setAttribute("aria-selected", "true");
1348
- nextItem.classList.add("iti__active");
1349
- this.activeItem = nextItem;
1350
- }
1351
- }
1352
- // return if the flag has changed or not
1353
- return prevCountry.iso2 !== iso2;
1354
- }
1355
- }, {
1356
- key: "_setSelectedCountryFlagTitleAttribute",
1357
- value: function _setSelectedCountryFlagTitleAttribute(iso2, showSelectedDialCode) {
1358
- if (!this.selectedFlag) {
1359
- return;
1360
- }
1361
- var title;
1362
- if (iso2 && !showSelectedDialCode) {
1363
- title = "".concat(this.selectedCountryData.name, ": +").concat(this.selectedCountryData.dialCode);
1364
- } else if (iso2) {
1365
- // For screen reader output, we don't want to include the dial code in the reader output twice
1366
- // so just use the selected country name here:
1367
- title = this.selectedCountryData.name;
1252
+ const coreNumber = intlTelInputUtils.getCoreNumber(validNumber, this.selectedCountryData.iso2);
1253
+ this.maxCoreNumberLength = coreNumber.length;
1368
1254
  } else {
1369
- title = "Unknown";
1370
- }
1371
- this.selectedFlag.setAttribute("title", title);
1372
- }
1373
- }, {
1374
- key: "_getHiddenSelectedFlagWidth",
1375
- value: function _getHiddenSelectedFlagWidth() {
1376
- // to get the right styling to apply, all we need is a shallow clone of the container,
1377
- // and then to inject a deep clone of the selectedFlag element
1378
- var containerClone = this.telInput.parentNode.cloneNode();
1379
- containerClone.style.visibility = "hidden";
1380
- document.body.appendChild(containerClone);
1381
- var flagsContainerClone = this.flagsContainer.cloneNode();
1382
- containerClone.appendChild(flagsContainerClone);
1383
- var selectedFlagClone = this.selectedFlag.cloneNode(true);
1384
- flagsContainerClone.appendChild(selectedFlagClone);
1385
- var width = selectedFlagClone.offsetWidth;
1386
- containerClone.parentNode.removeChild(containerClone);
1387
- return width;
1388
- }
1389
- }, {
1390
- key: "_updatePlaceholder",
1391
- value: function _updatePlaceholder() {
1392
- var _this$options4 = this.options, autoPlaceholder = _this$options4.autoPlaceholder, placeholderNumberType = _this$options4.placeholderNumberType, nationalMode = _this$options4.nationalMode, customPlaceholder = _this$options4.customPlaceholder;
1393
- var shouldSetPlaceholder = autoPlaceholder === "aggressive" || !this.hadInitialPlaceholder && autoPlaceholder === "polite";
1394
- if (window.intlTelInputUtils && shouldSetPlaceholder) {
1395
- var numberType = intlTelInputUtils.numberType[placeholderNumberType];
1396
- var placeholder = this.selectedCountryData.iso2 ? intlTelInputUtils.getExampleNumber(this.selectedCountryData.iso2, nationalMode, numberType) : "";
1397
- placeholder = this._beforeSetNumber(placeholder);
1398
- if (typeof customPlaceholder === "function") {
1399
- placeholder = customPlaceholder(placeholder, this.selectedCountryData);
1400
- }
1401
- this.telInput.setAttribute("placeholder", placeholder);
1255
+ this.maxCoreNumberLength = null;
1402
1256
  }
1403
1257
  }
1404
- }, {
1405
- key: "_selectListItem",
1406
- value: function _selectListItem(listItem) {
1407
- // update selected flag and active list item
1408
- var flagChanged = this._setCountry(listItem.getAttribute("data-country-code"));
1409
- this._closeDropdown();
1410
- this._updateDialCode(listItem.getAttribute("data-dial-code"));
1411
- // focus the input
1412
- this.telInput.focus();
1413
- if (flagChanged) {
1414
- this._triggerCountryChange();
1415
- }
1258
+ }
1259
+ _setSelectedCountryFlagTitleAttribute(iso2, showSelectedDialCode) {
1260
+ if (!this.selectedFlag) {
1261
+ return;
1262
+ }
1263
+ let title;
1264
+ if (iso2 && !showSelectedDialCode) {
1265
+ title = `${this.selectedCountryData.name}: +${this.selectedCountryData.dialCode}`;
1266
+ } else if (iso2) {
1267
+ // For screen reader output, we don't want to include the dial code in the reader output twice
1268
+ // so just use the selected country name here:
1269
+ title = this.selectedCountryData.name;
1270
+ } else {
1271
+ title = "Unknown";
1272
+ }
1273
+ this.selectedFlag.setAttribute("title", title);
1274
+ }
1275
+ // when the input is in a hidden container during initialisation, we must inject some markup
1276
+ // into the end of the DOM to calculate the correct offsetWidth
1277
+ // NOTE: this is only used when showSelectedDialCode is enabled, so flagsContainer and selectedFlag
1278
+ // will definitely exist
1279
+ _getHiddenSelectedFlagWidth() {
1280
+ // to get the right styling to apply, all we need is a shallow clone of the container,
1281
+ // and then to inject a deep clone of the selectedFlag element
1282
+ const containerClone = this.telInput.parentNode.cloneNode();
1283
+ containerClone.style.visibility = "hidden";
1284
+ document.body.appendChild(containerClone);
1285
+ const flagsContainerClone = this.flagsContainer.cloneNode();
1286
+ containerClone.appendChild(flagsContainerClone);
1287
+ const selectedFlagClone = this.selectedFlag.cloneNode(true);
1288
+ flagsContainerClone.appendChild(selectedFlagClone);
1289
+ const width = selectedFlagClone.offsetWidth;
1290
+ containerClone.parentNode.removeChild(containerClone);
1291
+ return width;
1292
+ }
1293
+ // update the input placeholder to an example number from the currently selected country
1294
+ _updatePlaceholder() {
1295
+ const {
1296
+ autoPlaceholder,
1297
+ placeholderNumberType,
1298
+ nationalMode,
1299
+ customPlaceholder
1300
+ } = this.options;
1301
+ const shouldSetPlaceholder = autoPlaceholder === "aggressive" || !this.hadInitialPlaceholder && autoPlaceholder === "polite";
1302
+ if (window.intlTelInputUtils && shouldSetPlaceholder) {
1303
+ const numberType = intlTelInputUtils.numberType[placeholderNumberType];
1304
+ // note: must set placeholder to empty string if no country selected (globe icon showing)
1305
+ let placeholder = this.selectedCountryData.iso2 ? intlTelInputUtils.getExampleNumber(this.selectedCountryData.iso2, nationalMode, numberType) : "";
1306
+ placeholder = this._beforeSetNumber(placeholder);
1307
+ if (typeof customPlaceholder === "function") {
1308
+ placeholder = customPlaceholder(placeholder, this.selectedCountryData);
1309
+ }
1310
+ this.telInput.setAttribute("placeholder", placeholder);
1416
1311
  }
1417
- }, {
1418
- key: "_closeDropdown",
1419
- value: function _closeDropdown() {
1420
- this.dropdownContent.classList.add("iti__hide");
1421
- this.selectedFlag.setAttribute("aria-expanded", "false");
1422
- this.selectedFlag.removeAttribute("aria-activedescendant");
1423
- if (this.highlightedItem) {
1424
- this.highlightedItem.setAttribute("aria-selected", "false");
1425
- }
1426
- if (this.options.countrySearch) {
1427
- this.searchInput.removeAttribute("aria-activedescendant");
1312
+ }
1313
+ // called when the user selects a list item from the dropdown
1314
+ _selectListItem(listItem) {
1315
+ // update selected flag and active list item
1316
+ const flagChanged = this._setCountry(listItem.getAttribute("data-country-code"));
1317
+ this._closeDropdown();
1318
+ this._updateDialCode(listItem.getAttribute("data-dial-code"));
1319
+ // focus the input
1320
+ this.telInput.focus();
1321
+ if (flagChanged) {
1322
+ this._triggerCountryChange();
1323
+ }
1324
+ }
1325
+ // close the dropdown and unbind any listeners
1326
+ _closeDropdown() {
1327
+ this.dropdownContent.classList.add("iti__hide");
1328
+ this.selectedFlag.setAttribute("aria-expanded", "false");
1329
+ this.selectedFlag.removeAttribute("aria-activedescendant");
1330
+ if (this.highlightedItem) {
1331
+ this.highlightedItem.setAttribute("aria-selected", "false");
1332
+ }
1333
+ if (this.options.countrySearch) {
1334
+ this.searchInput.removeAttribute("aria-activedescendant");
1335
+ }
1336
+ // update the arrow
1337
+ this.dropdownArrow.classList.remove("iti__arrow--up");
1338
+ // unbind key events
1339
+ document.removeEventListener("keydown", this._handleKeydownOnDropdown);
1340
+ if (this.options.countrySearch) {
1341
+ this.searchInput.removeEventListener("input", this._handleSearchChange);
1342
+ }
1343
+ document.documentElement.removeEventListener("click", this._handleClickOffToClose);
1344
+ this.countryList.removeEventListener("mouseover", this._handleMouseoverCountryList);
1345
+ this.countryList.removeEventListener("click", this._handleClickCountryList);
1346
+ // remove menu from container
1347
+ if (this.options.dropdownContainer) {
1348
+ if (!this.options.useFullscreenPopup) {
1349
+ window.removeEventListener("scroll", this._handleWindowScroll);
1428
1350
  }
1429
- // update the arrow
1430
- this.dropdownArrow.classList.remove("iti__arrow--up");
1431
- // unbind key events
1432
- document.removeEventListener("keydown", this._handleKeydownOnDropdown);
1433
- if (this.options.countrySearch) {
1434
- this.searchInput.removeEventListener("input", this._handleSearchChange);
1351
+ if (this.dropdown.parentNode) {
1352
+ this.dropdown.parentNode.removeChild(this.dropdown);
1435
1353
  }
1436
- document.documentElement.removeEventListener("click", this._handleClickOffToClose);
1437
- this.countryList.removeEventListener("mouseover", this._handleMouseoverCountryList);
1438
- this.countryList.removeEventListener("click", this._handleClickCountryList);
1439
- // remove menu from container
1440
- if (this.options.dropdownContainer) {
1441
- if (!this.options.useFullscreenPopup) {
1442
- window.removeEventListener("scroll", this._handleWindowScroll);
1443
- }
1444
- if (this.dropdown.parentNode) {
1445
- this.dropdown.parentNode.removeChild(this.dropdown);
1446
- }
1447
- }
1448
- this._trigger("close:countrydropdown");
1449
1354
  }
1450
- }, {
1451
- key: "_scrollTo",
1452
- value: function _scrollTo(element, middle) {
1453
- var container = this.countryList;
1454
- // windowTop from https://stackoverflow.com/a/14384091/217866
1455
- var windowTop = document.documentElement.scrollTop;
1456
- var containerHeight = container.offsetHeight;
1457
- var containerTop = container.getBoundingClientRect().top + windowTop;
1458
- var containerBottom = containerTop + containerHeight;
1459
- var elementHeight = element.offsetHeight;
1460
- var elementTop = element.getBoundingClientRect().top + windowTop;
1461
- var elementBottom = elementTop + elementHeight;
1462
- var newScrollTop = elementTop - containerTop + container.scrollTop;
1463
- var middleOffset = containerHeight / 2 - elementHeight / 2;
1464
- if (elementTop < containerTop) {
1465
- // scroll up
1466
- if (middle) {
1467
- newScrollTop -= middleOffset;
1468
- }
1469
- container.scrollTop = newScrollTop;
1470
- } else if (elementBottom > containerBottom) {
1471
- // scroll down
1472
- if (middle) {
1473
- newScrollTop += middleOffset;
1474
- }
1475
- var heightDifference = containerHeight - elementHeight;
1476
- container.scrollTop = newScrollTop - heightDifference;
1477
- }
1355
+ this._trigger("close:countrydropdown");
1356
+ }
1357
+ // check if an element is visible within it's container, else scroll until it is
1358
+ _scrollTo(element, middle) {
1359
+ const container = this.countryList;
1360
+ // windowTop from https://stackoverflow.com/a/14384091/217866
1361
+ const windowTop = document.documentElement.scrollTop;
1362
+ const containerHeight = container.offsetHeight;
1363
+ const containerTop = container.getBoundingClientRect().top + windowTop;
1364
+ const containerBottom = containerTop + containerHeight;
1365
+ const elementHeight = element.offsetHeight;
1366
+ const elementTop = element.getBoundingClientRect().top + windowTop;
1367
+ const elementBottom = elementTop + elementHeight;
1368
+ let newScrollTop = elementTop - containerTop + container.scrollTop;
1369
+ const middleOffset = containerHeight / 2 - elementHeight / 2;
1370
+ if (elementTop < containerTop) {
1371
+ // scroll up
1372
+ if (middle) {
1373
+ newScrollTop -= middleOffset;
1374
+ }
1375
+ container.scrollTop = newScrollTop;
1376
+ } else if (elementBottom > containerBottom) {
1377
+ // scroll down
1378
+ if (middle) {
1379
+ newScrollTop += middleOffset;
1380
+ }
1381
+ const heightDifference = containerHeight - elementHeight;
1382
+ container.scrollTop = newScrollTop - heightDifference;
1478
1383
  }
1479
- }, {
1480
- key: "_updateDialCode",
1481
- value: function _updateDialCode(newDialCodeBare) {
1482
- var inputVal = this.telInput.value;
1483
- // save having to pass this every time
1484
- var newDialCode = "+".concat(newDialCodeBare);
1485
- var newNumber;
1486
- if (inputVal.charAt(0) === "+") {
1487
- // there's a plus so we're dealing with a replacement
1488
- var prevDialCode = this._getDialCode(inputVal);
1489
- if (prevDialCode) {
1490
- // current number contains a valid dial code, so replace it
1491
- newNumber = inputVal.replace(prevDialCode, newDialCode);
1492
- } else {
1493
- // current number contains an invalid dial code, so ditch it
1494
- // (no way to determine where the invalid dial code ends and the rest of the number begins)
1495
- newNumber = newDialCode;
1496
- }
1497
- this.telInput.value = newNumber;
1384
+ }
1385
+ // replace any existing dial code with the new one
1386
+ // Note: called from _selectListItem and setCountry
1387
+ _updateDialCode(newDialCodeBare) {
1388
+ const inputVal = this.telInput.value;
1389
+ // save having to pass this every time
1390
+ const newDialCode = `+${newDialCodeBare}`;
1391
+ let newNumber;
1392
+ if (inputVal.charAt(0) === "+") {
1393
+ // there's a plus so we're dealing with a replacement
1394
+ const prevDialCode = this._getDialCode(inputVal);
1395
+ if (prevDialCode) {
1396
+ // current number contains a valid dial code, so replace it
1397
+ newNumber = inputVal.replace(prevDialCode, newDialCode);
1398
+ } else {
1399
+ // current number contains an invalid dial code, so ditch it
1400
+ // (no way to determine where the invalid dial code ends and the rest of the number begins)
1401
+ newNumber = newDialCode;
1498
1402
  }
1403
+ this.telInput.value = newNumber;
1499
1404
  }
1500
- }, {
1501
- key: "_getDialCode",
1502
- value: function _getDialCode(number, includeAreaCode) {
1503
- var dialCode = "";
1504
- // only interested in international numbers (starting with a plus)
1505
- if (number.charAt(0) === "+") {
1506
- var numericChars = "";
1507
- // iterate over chars
1508
- for (var i = 0; i < number.length; i++) {
1509
- var c = number.charAt(i);
1510
- // if char is number (https://stackoverflow.com/a/8935649/217866)
1511
- if (!isNaN(parseInt(c, 10))) {
1512
- numericChars += c;
1513
- // if current numericChars make a valid dial code
1514
- if (includeAreaCode) {
1515
- if (this.dialCodeToIso2Map[numericChars]) {
1516
- // store the actual raw string (useful for matching later)
1517
- dialCode = number.substr(0, i + 1);
1518
- }
1519
- } else {
1520
- if (this.dialCodes[numericChars]) {
1521
- dialCode = number.substr(0, i + 1);
1522
- // if we're just looking for a dial code, we can break as soon as we find one
1523
- break;
1524
- }
1405
+ }
1406
+ // try and extract a valid international dial code from a full telephone number
1407
+ // Note: returns the raw string inc plus character and any whitespace/dots etc
1408
+ _getDialCode(number, includeAreaCode) {
1409
+ let dialCode = "";
1410
+ // only interested in international numbers (starting with a plus)
1411
+ if (number.charAt(0) === "+") {
1412
+ let numericChars = "";
1413
+ // iterate over chars
1414
+ for (let i = 0; i < number.length; i++) {
1415
+ const c = number.charAt(i);
1416
+ // if char is number (https://stackoverflow.com/a/8935649/217866)
1417
+ if (!isNaN(parseInt(c, 10))) {
1418
+ numericChars += c;
1419
+ // if current numericChars make a valid dial code
1420
+ if (includeAreaCode) {
1421
+ if (this.dialCodeToIso2Map[numericChars]) {
1422
+ // store the actual raw string (useful for matching later)
1423
+ dialCode = number.substr(0, i + 1);
1525
1424
  }
1526
- // stop searching as soon as we can - in this case when we hit max len
1527
- if (numericChars.length === this.dialCodeMaxLen) {
1425
+ } else {
1426
+ if (this.dialCodes[numericChars]) {
1427
+ dialCode = number.substr(0, i + 1);
1428
+ // if we're just looking for a dial code, we can break as soon as we find one
1528
1429
  break;
1529
1430
  }
1530
1431
  }
1432
+ // stop searching as soon as we can - in this case when we hit max len
1433
+ if (numericChars.length === this.dialCodeMaxLen) {
1434
+ break;
1435
+ }
1531
1436
  }
1532
1437
  }
1533
- return dialCode;
1534
- }
1535
- }, {
1536
- key: "_getFullNumber",
1537
- value: function _getFullNumber() {
1538
- var val = this.telInput.value.trim();
1539
- var dialCode = this.selectedCountryData.dialCode;
1540
- var prefix;
1541
- var numericVal = this._getNumeric(val);
1542
- if (this.options.showSelectedDialCode && !this.options.nationalMode && val.charAt(0) !== "+" && dialCode && numericVal) {
1543
- // when using showSelectedDialCode, it is visible so is effectively part of the typed number
1544
- prefix = "+".concat(dialCode);
1545
- } else {
1546
- prefix = "";
1547
- }
1548
- return prefix + val;
1549
- }
1550
- }, {
1551
- key: "_beforeSetNumber",
1552
- value: function _beforeSetNumber(fullNumber) {
1553
- var number = fullNumber;
1554
- if (this.options.showSelectedDialCode) {
1555
- var dialCode = this._getDialCode(number);
1556
- // if there is a valid dial code
1557
- if (dialCode) {
1558
- // in case _getDialCode returned an area code as well
1559
- dialCode = "+".concat(this.selectedCountryData.dialCode);
1560
- // a lot of numbers will have a space separating the dial code and the main number, and
1561
- // some NANP numbers will have a hyphen e.g. +1 684-733-1234 - in both cases we want to get
1562
- // rid of it
1563
- // NOTE: don't just trim all non-numerics as may want to preserve an open parenthesis etc
1564
- var start = number[dialCode.length] === " " || number[dialCode.length] === "-" ? dialCode.length + 1 : dialCode.length;
1565
- number = number.substr(start);
1566
- }
1567
- }
1568
- return this._cap(number);
1569
- }
1570
- }, {
1571
- key: "_triggerCountryChange",
1572
- value: function _triggerCountryChange() {
1573
- this._trigger("countrychange");
1574
- }
1575
- }, {
1576
- key: "_formatNumberAsYouType",
1577
- value: function _formatNumberAsYouType() {
1578
- var val = this._getFullNumber();
1579
- var result = window.intlTelInputUtils ? intlTelInputUtils.formatNumberAsYouType(val, this.selectedCountryData.iso2) : val;
1580
- // if showSelectedDialCode and they haven't (re)typed the dial code in the input as well, then remove the dial code
1581
- var dialCode = this.selectedCountryData.dialCode;
1582
- if (this.options.showSelectedDialCode && !this.options.nationalMode && this.telInput.value.charAt(0) !== "+" && result.includes("+".concat(dialCode))) {
1583
- var afterDialCode = result.split("+".concat(dialCode))[1] || "";
1584
- return afterDialCode.trim();
1585
- }
1586
- return result;
1587
- }
1588
- }, {
1589
- key: "handleAutoCountry",
1590
- value: function handleAutoCountry() {
1591
- if (this.options.initialCountry === "auto") {
1592
- // we must set this even if there is an initial val in the input: in case the initial val is
1593
- // invalid and they delete it - they should see their auto country
1594
- this.defaultCountry = window.intlTelInputGlobals.autoCountry;
1595
- // if there's no initial value in the input, then update the flag
1596
- if (!this.telInput.value) {
1597
- this.setCountry(this.defaultCountry);
1598
- }
1599
- this.resolveAutoCountryPromise();
1600
- }
1601
1438
  }
1602
- }, {
1603
- key: "handleUtils",
1604
- value: function handleUtils() {
1605
- // if the request was successful
1606
- if (window.intlTelInputUtils) {
1607
- // if there's an initial value in the input, then format it
1608
- if (this.telInput.value) {
1609
- this._updateValFromNumber(this.telInput.value);
1610
- }
1611
- if (this.selectedCountryData.iso2) {
1612
- this._updatePlaceholder();
1613
- }
1439
+ return dialCode;
1440
+ }
1441
+ // get the input val, adding the dial code if showSelectedDialCode is enabled
1442
+ _getFullNumber() {
1443
+ const val = this.telInput.value.trim();
1444
+ const {
1445
+ dialCode
1446
+ } = this.selectedCountryData;
1447
+ let prefix;
1448
+ const numericVal = this._getNumeric(val);
1449
+ if (this.options.showSelectedDialCode && !this.options.nationalMode && val.charAt(0) !== "+" && dialCode && numericVal) {
1450
+ // when using showSelectedDialCode, it is visible so is effectively part of the typed number
1451
+ prefix = `+${dialCode}`;
1452
+ } else {
1453
+ prefix = "";
1454
+ }
1455
+ return prefix + val;
1456
+ }
1457
+ // remove the dial code if showSelectedDialCode is enabled
1458
+ // also cap the length if the input has a maxlength attribute
1459
+ _beforeSetNumber(fullNumber) {
1460
+ let number = fullNumber;
1461
+ if (this.options.showSelectedDialCode) {
1462
+ let dialCode = this._getDialCode(number);
1463
+ // if there is a valid dial code
1464
+ if (dialCode) {
1465
+ // in case _getDialCode returned an area code as well
1466
+ dialCode = `+${this.selectedCountryData.dialCode}`;
1467
+ // a lot of numbers will have a space separating the dial code and the main number, and
1468
+ // some NANP numbers will have a hyphen e.g. +1 684-733-1234 - in both cases we want to get
1469
+ // rid of it
1470
+ // NOTE: don't just trim all non-numerics as may want to preserve an open parenthesis etc
1471
+ const start = number[dialCode.length] === " " || number[dialCode.length] === "-" ? dialCode.length + 1 : dialCode.length;
1472
+ number = number.substr(start);
1614
1473
  }
1615
- this.resolveUtilsScriptPromise();
1616
1474
  }
1617
- }, {
1618
- key: "destroy",
1619
- value: function destroy() {
1620
- if (this.options.allowDropdown) {
1621
- // make sure the dropdown is closed (and unbind listeners)
1622
- this._closeDropdown();
1623
- this.selectedFlag.removeEventListener("click", this._handleClickSelectedFlag);
1624
- this.flagsContainer.removeEventListener("keydown", this._handleFlagsContainerKeydown);
1625
- // label click hack
1626
- var label = this.telInput.closest("label");
1627
- if (label) {
1628
- label.removeEventListener("click", this._handleLabelClick);
1629
- }
1630
- }
1631
- // unbind hiddenInput listeners
1632
- var form = this.telInput.form;
1633
- if (this._handleHiddenInputSubmit && form) {
1634
- form.removeEventListener("submit", this._handleHiddenInputSubmit);
1635
- }
1636
- // unbind key events, and cut/paste events
1637
- this.telInput.removeEventListener("input", this._handleKeyEvent);
1638
- // remove attribute of id instance: data-intl-tel-input-id
1639
- this.telInput.removeAttribute("data-intl-tel-input-id");
1640
- // remove markup (but leave the original input)
1641
- var wrapper = this.telInput.parentNode;
1642
- wrapper.parentNode.insertBefore(this.telInput, wrapper);
1643
- wrapper.parentNode.removeChild(wrapper);
1644
- delete window.intlTelInputGlobals.instances[this.id];
1645
- }
1646
- }, {
1647
- key: "getExtension",
1648
- value: function getExtension() {
1649
- if (window.intlTelInputUtils) {
1650
- return intlTelInputUtils.getExtension(this._getFullNumber(), this.selectedCountryData.iso2);
1651
- }
1652
- return "";
1653
- }
1654
- }, {
1655
- key: "getNumber",
1656
- value: function getNumber(format) {
1657
- if (window.intlTelInputUtils) {
1658
- var iso2 = this.selectedCountryData.iso2;
1659
- return intlTelInputUtils.formatNumber(this._getFullNumber(), iso2, format);
1660
- }
1661
- return "";
1475
+ return this._cap(number);
1476
+ }
1477
+ // trigger the 'countrychange' event
1478
+ _triggerCountryChange() {
1479
+ this._trigger("countrychange");
1480
+ }
1481
+ // format the number as the user types
1482
+ _formatNumberAsYouType() {
1483
+ const val = this._getFullNumber();
1484
+ const result = window.intlTelInputUtils ? intlTelInputUtils.formatNumberAsYouType(val, this.selectedCountryData.iso2) : val;
1485
+ // if showSelectedDialCode and they haven't (re)typed the dial code in the input as well, then remove the dial code
1486
+ const {
1487
+ dialCode
1488
+ } = this.selectedCountryData;
1489
+ if (this.options.showSelectedDialCode && !this.options.nationalMode && this.telInput.value.charAt(0) !== "+" && result.includes(`+${dialCode}`)) {
1490
+ const afterDialCode = result.split(`+${dialCode}`)[1] || "";
1491
+ return afterDialCode.trim();
1492
+ }
1493
+ return result;
1494
+ }
1495
+ /**************************
1496
+ * SECRET PUBLIC METHODS
1497
+ **************************/
1498
+ // this is called when the geoip call returns
1499
+ handleAutoCountry() {
1500
+ if (this.options.initialCountry === "auto") {
1501
+ // we must set this even if there is an initial val in the input: in case the initial val is
1502
+ // invalid and they delete it - they should see their auto country
1503
+ this.defaultCountry = window.intlTelInputGlobals.autoCountry;
1504
+ // if there's no initial value in the input, then update the flag
1505
+ if (!this.telInput.value) {
1506
+ this.setCountry(this.defaultCountry);
1507
+ }
1508
+ this.resolveAutoCountryPromise();
1662
1509
  }
1663
- }, {
1664
- key: "getNumberType",
1665
- value: function getNumberType() {
1666
- if (window.intlTelInputUtils) {
1667
- return intlTelInputUtils.getNumberType(this._getFullNumber(), this.selectedCountryData.iso2);
1668
- }
1669
- return -99;
1670
- }
1671
- }, {
1672
- key: "getSelectedCountryData",
1673
- value: function getSelectedCountryData() {
1674
- return this.selectedCountryData;
1675
- }
1676
- }, {
1677
- key: "getValidationError",
1678
- value: function getValidationError() {
1679
- if (window.intlTelInputUtils) {
1680
- var iso2 = this.selectedCountryData.iso2;
1681
- return intlTelInputUtils.getValidationError(this._getFullNumber(), iso2);
1682
- }
1683
- return -99;
1684
- }
1685
- }, {
1686
- key: "isValidNumber",
1687
- value: function isValidNumber() {
1688
- var mobileOnly = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : true;
1689
- var val = this._getFullNumber();
1690
- // return false for any alpha chars
1691
- 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)) {
1692
- return false;
1693
- }
1694
- return window.intlTelInputUtils ? intlTelInputUtils.isPossibleNumber(val, this.selectedCountryData.iso2, mobileOnly) : null;
1695
- }
1696
- }, {
1697
- key: "isValidNumberPrecise",
1698
- value: function isValidNumberPrecise() {
1699
- var val = this._getFullNumber();
1700
- // return false for any alpha chars
1701
- 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)) {
1702
- return false;
1510
+ }
1511
+ // this is called when the utils request completes
1512
+ handleUtils() {
1513
+ // if the request was successful
1514
+ if (window.intlTelInputUtils) {
1515
+ // if there's an initial value in the input, then format it
1516
+ if (this.telInput.value) {
1517
+ this._updateValFromNumber(this.telInput.value);
1703
1518
  }
1704
- return window.intlTelInputUtils ? intlTelInputUtils.isValidNumber(val, this.selectedCountryData.iso2) : null;
1705
- }
1706
- }, {
1707
- key: "setCountry",
1708
- value: function setCountry(iso2) {
1709
- var iso2Lower = iso2.toLowerCase();
1710
- // check if already selected
1711
- if (this.selectedCountryData.iso2 !== iso2Lower) {
1712
- this._setCountry(iso2Lower);
1713
- this._updateDialCode(this.selectedCountryData.dialCode);
1714
- this._triggerCountryChange();
1519
+ if (this.selectedCountryData.iso2) {
1520
+ this._updatePlaceholder();
1521
+ this._updateMaxLength();
1715
1522
  }
1716
1523
  }
1717
- }, {
1718
- key: "setNumber",
1719
- value: function setNumber(number) {
1720
- // we must update the flag first, which updates this.selectedCountryData, which is used for
1721
- // formatting the number before displaying it
1722
- var flagChanged = this._updateFlagFromNumber(number);
1723
- this._updateValFromNumber(number);
1724
- if (flagChanged) {
1725
- this._triggerCountryChange();
1726
- }
1524
+ this.resolveUtilsScriptPromise();
1525
+ }
1526
+ /********************
1527
+ * PUBLIC METHODS
1528
+ ********************/
1529
+ // remove plugin
1530
+ destroy() {
1531
+ if (this.options.allowDropdown) {
1532
+ // make sure the dropdown is closed (and unbind listeners)
1533
+ this._closeDropdown();
1534
+ this.selectedFlag.removeEventListener("click", this._handleClickSelectedFlag);
1535
+ this.flagsContainer.removeEventListener("keydown", this._handleFlagsContainerKeydown);
1536
+ // label click hack
1537
+ const label = this.telInput.closest("label");
1538
+ if (label) {
1539
+ label.removeEventListener("click", this._handleLabelClick);
1540
+ }
1541
+ }
1542
+ // unbind hiddenInput listeners
1543
+ const {
1544
+ form
1545
+ } = this.telInput;
1546
+ if (this._handleHiddenInputSubmit && form) {
1547
+ form.removeEventListener("submit", this._handleHiddenInputSubmit);
1548
+ }
1549
+ // unbind key events, and cut/paste events
1550
+ this.telInput.removeEventListener("input", this._handleInputEvent);
1551
+ if (this._handleKeydownEvent) {
1552
+ this.telInput.removeEventListener("keydown", this._handleKeydownEvent);
1553
+ }
1554
+ // remove attribute of id instance: data-intl-tel-input-id
1555
+ this.telInput.removeAttribute("data-intl-tel-input-id");
1556
+ // remove markup (but leave the original input)
1557
+ const wrapper = this.telInput.parentNode;
1558
+ wrapper.parentNode.insertBefore(this.telInput, wrapper);
1559
+ wrapper.parentNode.removeChild(wrapper);
1560
+ delete window.intlTelInputGlobals.instances[this.id];
1561
+ }
1562
+ // get the extension from the current number
1563
+ getExtension() {
1564
+ if (window.intlTelInputUtils) {
1565
+ return intlTelInputUtils.getExtension(this._getFullNumber(), this.selectedCountryData.iso2);
1727
1566
  }
1728
- }, {
1729
- key: "setPlaceholderNumberType",
1730
- value: function setPlaceholderNumberType(type) {
1731
- this.options.placeholderNumberType = type;
1732
- this._updatePlaceholder();
1567
+ return "";
1568
+ }
1569
+ // format the number to the given format
1570
+ getNumber(format) {
1571
+ if (window.intlTelInputUtils) {
1572
+ const {
1573
+ iso2
1574
+ } = this.selectedCountryData;
1575
+ return intlTelInputUtils.formatNumber(this._getFullNumber(), iso2, format);
1576
+ }
1577
+ return "";
1578
+ }
1579
+ // get the type of the entered number e.g. landline/mobile
1580
+ getNumberType() {
1581
+ if (window.intlTelInputUtils) {
1582
+ return intlTelInputUtils.getNumberType(this._getFullNumber(), this.selectedCountryData.iso2);
1733
1583
  }
1734
- } ]);
1735
- return Iti;
1736
- }();
1584
+ return -99;
1585
+ }
1586
+ // get the country data for the currently selected flag
1587
+ getSelectedCountryData() {
1588
+ return this.selectedCountryData;
1589
+ }
1590
+ // get the validation error
1591
+ getValidationError() {
1592
+ if (window.intlTelInputUtils) {
1593
+ const {
1594
+ iso2
1595
+ } = this.selectedCountryData;
1596
+ return intlTelInputUtils.getValidationError(this._getFullNumber(), iso2);
1597
+ }
1598
+ return -99;
1599
+ }
1600
+ // validate the input val - assumes the global function isPossibleNumber (from utilsScript)
1601
+ isValidNumber(mobileOnly = true) {
1602
+ const val = this._getFullNumber();
1603
+ // return false for any alpha chars
1604
+ if (/\p{L}/u.test(val)) {
1605
+ return false;
1606
+ }
1607
+ return window.intlTelInputUtils ? intlTelInputUtils.isPossibleNumber(val, this.selectedCountryData.iso2, mobileOnly) : null;
1608
+ }
1609
+ // validate the input val (precise) - assumes the global function isValidNumber (from utilsScript)
1610
+ isValidNumberPrecise() {
1611
+ const val = this._getFullNumber();
1612
+ // return false for any alpha chars
1613
+ if (/\p{L}/u.test(val)) {
1614
+ return false;
1615
+ }
1616
+ return window.intlTelInputUtils ? intlTelInputUtils.isValidNumber(val, this.selectedCountryData.iso2) : null;
1617
+ }
1618
+ // update the selected flag, and update the input val accordingly
1619
+ setCountry(iso2) {
1620
+ const iso2Lower = iso2.toLowerCase();
1621
+ // check if already selected
1622
+ if (this.selectedCountryData.iso2 !== iso2Lower) {
1623
+ this._setCountry(iso2Lower);
1624
+ this._updateDialCode(this.selectedCountryData.dialCode);
1625
+ this._triggerCountryChange();
1626
+ }
1627
+ }
1628
+ // set the input value and update the flag
1629
+ setNumber(number) {
1630
+ // we must update the flag first, which updates this.selectedCountryData, which is used for
1631
+ // formatting the number before displaying it
1632
+ const flagChanged = this._updateFlagFromNumber(number);
1633
+ this._updateValFromNumber(number);
1634
+ if (flagChanged) {
1635
+ this._triggerCountryChange();
1636
+ }
1637
+ }
1638
+ // set the placeholder number typ
1639
+ setPlaceholderNumberType(type) {
1640
+ this.options.placeholderNumberType = type;
1641
+ this._updatePlaceholder();
1642
+ }
1643
+ }
1737
1644
  /********************
1738
1645
  * STATIC METHODS
1739
1646
  ********************/
1740
1647
  // get the country data object
1741
- intlTelInputGlobals.getCountryData = function() {
1742
- return allCountries;
1743
- };
1648
+ intlTelInputGlobals.getCountryData = () => allCountries;
1744
1649
  // inject a <script> element to load utils.js
1745
- var injectScript = function injectScript(path, handleSuccess, handleFailure) {
1650
+ const injectScript = (path, handleSuccess, handleFailure) => {
1746
1651
  // inject a new script element into the page
1747
- var script = document.createElement("script");
1748
- script.onload = function() {
1652
+ const script = document.createElement("script");
1653
+ script.onload = () => {
1749
1654
  forEachInstance("handleUtils");
1750
1655
  if (handleSuccess) {
1751
1656
  handleSuccess();
1752
1657
  }
1753
1658
  };
1754
- script.onerror = function() {
1659
+ script.onerror = () => {
1755
1660
  forEachInstance("rejectUtilsScriptPromise");
1756
1661
  if (handleFailure) {
1757
1662
  handleFailure();
@@ -1763,7 +1668,7 @@
1763
1668
  document.body.appendChild(script);
1764
1669
  };
1765
1670
  // load the utils script
1766
- intlTelInputGlobals.loadUtils = function(path) {
1671
+ intlTelInputGlobals.loadUtils = path => {
1767
1672
  // 2 options:
1768
1673
  // 1) not already started loading (start)
1769
1674
  // 2) already started loading (do nothing - just wait for the onload callback to fire, which will
@@ -1771,16 +1676,14 @@
1771
1676
  if (!window.intlTelInputUtils && !window.intlTelInputGlobals.startedLoadingUtilsScript) {
1772
1677
  // only do this once
1773
1678
  window.intlTelInputGlobals.startedLoadingUtilsScript = true;
1774
- return new Promise(function(resolve, reject) {
1775
- return injectScript(path, resolve, reject);
1776
- });
1679
+ return new Promise((resolve, reject) => injectScript(path, resolve, reject));
1777
1680
  }
1778
1681
  return null;
1779
1682
  };
1780
1683
  // default options
1781
1684
  intlTelInputGlobals.defaults = defaults;
1782
1685
  // version
1783
- intlTelInputGlobals.version = "20.1.0";
1686
+ intlTelInputGlobals.version = "20.3.0";
1784
1687
  var pluginName = "intlTelInput";
1785
1688
  // A really lightweight plugin wrapper around the constructor,
1786
1689
  // preventing against multiple instantiations