Skip to content

Commit

Permalink
prevent hitting enter to submit a form
Browse files Browse the repository at this point in the history
  • Loading branch information
bohnacker committed Sep 6, 2021
1 parent f0c2e4a commit 718b910
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 3 deletions.
5 changes: 5 additions & 0 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -712,6 +712,11 @@
}

function keydownHandler(ev) {
// prevent submitting if the number spinner is inside a form element
if (ev.key == "Enter") {
ev.preventDefault();
}

if (ev.target == dragElement || ev.target == editElement) {
dispatch("consoleLog", ev.type);
} // console.log(ev);
Expand Down
5 changes: 5 additions & 0 deletions dist/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -706,6 +706,11 @@ function instance($$self, $$props, $$invalidate) {
}

function keydownHandler(ev) {
// prevent submitting if the number spinner is inside a form element
if (ev.key == "Enter") {
ev.preventDefault();
}

if (ev.target == dragElement || ev.target == editElement) {
dispatch("consoleLog", ev.type);
} // console.log(ev);
Expand Down
2 changes: 1 addition & 1 deletion example/public/build/bundle.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion example/public/build/bundle.js.map

Large diffs are not rendered by default.

16 changes: 16 additions & 0 deletions example/src/App.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
let value9 = 100;
let value10 = 12 * 60;
let value11 = 1;
let value12 = 100;
// Callback functions for example 9 ---------------------
function addDollar(val) {
Expand Down Expand Up @@ -233,6 +234,21 @@

<hr />

<div class="row">
<div class="explanation">
Hitting enter should not submit the form.<br />Current value is {value12}
</div>
<div class="right">
<form>
<div>
<NumberSpinner bind:value={value12} />
</div>
<div>
<button on:click|preventDefault={() => alert('Form was submitted')}>Submit</button>
</div>
</form>
</div>
</div>

</main>

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"src",
"dist"
],
"version": "0.7.7",
"version": "0.7.8",
"repository": {
"type": "git",
"url": "git+https://github.com/bohnacker/svelte-number-spinner.git"
Expand Down
5 changes: 5 additions & 0 deletions src/NumberSpinner.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,11 @@
}
function keydownHandler(ev) {
// prevent submitting if the number spinner is inside a form element
if (ev.key == "Enter") {
ev.preventDefault();
}
if (ev.target == dragElement || ev.target == editElement) {
dispatch("consoleLog", ev.type);
// console.log(ev);
Expand Down

0 comments on commit 718b910

Please sign in to comment.