From d838ea100cb592403d3ca4cfae0b4752664fc2e7 Mon Sep 17 00:00:00 2001 From: Jose Luis de Vega Date: Wed, 14 Sep 2022 18:55:28 +0200 Subject: [PATCH] fix(switch): do not change checked state when disabled --- .changeset/gentle-socks-juggle.md | 5 +++++ packages/switch/src/LionSwitchButton.js | 2 +- packages/switch/test/lion-switch-button.test.js | 8 ++++++++ 3 files changed, 14 insertions(+), 1 deletion(-) create mode 100644 .changeset/gentle-socks-juggle.md diff --git a/.changeset/gentle-socks-juggle.md b/.changeset/gentle-socks-juggle.md new file mode 100644 index 000000000..00f0efb2b --- /dev/null +++ b/.changeset/gentle-socks-juggle.md @@ -0,0 +1,5 @@ +--- +'@lion/switch': patch +--- + +fix(LionSwitchButton): do not dispatch checked-change event when is disabled diff --git a/packages/switch/src/LionSwitchButton.js b/packages/switch/src/LionSwitchButton.js index 4ae1b7341..eea64c2b6 100644 --- a/packages/switch/src/LionSwitchButton.js +++ b/packages/switch/src/LionSwitchButton.js @@ -158,7 +158,7 @@ export class LionSwitchButton extends DisabledWithTabIndexMixin(LitElement) { */ requestUpdate(name, oldValue) { super.requestUpdate(name, oldValue); - if (this.isConnected && name === 'checked' && this.checked !== oldValue) { + if (this.isConnected && name === 'checked' && this.checked !== oldValue && !this.disabled) { this.__checkedStateChange(); } } diff --git a/packages/switch/test/lion-switch-button.test.js b/packages/switch/test/lion-switch-button.test.js index 33fe08cc5..e02666411 100644 --- a/packages/switch/test/lion-switch-button.test.js +++ b/packages/switch/test/lion-switch-button.test.js @@ -118,6 +118,14 @@ describe('lion-switch-button', () => { expect(handlerSpy.called).to.be.false; }); + it('should not dispatch "checked-changed" event if disabled on update', () => { + const handlerSpy = sinon.spy(); + el.disabled = true; + el.addEventListener('checked-changed', handlerSpy); + el.checked = !el.checked; + expect(handlerSpy.called).to.be.false; + }); + describe('a11y', () => { it('should manage "aria-checked"', async () => { expect(el.hasAttribute('aria-checked')).to.be.true;