intl-tel-input 19.3.0 → 19.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/spec.html CHANGED
@@ -90,6 +90,8 @@
90
90
 
91
91
  <script src="src/spec/tests/options/geoIpLookup.js"></script>
92
92
 
93
+ <script src="src/spec/tests/options/hiddenInput.js"></script>
94
+
93
95
  <script src="src/spec/tests/options/initialCountry.js"></script>
94
96
 
95
97
  <script src="src/spec/tests/options/nationalMode.js"></script>
@@ -515,17 +515,38 @@ class Iti {
515
515
 
516
516
  if (hiddenInput) {
517
517
  const telInputName = this.telInput.getAttribute("name");
518
- const hiddenInputName = hiddenInput(telInputName);
518
+ const result = hiddenInput(telInputName);
519
+ const isObject = result !== null && typeof result === "object";
520
+
521
+ let hiddenInputPhoneName;
522
+ let hiddenInputCountryName;
523
+
524
+ if (isObject) {
525
+ hiddenInputPhoneName = result.phone || telInputName;
526
+ hiddenInputCountryName = result.country || `${hiddenInputPhoneName}_country`;
527
+ } else {
528
+ hiddenInputPhoneName = result || telInputName;
529
+ hiddenInputCountryName = `${hiddenInputPhoneName}_country`;
530
+ }
531
+
532
+ // Check if a name has been determined for the phone input field after all conditions
533
+ if (!hiddenInputPhoneName) {
534
+ return;
535
+ }
536
+
537
+ // Create hidden input for the full international number
519
538
  this.hiddenInput = this._createEl("input", {
520
539
  type: "hidden",
521
- name: hiddenInputName
540
+ name: hiddenInputPhoneName
522
541
  });
523
- wrapper.appendChild(this.hiddenInput);
524
- // add a 2nd hidden input for the selected country code - this is useful for handling invalid numbers with server-side validation, as getNumber does not always include the international dial code for invalid numbers
542
+
543
+ // Create hidden input for the selected country code
525
544
  this.hiddenInputCountry = this._createEl("input", {
526
545
  type: "hidden",
527
- name: `${hiddenInputName}_country`
546
+ name: hiddenInputCountryName
528
547
  });
548
+
549
+ wrapper.appendChild(this.hiddenInput);
529
550
  wrapper.appendChild(this.hiddenInputCountry);
530
551
  }
531
552
  }
@@ -50,6 +50,11 @@ var getParentElement = function(i) {
50
50
  return i.parent();
51
51
  };
52
52
 
53
+ var getHiddenInputs = function(i) {
54
+ i = i || input;
55
+ return i.parent().find("input[type=hidden]");
56
+ };
57
+
53
58
  var getDropdownContent = function(i) {
54
59
  i = i || input;
55
60
  return i.parent().find(".iti__dropdown-content");
@@ -0,0 +1,120 @@
1
+ "use strict";
2
+
3
+ describe("hiddenInput: ", function() {
4
+
5
+ beforeEach(function() {
6
+ intlSetup();
7
+ });
8
+
9
+ afterEach(function() {
10
+ intlTeardown();
11
+ });
12
+
13
+ describe("init plugin with hiddenInput returning a string", function() {
14
+
15
+ beforeEach(function() {
16
+ input = $("<input>").wrap("div");
17
+ iti = window.intlTelInput(input[0], {
18
+ hiddenInput: () => "phone_full"
19
+ });
20
+ });
21
+
22
+ it("creates two hidden inputs", function() {
23
+ expect(getHiddenInputs()).toHaveLength(2);
24
+ });
25
+
26
+ it("sets the name of the first hidden input to the returned string", function() {
27
+ expect(getHiddenInputs().eq(0)).toHaveAttr("name", "phone_full");
28
+ });
29
+
30
+ it("sets the name of the second hidden input to the returned string with a '_country' suffix", function() {
31
+ expect(getHiddenInputs().eq(1)).toHaveAttr("name", "phone_full_country");
32
+ });
33
+ });
34
+
35
+ describe("init plugin with hiddenInput returning an object with correct properties", function() {
36
+
37
+ beforeEach(function() {
38
+ input = $("<input>").wrap("div");
39
+ iti = window.intlTelInput(input[0], {
40
+ hiddenInput: () => ({
41
+ phone: "phone_full",
42
+ country: "phone_country"
43
+ })
44
+ });
45
+ });
46
+
47
+ it("creates two hidden inputs", function() {
48
+ expect(getHiddenInputs()).toHaveLength(2);
49
+ });
50
+
51
+ it("sets the name of the first hidden input to the returned object's 'phone' property", function() {
52
+ expect(getHiddenInputs().eq(0)).toHaveAttr("name", "phone_full");
53
+ });
54
+
55
+ it("sets the name of the second hidden input to the returned object's 'country' property", function() {
56
+ expect(getHiddenInputs().eq(1)).toHaveAttr("name", "phone_country");
57
+ });
58
+ });
59
+
60
+ describe("init plugin with hiddenInput returning an object with incorrect properties", function() {
61
+
62
+ beforeEach(function() {
63
+ input = $("<input name='phone'>").wrap("div");
64
+ iti = window.intlTelInput(input[0], {
65
+ hiddenInput: () => ({
66
+ test: "test",
67
+ data: "data"
68
+ })
69
+ });
70
+ });
71
+
72
+ it("creates two hidden inputs", function() {
73
+ expect(getHiddenInputs()).toHaveLength(2);
74
+ });
75
+
76
+ it("sets the name of the hidden input to the name of the input", function() {
77
+ expect(getHiddenInputs().eq(0)).toHaveAttr("name", "phone");
78
+ });
79
+
80
+ it("sets the name of the second hidden input to the name of the input with a '_country' suffix", function() {
81
+ expect(getHiddenInputs().eq(1)).toHaveAttr("name", "phone_country");
82
+ });
83
+ });
84
+
85
+ describe("init plugin with hiddenInput returning an empty object", function() {
86
+
87
+ beforeEach(function() {
88
+ input = $("<input name='phone'>").wrap("div");
89
+ iti = window.intlTelInput(input[0], {
90
+ hiddenInput: () => ({})
91
+ });
92
+ });
93
+
94
+ it("creates two hidden inputs", function() {
95
+ expect(getHiddenInputs()).toHaveLength(2);
96
+ });
97
+
98
+ it("sets the name of the hidden input to the name of the input", function() {
99
+ expect(getHiddenInputs().eq(0)).toHaveAttr("name", "phone");
100
+ });
101
+
102
+ it("sets the name of the second hidden input to the name of the input with a '_country' suffix", function() {
103
+ expect(getHiddenInputs().eq(1)).toHaveAttr("name", "phone_country");
104
+ });
105
+ });
106
+
107
+ describe("init plugin with no name attribute set on input and hiddenInput returning an empty object", function() {
108
+
109
+ beforeEach(function() {
110
+ input = $("<input>").wrap("div");
111
+ iti = window.intlTelInput(input[0], {
112
+ hiddenInput: () => ({})
113
+ });
114
+ });
115
+
116
+ it("does not create any hidden inputs", function() {
117
+ expect(getHiddenInputs()).toHaveLength(0);
118
+ });
119
+ });
120
+ });