fix(switch): clicking on label should toggle checked state

This commit is contained in:
Thomas Allmer 2020-07-30 16:28:55 +02:00 committed by Thomas Allmer
parent bfd9e99638
commit d94a83a92d
6 changed files with 25 additions and 2 deletions

View file

@ -0,0 +1,5 @@
---
'@lion/switch': patch
---
When clicking on the label the checked state should be toggled. This expected behavior comes from the native radio or checkbox.

View file

@ -2,7 +2,7 @@
module.exports = {
'*': ['eclint fix', 'git add'],
'*.js': ['eslint --fix', 'prettier --write', 'git add'],
'*.md': ['prettier --write', 'markdownlint', 'git add'],
'{*!(.changeset)/**/*.md,*.md}': ['prettier --write', 'markdownlint', 'git add'],
'yarn.lock': ['node ./scripts/yarn-lock-scan.js'],
'*package.json': absolutePaths => {
const sortPackages = [];

View file

@ -16,7 +16,7 @@
"lint": "run-p lint:*",
"lint:eclint": "git ls-files | xargs eclint check",
"lint:eslint": "eslint --ext .js,.html .",
"lint:markdownlint": "git ls-files '*.md' | xargs markdownlint --ignore '**/CHANGELOG.md'",
"lint:markdownlint": "git ls-files '*.md' | xargs markdownlint --ignore '{.changeset/*.md,**/CHANGELOG.md}'",
"lint:prettier": "prettier \"**/*.js\" --list-different || (echo '↑↑ these files are not prettier formatted ↑↑' && exit 1)",
"lint:types": "tsc",
"lint:versions": "node ./scripts/lint-versions.js",

View file

@ -81,6 +81,7 @@ export const ChoiceInputMixin = superclass =>
constructor() {
super();
this.modelValue = { value: '', checked: false };
this.__toggleChecked = this.__toggleChecked.bind(this);
}
/**

View file

@ -61,10 +61,19 @@ export class LionSwitch extends ScopedElementsMixin(ChoiceInputMixin(LionField))
'checked-changed',
this.__handleButtonSwitchCheckedChanged.bind(this),
);
if (this._labelNode) {
this._labelNode.addEventListener('click', this.__toggleChecked);
}
this._syncButtonSwitch();
this.submitted = true;
}
disconnectedCallback() {
if (this._labelNode) {
this._labelNode.removeEventListener('click', this.__toggleChecked);
}
}
updated(changedProperties) {
super.updated(changedProperties);
this._syncButtonSwitch();

View file

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