Skip to content

Commit c4e9776

Browse files
authored
Merge pull request #4 from envoy/intl-phone-picker
Create intl-phone-init.js
2 parents 43d1670 + 1e69b11 commit c4e9776

File tree

1 file changed

+74
-0
lines changed

1 file changed

+74
-0
lines changed

src/utils/intl-phone-init.js

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
(function() {
2+
var css = document.createElement('style');
3+
css.textContent =
4+
'.mktoForm .iti .int-phone { padding-left: 65px !important; height: 44px; line-height: 44px; box-sizing: border-box; width: 100%; }' +
5+
'.mktoForm .iti--separate-dial-code.iti--show-flags .iti__selected-dial-code { margin-right: 4px; }' +
6+
'.mktoForm .iti__selected-flag { width: 60px !important; padding: 5px !important; }' +
7+
'.mktoForm .iti__country-list { z-index: 9999 !important; }';
8+
document.head.appendChild(css);
9+
10+
function attach(form) {
11+
var el = form.getFormElem()[0];
12+
if (el.__intlBound) return;
13+
el.__intlBound = true;
14+
15+
var FIELD_NAME = 'Phone';
16+
var mktoPhone = el.querySelector('input[name="' + FIELD_NAME + '"]');
17+
if (!mktoPhone) return;
18+
19+
if (el.querySelector('.intl-tel-input,.int-phone')) return;
20+
21+
var visible = document.createElement('input');
22+
visible.type = 'tel';
23+
visible.className = 'int-phone';
24+
visible.placeholder = 'Your number here';
25+
visible.autocomplete = 'tel';
26+
mktoPhone.parentNode.insertBefore(visible, mktoPhone);
27+
mktoPhone.style.display = 'none';
28+
29+
var countryISO = el.querySelector('input[name="Country"]');
30+
if (!countryISO) {
31+
countryISO = document.createElement('input');
32+
countryISO.type = 'hidden';
33+
countryISO.name = 'Country';
34+
el.appendChild(countryISO);
35+
}
36+
37+
var inferred = el.querySelector('input[name="formCountry"]');
38+
if (!inferred) {
39+
inferred = document.createElement('input');
40+
inferred.type = 'hidden';
41+
inferred.name = 'formCountry';
42+
el.appendChild(inferred);
43+
}
44+
45+
var iti = window.intlTelInput(visible, {
46+
initialCountry: "us",
47+
separateDialCode: true,
48+
preferredCountries: ["us", "gb", "ca"]
49+
});
50+
51+
function sync() {
52+
iti.setNumber(visible.value);
53+
mktoPhone.value = iti.getNumber() || '';
54+
var d = iti.getSelectedCountryData() || {};
55+
countryISO.value = (d.iso2 || '').toUpperCase();
56+
inferred.value = d.name || '';
57+
}
58+
59+
visible.addEventListener('input', sync);
60+
visible.addEventListener('change', sync);
61+
visible.addEventListener('countrychange', sync);
62+
sync();
63+
form.onSubmit(sync);
64+
}
65+
66+
function waitForMarketo() {
67+
if (window.MktoForms2 && MktoForms2.whenReady) {
68+
MktoForms2.whenReady(attach);
69+
} else {
70+
setTimeout(waitForMarketo, 100);
71+
}
72+
}
73+
waitForMarketo();
74+
})();

0 commit comments

Comments
 (0)