Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fire turbo:click event when submitting <form method="get"> #421

Closed

Conversation

seanpdoyle
Copy link
Contributor

Prior to this change, navigating a page via an <a> element and
navigating a page via a <form method="get"> element both resulted in
turbo:visit events.

However, clicking an <a> fired a turbo:click event, whereas
submitting a <form method="get"> by clicking its <input type="submit"> or <button> element did not fire the click for the
submitter.

This commit adds support for firing turbo:click events for a
<form method="get"> element's submitter.

Prior to this change, navigating a page via an `<a>` element and
navigating a page via a `<form method="get">` element both resulted in
`turbo:visit` events.

However, clicking an `<a>` fired a `turbo:click` event, whereas
submitting a `<form method="get">` by clicking its `<input
type="submit">` or `<button>` element _did not_ fire the click for the
submitter.

This commit adds support for firing `turbo:click` events for a
`<form method="get">` element's submitter.
@dhh
Copy link
Member

dhh commented Oct 14, 2021

I wouldn't expect a form submission, even if its a GET method, to trigger turbo:click, when other form submissions do not. What gave rise to wanting this?

@seanpdoyle
Copy link
Contributor Author

Form submissions with GET are a bit of an odd duck.

Clicking on an <a> fires:

  • turbo:click
  • turbo:before-visit
  • turbo:visit
  • turbo:before-cache
  • turbo:before-render
  • turbo:render
  • turbo:load

Submitting a <form method="post"> fires:

  • turbo:submit-start
  • turbo:before-fetch-request
  • turbo:before-fetch-response
  • turbo:submit-end
  • turbo:before-visit
  • turbo:visit
  • turbo:before-cache
  • turbo:before-render
  • turbo:render
  • turbo:load

Submitting a <form method="get"> fires:

  • turbo:before-visit
  • turbo:visit
  • turbo:before-cache
  • turbo:before-render
  • turbo:render
  • turbo:load

<a> navigations start with turbo:click, <form method="post"> submissions start with turbo:submit-start, and <form method="get"> start with turbo:before-visit.

This proposed change was motivated by a desire to have parity between clicking a <a> and submitting a <form method="get"> since they both initiate the visit->load sequence without firing form-specific events.

There are, of course, the native browser events that precede the turbo: events. <a>, <input type="submit">, and <button> events all fire click, and the <form> events all fire submit.

My worry is that the lack of consistency between navigational event sequences initiated by <a> and <form method="get"> could be surprising.

@dhh
Copy link
Member

dhh commented Oct 14, 2021

It is an odd duck, but making get-forms fire turbo:click doesn't quite make sense to me. I get that they under the good are actually more like links than forms, but that's not how a user would think of it. If anything, it should fire more of the same like post-forms. But I also think that maybe it's just fine as it is? It is a bit of a hybrid, so imo OK that it has a different event stack.

@t27duck
Copy link
Contributor

t27duck commented Oct 14, 2021

