diff --git a/app/webpacker/controllers/paymentmethod_controller.js b/app/webpacker/controllers/paymentmethod_controller.js index 133024227c..f352430b71 100644 --- a/app/webpacker/controllers/paymentmethod_controller.js +++ b/app/webpacker/controllers/paymentmethod_controller.js @@ -10,9 +10,11 @@ export default class extends Controller { if (e.id === paymentMethodContainerId) { e.style.display = "block"; this.addRequiredAttributeOnInputIfNeeded(e); + this.removeDisabledAttributeOnInput(e); } else { e.style.display = "none"; this.removeRequiredAttributeOnInput(e); + this.addDisabledAttributeOnInput(e); } }); } @@ -20,6 +22,19 @@ export default class extends Controller { getFormElementsArray(container) { return Array.from(container.querySelectorAll("input, select, textarea")); } + + addDisabledAttributeOnInput(container) { + this.getFormElementsArray(container).forEach((i) => { + i.disabled = true; + }); + } + + removeDisabledAttributeOnInput(container) { + this.getFormElementsArray(container).forEach((i) => { + i.disabled = false; + }); + } + removeRequiredAttributeOnInput(container) { this.getFormElementsArray(container).forEach((i) => { if (i.required) { diff --git a/spec/javascripts/stimulus/paymentmethod_controller_test.js b/spec/javascripts/stimulus/paymentmethod_controller_test.js index e7af59fec6..aab38a9615 100644 --- a/spec/javascripts/stimulus/paymentmethod_controller_test.js +++ b/spec/javascripts/stimulus/paymentmethod_controller_test.js @@ -13,9 +13,24 @@ describe("PaymentmethodController", () => { - -
- + +
+ + +
+ `; const application = Application.start(); @@ -78,5 +93,45 @@ describe("PaymentmethodController", () => { expect(input2.required).toBe(false); expect(input3.required).toBe(false); }); + + it("handle well the add/remove 'disabled='disabled'' attribute on each input/select", () => { + const paymentMethod1 = document.getElementById("paymentmethod_1"); + const paymentMethod2 = document.getElementById("paymentmethod_2"); + const paymentMethod3 = document.getElementById("paymentmethod_3"); + + const input1 = document.getElementById("input1"); + const input2 = document.getElementById("input2"); + const input3 = document.getElementById("input3"); + const select1 = document.getElementById("select1"); + const select2 = document.getElementById("select2"); + const select3 = document.getElementById("select3"); + + paymentMethod1.click(); + expect(input1.disabled).toBe(false); + expect(select1.disabled).toBe(false); + + expect(input2.disabled).toBe(true); + expect(select2.disabled).toBe(true); + expect(input3.disabled).toBe(true); + expect(select3.disabled).toBe(true); + + paymentMethod2.click(); + expect(input2.disabled).toBe(false); + expect(select2.disabled).toBe(false); + + expect(input1.disabled).toBe(true); + expect(select1.disabled).toBe(true); + expect(input3.disabled).toBe(true); + expect(select3.disabled).toBe(true); + + paymentMethod3.click(); + expect(input3.disabled).toBe(false); + expect(select3.disabled).toBe(false); + + expect(input1.disabled).toBe(true); + expect(select1.disabled).toBe(true); + expect(input2.disabled).toBe(true); + expect(select2.disabled).toBe(true); + }); }); });