-
-
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 (#757)
- Loading branch information
Showing
6 changed files
with
174 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 |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import { | ||
afterEach, | ||
beforeAll, | ||
beforeEach, | ||
describe, | ||
expect, | ||
test, | ||
} from "vitest"; | ||
import { initAutosubmit } from "./autosubmit"; | ||
|
||
describe("autosubmit", () => { | ||
beforeAll(() => { | ||
initAutosubmit(); | ||
}); | ||
|
||
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("auto-submits text input", async () => { | ||
document.body.innerHTML = ` | ||
<form action="#"> | ||
<input type="text" data-auto-submit="submitter" /> | ||
<button type="submit" id="submitter">Submit</button> | ||
</form> | ||
`; | ||
|
||
let submitter; | ||
|
||
document.querySelector("form").addEventListener("submit", function (event) { | ||
submitter = event.submitter; | ||
}); | ||
|
||
const inputElement = document.querySelector("input"); | ||
inputElement.value = "awesome text"; | ||
inputElement.dispatchEvent(new InputEvent("input", { bubbles: true })); | ||
|
||
await wait(); | ||
|
||
expect(submitter).toBe(document.querySelector("button")); | ||
}); | ||
|
||
test("auto-submits text input with custom delay", async () => { | ||
document.body.innerHTML = ` | ||
<form action="#"> | ||
<input type="text" data-auto-submit="submitter" data-auto-submit-delay="100" /> | ||
<button type="submit" id="submitter">Submit</button> | ||
</form> | ||
`; | ||
|
||
let submitter; | ||
|
||
document.querySelector("form").addEventListener("submit", function (event) { | ||
submitter = event.submitter; | ||
}); | ||
|
||
const inputElement = document.querySelector("input"); | ||
inputElement.value = "awesome text"; | ||
inputElement.dispatchEvent(new InputEvent("input", { bubbles: true })); | ||
|
||
await wait(); | ||
expect(submitter).toBeUndefined(); | ||
|
||
await wait(100); | ||
expect(submitter).toBe(document.querySelector("button")); | ||
}); | ||
}); | ||
|
||
function wait(delay = 0) { | ||
return new Promise((resolve) => setTimeout(resolve, delay)); | ||
} |
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. | ||
} | ||
}); | ||
} |