Files
openfoodnetwork/app/webpacker/controllers/paymentmethod_controller.js
Jean-Baptiste Bellet 5f3ea6accb Do not init by hidden all the container
that allow to init the content without clicking on anything
2021-12-07 16:27:22 +00:00

37 lines
1.0 KiB
JavaScript

import { Controller } from "stimulus";
export default class extends Controller {
static targets = ["paymentMethod"];
selectPaymentMethod(event) {
const paymentMethodContainerId = event.target.dataset.paymentmethodId;
Array.from(
document.getElementsByClassName("paymentmethod-container")
).forEach((e) => {
if (e.id === paymentMethodContainerId) {
e.style.display = "block";
this.addRequiredAttributeOnInputIfNeeded(e);
} else {
e.style.display = "none";
this.removeRequiredAttributeOnInput(e);
}
});
}
removeRequiredAttributeOnInput(container) {
Array.from(container.getElementsByTagName("input")).forEach((i) => {
if (i.required) {
i.dataset.required = i.required;
i.required = false;
}
});
}
addRequiredAttributeOnInputIfNeeded(container) {
Array.from(container.getElementsByTagName("input")).forEach((i) => {
if (i.dataset.required === "true") {
i.required = true;
}
});
}
}