Skip to content

Commit

Permalink
Prevent toggling on links click inside a label (#93)
Browse files Browse the repository at this point in the history
* Prevent toggling on links click inside a label

* Make __interactionsAllowed() private
  • Loading branch information
Limon Monte authored May 28, 2018
1 parent 8f7667c commit b5e003d
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 6 deletions.
2 changes: 1 addition & 1 deletion demo/checkbox-basic-demos.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<h3>Basic Checkbox</h3>
<vaadin-demo-snippet id="checkbox-basic-demos-default-checkbox">
<template preserve-content>
<vaadin-checkbox checked>Option label</vaadin-checkbox>
<vaadin-checkbox checked>I agree with <a href>Terms & Conditions</a></vaadin-checkbox>
</template>
</vaadin-demo-snippet>

Expand Down
25 changes: 21 additions & 4 deletions src/vaadin-checkbox.html
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@
_addActiveListeners() {
// DOWN
this._addEventListenerToNode(this, 'down', (e) => {
if (!this.disabled) {
if (this.__interactionsAllowed(e)) {
this.setAttribute('active', '');
}
});
Expand All @@ -217,15 +217,15 @@

// KEYDOWN
this.addEventListener('keydown', e => {
if (!this.disabled && e.keyCode === 32) {
if (this.__interactionsAllowed(e) && e.keyCode === 32) {
e.preventDefault();
this.setAttribute('active', '');
}
});

// KEYUP
this.addEventListener('keyup', e => {
if (!this.disabled && e.keyCode === 32) {
if (this.__interactionsAllowed(e) && e.keyCode === 32) {
e.preventDefault();
this._toggleChecked();
this.removeAttribute('active');
Expand All @@ -241,8 +241,25 @@
return this.shadowRoot.querySelector('label');
}

/**
* True if users' interactions (mouse or keyboard)
* should toggle the checkbox
*/
__interactionsAllowed(e) {
if (this.disabled) {
return false;
}

// https://github.com/vaadin/vaadin-checkbox/issues/63
if (e.target.localName === 'a') {
return false;
}

return true;
}

_handleClick(e) {
if (!this.disabled) {
if (this.__interactionsAllowed(e)) {
if (!this.indeterminate) {
if (e.composedPath()[0] !== this._nativeCheckbox) {
e.preventDefault();
Expand Down
9 changes: 8 additions & 1 deletion test/vaadin-checkbox_test.html
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@

<test-fixture id="default">
<template>
<vaadin-checkbox name="test-checkbox">Vaadin <i>Checkbox</i></vaadin-checkbox>
<vaadin-checkbox name="test-checkbox">Vaadin <i>Checkbox</i> with <a href="#">Terms &amp; Conditions</a></vaadin-checkbox>
</template>
</test-fixture>

Expand Down Expand Up @@ -109,6 +109,13 @@
expect(vaadinCheckbox.checked).to.be.false;
});

it('should not toggle on link inside host click', () => {
const link = Polymer.dom(label).getEffectiveChildNodes()[4];
expect(link.outerHTML).to.be.equal('<a href="#">Terms &amp; Conditions</a>');
link.click();
expect(vaadinCheckbox.checked).to.be.false;
});

it('should bind checked to the native checkbox and vice versa', () => {
vaadinCheckbox.checked = true;
expect(nativeCheckbox.checked).to.be.eql(true);
Expand Down

0 comments on commit b5e003d

Please sign in to comment.