fix(switch): fire model-value-changed with isTriggeredByUser: true

This commit is contained in:
jorenbroekema 2021-05-12 11:54:59 +02:00
parent fce7f3b330
commit e297c2e1db
3 changed files with 30 additions and 0 deletions

View file

@ -0,0 +1,5 @@
---
'@lion/switch': patch
---
Fire model-value-changed event with isTriggeredByUser: true when clicking the switch-button.

View file

@ -122,7 +122,9 @@ export class LionSwitch extends ScopedElementsMixin(ChoiceInputMixin(LionField))
/** @private */
__handleButtonSwitchCheckedChanged() {
this._isHandlingUserInput = true;
this.checked = this._inputNode.checked;
this._isHandlingUserInput = false;
}
/** @protected */

View file

@ -69,6 +69,29 @@ describe('lion-switch', () => {
expect(document.activeElement).to.not.equal(_inputNode);
});
it('clicking the switch or the label should fire the model-value-changed event with isTriggeredByUser: true', async () => {
let isTriggeredByUser = false;
/** @param {CustomEvent} ev */
const modelValueChanged = ev => {
isTriggeredByUser = ev.detail.isTriggeredByUser;
};
const el = await fixture(
html`<lion-switch
@model-value-changed=${modelValueChanged}
label="Enable Setting"
></lion-switch>`,
);
const { _inputNode, _labelNode } = getSwitchMembers(el);
_inputNode.click();
expect(isTriggeredByUser).to.be.true;
isTriggeredByUser = false;
_labelNode.click();
expect(isTriggeredByUser).to.be.true;
});
it('should sync its "disabled" state to child button', async () => {
const el = await fixture(html`<lion-switch disabled></lion-switch>`);
const { _inputNode } = getSwitchMembers(el);