fix(form-core): prevent toggle of checked state when disabled

This commit is contained in:
qa46hx 2020-09-24 13:46:32 +02:00 committed by Thomas Allmer
parent 6aacbc4403
commit 857206548c
4 changed files with 32 additions and 0 deletions

View file

@ -0,0 +1,7 @@
---
'@lion/form-core': patch
'@lion/switch': patch
---
- prevent toggle of checked state when disabled
- dispatch checked-changed on label click

View file

@ -31,6 +31,13 @@ const ChoiceInputMixinImplementation = superclass =>
type: Boolean, type: Boolean,
reflect: true, reflect: true,
}, },
/**
* Boolean indicating whether or not this element is disabled.
*/
disabled: {
type: Boolean,
reflect: true,
},
/** /**
* Whereas 'normal' `.modelValue`s usually store a complex/typed version * Whereas 'normal' `.modelValue`s usually store a complex/typed version
* of a view value, choice inputs have a slightly different approach. * of a view value, choice inputs have a slightly different approach.
@ -108,6 +115,7 @@ const ChoiceInputMixinImplementation = superclass =>
constructor() { constructor() {
super(); super();
this.modelValue = { value: '', checked: false }; this.modelValue = { value: '', checked: false };
this.disabled = false;
this.__toggleChecked = this.__toggleChecked.bind(this); this.__toggleChecked = this.__toggleChecked.bind(this);
} }
@ -170,6 +178,9 @@ const ChoiceInputMixinImplementation = superclass =>
} }
__toggleChecked() { __toggleChecked() {
if (this.disabled) {
return;
}
this.checked = !this.checked; this.checked = !this.checked;
} }

View file

@ -131,6 +131,14 @@ describe('ChoiceInputMixin', () => {
expect(el.checked).to.be.false; expect(el.checked).to.be.false;
}); });
it('can not toggle the checked state when disabled via user interaction', async () => {
const el = /** @type {ChoiceClass} */ (await fixture(
html`<choice-input disabled></choice-input>`,
));
el._inputNode.dispatchEvent(new CustomEvent('change', { bubbles: true }));
expect(el.checked).to.be.false;
});
it('synchronizes modelValue to checked state and vice versa', async () => { it('synchronizes modelValue to checked state and vice versa', async () => {
const el = /** @type {ChoiceClass} */ (await fixture( const el = /** @type {ChoiceClass} */ (await fixture(
html`<choice-input .choiceValue=${'foo'}></choice-input>`, html`<choice-input .choiceValue=${'foo'}></choice-input>`,

View file

@ -16,6 +16,12 @@ describe('lion-switch', () => {
expect(el.checked).to.be.false; expect(el.checked).to.be.false;
}); });
it('clicking the label should not toggle the checked state when disabled', async () => {
const el = await fixture(html`<lion-switch disabled label="Enable Setting"></lion-switch>`);
el._labelNode.click();
expect(el.checked).to.be.false;
});
it('should sync its "disabled" state to child button', async () => { it('should sync its "disabled" state to child button', async () => {
const el = await fixture(html`<lion-switch disabled></lion-switch>`); const el = await fixture(html`<lion-switch disabled></lion-switch>`);
expect(el._inputNode.disabled).to.be.true; expect(el._inputNode.disabled).to.be.true;