Skip to content

Commit

Permalink
Checkbox Example (Two State): Activate checkbox only on keyup (pull #…
Browse files Browse the repository at this point in the history
…2518)

Fix issue #2425 by changing checkbox key event processing to wait until key up to activate the checkbox.
This prevents the checkbox from toggling continuously if someone holds down the Space key too long.
  • Loading branch information
sivakusayan authored Nov 10, 2022
1 parent 74a04e9 commit d7db313
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion examples/checkbox/js/checkbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class Checkbox {
}

this.domNode.addEventListener('keydown', this.onKeydown.bind(this));
this.domNode.addEventListener('keyup', this.onKeyup.bind(this));
this.domNode.addEventListener('click', this.onClick.bind(this));
}

Expand All @@ -32,7 +33,14 @@ class Checkbox {

/* EVENT HANDLERS */

// Make sure to prevent page scrolling on space down
onKeydown(event) {
if (event.key === ' ') {
event.preventDefault();
}
}

onKeyup(event) {
var flag = false;

switch (event.key) {
Expand All @@ -47,7 +55,6 @@ class Checkbox {

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

Expand Down

0 comments on commit d7db313

Please sign in to comment.