-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
prevent double click form submit with javascript
- Loading branch information
Showing
4 changed files
with
84 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from "./autosubmit"; | ||
export * from "./prevent-double-click-submit"; |
67 changes: 67 additions & 0 deletions
67
src/main/javascript/components/form/prevent-double-click-submit.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { | ||
afterEach, | ||
beforeAll, | ||
beforeEach, | ||
describe, | ||
expect, | ||
test, | ||
} from "vitest"; | ||
import { initPreventDoubleClickSubmit } from "./prevent-double-click-submit"; | ||
|
||
describe("DoubleClickSubmitGuard", () => { | ||
beforeAll(() => { | ||
initPreventDoubleClickSubmit(); | ||
}); | ||
|
||
beforeEach(() => { | ||
// prevent HTMLFormElement.prototype.requestSubmit is not implemented log. | ||
// | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-expect-error | ||
window._virtualConsole.emit = () => {}; | ||
}); | ||
|
||
afterEach(() => { | ||
while (document.body.firstChild) { | ||
document.body.firstChild.remove(); | ||
} | ||
}); | ||
|
||
test("disables submitter on form submit", () => { | ||
document.body.innerHTML = ` | ||
<form action="#"> | ||
<button type="submit">Submit</button> | ||
</form> | ||
`; | ||
|
||
const button = document.querySelector("button"); | ||
|
||
expect(button.getAttribute("disabled")).toBeNull(); | ||
|
||
button.click(); | ||
|
||
expect(button.getAttribute("disabled")).toBe(""); | ||
}); | ||
|
||
test("does not disable submitter on form submit when defaultPrevented", () => { | ||
document.body.innerHTML = ` | ||
<form action="#"> | ||
<button type="submit">Submit</button> | ||
</form> | ||
`; | ||
|
||
let called = false; | ||
|
||
document.querySelector("form").addEventListener("submit", function (event) { | ||
event.preventDefault(); | ||
called = true; | ||
}); | ||
|
||
const button = document.querySelector("button"); | ||
|
||
button.click(); | ||
|
||
expect(button.getAttribute("disabled")).toBeNull(); | ||
expect(called).toBe(true); | ||
}); | ||
}); |
13 changes: 13 additions & 0 deletions
13
src/main/javascript/components/form/prevent-double-click-submit.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
/** | ||
* Adds a global `submit` listener and disables the submitter. | ||
*/ | ||
export function initPreventDoubleClickSubmit() { | ||
window.addEventListener("submit", function (event) { | ||
if (!event.defaultPrevented) { | ||
event.submitter?.setAttribute("disabled", ""); | ||
// full page reload renders the form again with enabled submitter. | ||
// maybe @hotwired/turbo is enabled somewhere. in this case, however, turbo | ||
// handles the disabled attribute of the submitter. | ||
} | ||
}); | ||
} |