For consistency, I would think if a form is submitting (regardless if it's a get or post) the expectation would be any of the events related to a form submitting would be fired.

So if there were to be a change, I'd vote for submitting a <form method="get">, regardless if the initial trigger was a link, to have the same events as <form method="post">

seanpdoyle added a commit to seanpdoyle/turbo that referenced this pull request Oct 14, 2021
Closes hotwired#421
Closes hotwired#122

---

Fire the same sequence of events for  `<form method="get">` submissions
as for `<form method="post">` submissions.

The [FormSubmission.prepareHeadersForRequest][] already accounts for
`[method="get"]` forms by omitting the `Accept:` header with the custom
`turbo-stream` MIME type.

Test changes
---

The `eventLogs` mechanisms we have in place declared in
`src/tests/fixutres/test.js` cannot properly serialize `Element`
instances, so adding `turbo:submit-start` and `turbo:submit-end`
listeners to serialize events for our test suite isn't possible in the
current configuration. To that end, this commit adds assertions for
`<form>` submit event sequences for all events except those two. In
their place, the suite adds listeners to set `[data-form-submit-start]`
and `[data-form-submit-end]` to the `<html>` element when they fire.

[FormSubmission.prepareHeadersForRequest]: https://github.com/hotwired/turbo/blob/58d2261274533a80a2c5efda7da211b3f20efcbb/src/core/drive/form_submission.ts#L136-L144
@seanpdoyle
Copy link
Contributor Author

I've opened #424 as an alternative to this change. It changes <form method="get"> to fire the same events as other <form> submissions.

seanpdoyle added a commit to seanpdoyle/turbo that referenced this pull request Oct 14, 2021
Closes hotwired#421
Closes hotwired#122

---

Fire the same sequence of events for  `<form method="get">` submissions
as for `<form method="post">` submissions.

The [FormSubmission.prepareHeadersForRequest][] already accounts for
`[method="get"]` forms by omitting the `Accept:` header with the custom
`turbo-stream` MIME type.

Test changes
---

The `eventLogs` mechanisms we have in place declared in
`src/tests/fixutres/test.js` cannot properly serialize `Element`
instances, so adding `turbo:submit-start` and `turbo:submit-end`
listeners to serialize events for our test suite isn't possible in the
current configuration. To that end, this commit adds assertions for
`<form>` submit event sequences for all events except those two. In
their place, the suite adds listeners to set `[data-form-submit-start]`
and `[data-form-submit-end]` to the `<html>` element when they fire.

[FormSubmission.prepareHeadersForRequest]: https://github.com/hotwired/turbo/blob/58d2261274533a80a2c5efda7da211b3f20efcbb/src/core/drive/form_submission.ts#L136-L144
seanpdoyle added a commit to seanpdoyle/turbo that referenced this pull request Oct 14, 2021
Closes hotwired#421
Closes hotwired#122

---

Fire the same sequence of events for  `<form method="get">` submissions
as for `<form method="post">` submissions.

The [FormSubmission.prepareHeadersForRequest][] already accounts for
`[method="get"]` forms by omitting the `Accept:` header with the custom
`turbo-stream` MIME type.

Test changes
---

The `eventLogs` mechanisms we have in place declared in
`src/tests/fixutres/test.js` cannot properly serialize `Element`
instances, so adding `turbo:submit-start` and `turbo:submit-end`
listeners to serialize events for our test suite isn't possible in the
current configuration. To that end, this commit adds assertions for
`<form>` submit event sequences for all events except those two. In
their place, the suite adds listeners to set `[data-form-submit-start]`
and `[data-form-submit-end]` to the `<html>` element when they fire.

[FormSubmission.prepareHeadersForRequest]: https://github.com/hotwired/turbo/blob/58d2261274533a80a2c5efda7da211b3f20efcbb/src/core/drive/form_submission.ts#L136-L144
seanpdoyle added a commit to seanpdoyle/turbo that referenced this pull request Oct 14, 2021
Closes hotwired#421
Closes hotwired#122

---

Fire the same sequence of events for  `<form method="get">` submissions
as for `<form method="post">` submissions.

The [FormSubmission.prepareHeadersForRequest][] already accounts for
`[method="get"]` forms by omitting the `Accept:` header with the custom
`turbo-stream` MIME type.

Test changes
---

The `eventLogs` mechanisms we have in place declared in
`src/tests/fixutres/test.js` cannot properly serialize `Element`
instances, so adding `turbo:submit-start` and `turbo:submit-end`
listeners to serialize events for our test suite isn't possible in the
current configuration. To that end, this commit adds assertions for
`<form>` submit event sequences for all events except those two. In
their place, the suite adds listeners to set `[data-form-submit-start]`
and `[data-form-submit-end]` to the `<html>` element when they fire.

[FormSubmission.prepareHeadersForRequest]: https://github.com/hotwired/turbo/blob/58d2261274533a80a2c5efda7da211b3f20efcbb/src/core/drive/form_submission.ts#L136-L144
seanpdoyle added a commit to seanpdoyle/turbo that referenced this pull request Oct 14, 2021
Closes hotwired#421
Closes hotwired#122

---

Fire the same sequence of events for  `<form method="get">` submissions
as for `<form method="post">` submissions.

The [FormSubmission.prepareHeadersForRequest][] already accounts for
`[method="get"]` forms by omitting the `Accept:` header with the custom
`turbo-stream` MIME type.

Visit actions
---

Prior to this change, a `<form method="get">` would _always_ result in
two Visits: the first with `{ action: "advance" }`, then a second with
`{ action: "replace" }`.

Since the response to a `<form method="get">` has the potential to be a
[200 OK][] or a [201 Created][], this commit also modifies the `Visit`
class to skip the `followRedirect()` call when the request is idempotent
and the response is not a redirect.

Test changes
---

The `eventLogs` mechanisms we have in place declared in
`src/tests/fixutres/test.js` cannot properly serialize `Element`
instances, so adding `turbo:submit-start` and `turbo:submit-end`
listeners to serialize events for our test suite isn't possible in the
current configuration. To that end, this commit adds assertions for
`<form>` submit event sequences for all events except those two. In
their place, the suite adds listeners to set `[data-form-submit-start]`
and `[data-form-submit-end]` to the `<html>` element when they fire.

[FormSubmission.prepareHeadersForRequest]: https://github.com/hotwired/turbo/blob/58d2261274533a80a2c5efda7da211b3f20efcbb/src/core/drive/form_submission.ts#L136-L144
[200 OK]: https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/200
[200 Created]: https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/201
@seanpdoyle
Copy link
Contributor Author

The more I think about it, the more I think what's proposed in #424 is more viable.

I'm closing this in favor of #424.

@seanpdoyle seanpdoyle closed this Oct 14, 2021
dhh pushed a commit that referenced this pull request Oct 18, 2021
Closes #421
Closes #122

---

Fire the same sequence of events for  `<form method="get">` submissions
as for `<form method="post">` submissions.

The [FormSubmission.prepareHeadersForRequest][] already accounts for
`[method="get"]` forms by omitting the `Accept:` header with the custom
`turbo-stream` MIME type.

Visit actions
---

Prior to this change, a `<form method="get">` would _always_ result in
two Visits: the first with `{ action: "advance" }`, then a second with
`{ action: "replace" }`.

Since the response to a `<form method="get">` has the potential to be a
[200 OK][] or a [201 Created][], this commit also modifies the `Visit`
class to skip the `followRedirect()` call when the request is idempotent
and the response is not a redirect.

Test changes
---

The `eventLogs` mechanisms we have in place declared in
`src/tests/fixutres/test.js` cannot properly serialize `Element`
instances, so adding `turbo:submit-start` and `turbo:submit-end`
listeners to serialize events for our test suite isn't possible in the
current configuration. To that end, this commit adds assertions for
`<form>` submit event sequences for all events except those two. In
their place, the suite adds listeners to set `[data-form-submit-start]`
and `[data-form-submit-end]` to the `<html>` element when they fire.

[FormSubmission.prepareHeadersForRequest]: https://github.com/hotwired/turbo/blob/58d2261274533a80a2c5efda7da211b3f20efcbb/src/core/drive/form_submission.ts#L136-L144
[200 OK]: https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/200
[200 Created]: https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/201
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants