Skip to content

Commit

Permalink
Set aria-busy on the form element during a form submission
Browse files Browse the repository at this point in the history
This is useful for styling the form while it is submitting.

Before this change, we were only setting aria-busy on frames while they
were loading and on the html element while Turbo was processing a visit.
  • Loading branch information
afcapel committed Dec 18, 2023
1 parent ac00359 commit 6788194
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/core/drive/form_submission.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { FetchRequest, FetchMethod, fetchMethodFromString, fetchEnctypeFromString, isSafe } from "../../http/fetch_request"
import { expandURL } from "../url"
import { dispatch, getAttribute, getMetaContent, hasAttribute } from "../../util"
import { clearBusyState, dispatch, getAttribute, getMetaContent, hasAttribute, markAsBusy } from "../../util"
import { StreamMessage } from "../streams/stream_message"

export const FormSubmissionState = {
Expand Down Expand Up @@ -117,6 +117,7 @@ export class FormSubmission {
this.state = FormSubmissionState.waiting
this.submitter?.setAttribute("disabled", "")
this.setSubmitsWith()
markAsBusy(this.formElement)
dispatch("turbo:submit-start", {
target: this.formElement,
detail: { formSubmission: this }
Expand Down Expand Up @@ -155,6 +156,7 @@ export class FormSubmission {
this.state = FormSubmissionState.stopped
this.submitter?.removeAttribute("disabled")
this.resetSubmitterText()
clearBusyState(this.formElement)
dispatch("turbo:submit-end", {
target: this.formElement,
detail: { formSubmission: this, ...this.result }
Expand Down
2 changes: 1 addition & 1 deletion src/tests/fixtures/form.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<body>
<h1>Form</h1>
<div id="standard">
<form action="/__turbo/redirect" method="post" class="redirect">
<form id="standard-form" action="/__turbo/redirect" method="post" class="redirect">
<input type="hidden" name="path" value="/src/tests/fixtures/form.html">
<input type="hidden" name="greeting" value="Hello from a redirect">
<input id="standard-post-form-submit" type="submit" value="form[method=post]">
Expand Down
19 changes: 19 additions & 0 deletions src/tests/functional/form_submission_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,25 @@ test("standard POST form submission with redirect response", async ({ page }) =>
)
})


test("sets aria-busy on the form element during a form submission", async ({ page }) => {
await page.click("#standard form.redirect input[type=submit]")

await nextEventNamed(page, "turbo:submit-start")
assert.equal(
await nextAttributeMutationNamed(page, "standard-form", "aria-busy"),
"true",
"sets [aria-busy] on the form element"
)

await nextEventNamed(page, "turbo:submit-end")
assert.equal(
await nextAttributeMutationNamed(page, "standard-form", "aria-busy"),
null,
"removes [aria-busy] from the form element"
)
})

test("standard POST form submission events", async ({ page }) => {
await page.click("#standard-post-form-submit")

Expand Down

0 comments on commit 6788194

Please sign in to comment.