-
Notifications
You must be signed in to change notification settings - Fork 435
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
Introduce FormLinkInterceptor
#631
Conversation
In an effort to match the patterns established by `LinkInterceptor` and `FormInterceptor`, this commit introduces a `FormLinkInterceptor` and `FormLinkInterceptorDelegate`. Behind the scenes, the `FormLinkInterceptor` relies on an instance of the `LinkInterceptor` to intervene in `<a>` element clicks when `[data-turbo-method]` or `[data-turbo-stream]` are present. When those clicks are detected, it creates a `<form hidden>` element, attaches it to the document, delegates to a `FormLinkInterceptorDelegate` to map the `<a>` element's attributes to the `<form>` element, submits the form through the polyfilled [HTMLFormElement.requestSubmit][] method, then removes the `<form>` from the document. The `Session` serves as a `FormLinkInterceptorDelegate`, making sure to start and stop the observer _before_ its `LinkInterceptor` instance, so that clicks that are intercepted by the `FormLinkInterceptor` are not also intercepted by the `LinkInterceptor`. [HTMLFormElement.requestSubmit]: https://developer.mozilla.org/en-US/docs/Web/API/HTMLFormElement/requestSubmit
e4c1a36
to
f52a47f
Compare
linkClickIntercepted(link: Element, action: string): void { | ||
const form = document.createElement("form") | ||
form.setAttribute("action", action) | ||
form.hidden = true |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Apologies if this edge case is already handled, I don't have a full understanding of all of the internals of Turbo.
But in the case that Turbo.session.drive = false
, but on this link data-turbo="true"
is specified, then wouldn't the form created by this also need to set data-turbo="true"
on the form element for it to be handled by Turbo?
I have a PR open to address this (#500) but the addition of this FormLinkInterceptor would change where that behavior should be implemented.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for raising these concerns! I've pushed up d317319 to add test coverage for the cases you've outlined.
d317319
to
b275a25
Compare
b275a25
to
2777608
Compare
Nice cleanup! |
In an effort to match the patterns established by
LinkInterceptor
and
FormInterceptor
, this commit introduces aFormLinkInterceptor
andFormLinkInterceptorDelegate
.Behind the scenes, the
FormLinkInterceptor
relies on an instance ofthe
LinkInterceptor
to intervene in<a>
element clicks when[data-turbo-method]
or[data-turbo-stream]
are present. When thoseclicks are detected, it creates a
<form hidden>
element, attaches itto the document, delegates to a
FormLinkInterceptorDelegate
to mapthe
<a>
element's attributes to the<form>
element, submits the formthrough the polyfilled HTMLFormElement.requestSubmit method, then
removes the
<form>
from the document.The
Session
serves as aFormLinkInterceptorDelegate
, making sureto start and stop the observer before its
LinkInterceptor
instance, so that clicks that are intercepted by the
FormLinkInterceptor
are not also intercepted by theLinkInterceptor
.