Skip to content

Commit 8fb2b44

Browse files
authored
Update form-handlers.js
Add `initIntlPhonePicker` function
1 parent 57cb169 commit 8fb2b44

File tree

1 file changed

+79
-0
lines changed

1 file changed

+79
-0
lines changed

src/utils/form-handlers.js

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,3 +391,82 @@ function successRedirect(values, followUpUrl, $form) {
391391
location.href = $form.data("redirect-url");
392392
return false;
393393
}
394+
395+
window.initIntlPhonePicker = function($form){
396+
// $form is jQuery object passed from formhandler.js
397+
const formEl = $form && $form[0];
398+
if (!formEl || formEl.__intlPhoneInit) return;
399+
// Wait for the *specific* Mkto form instance that matches this DOM node
400+
if (window.MktoForms2 && MktoForms2.whenReady) {
401+
MktoForms2.whenReady(function(form){
402+
const el = form.getFormElem()[0];
403+
if (el !== formEl) return; // <- scoping guard
404+
if (el.__intlPhoneInit) return;
405+
el.__intlPhoneInit = true;
406+
const FIELD_NAME = 'Phone';
407+
const mktoPhone = el.querySelector('input[name="'+FIELD_NAME+'"]');
408+
if (!mktoPhone) return;
409+
// Ensure visible tel input
410+
let visible = el.querySelector('.int-phone');
411+
if (!visible){
412+
visible = document.createElement('input');
413+
visible.type = 'tel';
414+
visible.className = 'int-phone';
415+
visible.placeholder = 'Your number here';
416+
visible.autocomplete = 'tel';
417+
mktoPhone.parentNode.insertBefore(visible, mktoPhone);
418+
}
419+
mktoPhone.style.display = 'none';
420+
// Ensure hidden fields exist in DOM
421+
function ensureHidden(name){
422+
let f = el.querySelector('[name="'+name+'"]');
423+
if (!f){
424+
f = document.createElement('input');
425+
f.type = 'hidden';
426+
f.name = name;
427+
el.appendChild(f);
428+
}
429+
return f;
430+
}
431+
// ISO2 and full country name
432+
const domIso2 = ensureHidden('Country');
433+
const domFull = ensureHidden('formCountry');
434+
// Ensure Marketo model knows about them (helps if fields aren't in form UI)
435+
if (form.addHiddenFields) {
436+
form.addHiddenFields({ Country:'', formCountry:'' });
437+
}
438+
if (!window.intlTelInput) {
439+
console.warn('intlTelInput is not loaded');
440+
return;
441+
}
442+
const iti = window.intlTelInput(visible, {
443+
initialCountry: "us",
444+
separateDialCode: true,
445+
preferredCountries: ["US","GB","CA"],
446+
utilsScript: "https://cdn.jsdelivr.net/npm/intl-tel-input@18.1.1/build/js/utils.js"
447+
});
448+
function sync(){
449+
// Don't call setNumber on every keystroke; it can be jumpy.
450+
const d = iti.getSelectedCountryData() || {};
451+
const full = d.name || '';
452+
const iso2 = (d.iso2 || '').toUpperCase();
453+
const e164 = iti.getNumber() || '';
454+
mktoPhone.value = e164;
455+
domIso2.value = iso2;
456+
domFull.value = full;
457+
if (form.vals) {
458+
const v = {};
459+
v[FIELD_NAME] = e164;
460+
v.Country = iso2;
461+
v.formCountry = full;
462+
form.vals(v);
463+
}
464+
}
465+
visible.addEventListener('input', sync);
466+
visible.addEventListener('change', sync);
467+
visible.addEventListener('countrychange', sync);
468+
if (form.onSubmit) form.onSubmit(sync);
469+
sync();
470+
});
471+
}
472+
};

0 commit comments

Comments
 (0)