support: toggle correct elements on page load

This commit is contained in:
Pierre de La Morinerie 2019-03-14 11:41:54 +01:00
parent bc408e3374
commit 4e2e8e9a57

View file

@ -1,10 +1,16 @@
import { show, hide, delegate } from '@utils';
delegate('change', '#contact-form #type', event => {
const type = event.target.value;
const visibleElements = `[data-contact-type-only="${type}"]`;
const hiddenElements = `[data-contact-type-only]:not([data-contact-type-only="${type}"])`;
function updateContactElementsVisibility() {
const contactSelect = document.querySelector('#contact-form #type');
if (contactSelect) {
const type = contactSelect.value;
const visibleElements = `[data-contact-type-only="${type}"]`;
const hiddenElements = `[data-contact-type-only]:not([data-contact-type-only="${type}"])`;
document.querySelectorAll(visibleElements).forEach(show);
document.querySelectorAll(hiddenElements).forEach(hide);
});
document.querySelectorAll(visibleElements).forEach(show);
document.querySelectorAll(hiddenElements).forEach(hide);
}
}
addEventListener('turbolinks:load', updateContactElementsVisibility);
delegate('change', '#contact-form #type', updateContactElementsVisibility);