Skip to content

Commit

Permalink
Checkbox Example (Mixed-State): Toggle on keyup to prevent continuous…
Browse files Browse the repository at this point in the history
… toggling (pull #2722)

Modified JavaScript for Checkbox Example (Mixed-State) so that the state of the checkbox does not change until key up. This change prevents continuous toggling if the user holds down the space key.

This is the same change that was made for Checkbox Example (Two State) in pull request #2518.

This commit fully resolves issue #2425.
  • Loading branch information
sivakusayan committed Jun 26, 2023
1 parent 3952625 commit 049893e
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions content/patterns/checkbox/examples/js/checkbox-mixed.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class CheckboxMixed {
this.checkboxNodes = domNode.querySelectorAll('input[type="checkbox"]');

this.mixedNode.addEventListener('keydown', this.onMixedKeydown.bind(this));
this.mixedNode.addEventListener('keyup', this.onMixedKeyup.bind(this));
this.mixedNode.addEventListener('click', this.onMixedClick.bind(this));
this.mixedNode.addEventListener('focus', this.onMixedFocus.bind(this));
this.mixedNode.addEventListener('blur', this.onMixedBlur.bind(this));
Expand Down Expand Up @@ -116,23 +117,23 @@ class CheckboxMixed {

/* EVENT HANDLERS */

// Prevent page scrolling on space down
onMixedKeydown(event) {
var flag = false;
if (event.key === ' ') {
event.preventDefault();
}
}

onMixedKeyup(event) {
switch (event.key) {
case ' ':
this.toggleMixed();
flag = true;
event.stopPropagation();
break;

default:
break;
}

if (flag) {
event.stopPropagation();
event.preventDefault();
}
}

onMixedClick() {
Expand Down

0 comments on commit 049893e

Please sign in to comment.