Skip to content

Commit

Permalink
fix spacebar firing change event twice
Browse files Browse the repository at this point in the history
  • Loading branch information
Tropix126 committed Apr 6, 2021
1 parent e22deef commit 90f3d8f
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
5 changes: 3 additions & 2 deletions src/renderer/common/Checkbox.svelte
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<script>
import {handleKeyboardToggle} from "../stores/controls.js";
// NOTES: preventing the default click event behavior is needed to stop the change event being fired twice when the spacebar is pressed.
import {handleKeyboardToggle, checkItem} from "../stores/controls.js";
export let label;
export let checked = false;
Expand All @@ -8,7 +9,7 @@
let checkbox;
</script>

<div class="checkbox" on:keypress={handleKeyboardToggle(checkbox)}>
<div class="checkbox" on:keypress={handleKeyboardToggle(checkbox)} on:click={checkItem(checkbox)}>
<label>
<input type="checkbox" {disabled} bind:this={checkbox} bind:checked on:change>
{#if label}
Expand Down
4 changes: 2 additions & 2 deletions src/renderer/stores/controls.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ export const radioSelectedIndex = writable(0);

let i = 0;

function checkItem(item) {
export function checkItem(item) {
item.checked = !item.checked;
const changeEvent = new Event("change");
item.dispatchEvent(changeEvent);
}

export const handleKeyboardToggle = (checkbox) => {
if ((event.key === "Enter" || event.key === " ") && checkbox.disabled != true) {
if ((event.key === "Enter" || event.key === " ") && !checkbox.disabled) {
checkItem(checkbox);
}
};
Expand Down

0 comments on commit 90f3d8f

Please sign in to comment.