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

feat: allow to opt-out of use:enhance for forms #13250

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/spotty-queens-beam.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': minor
---

Allow to do opt out of enhancing forms with `data-sveltekit-reload` on the form element or button
fehnomenal marked this conversation as resolved.
Show resolved Hide resolved
2 changes: 2 additions & 0 deletions documentation/docs/20-core-concepts/30-form-actions.md
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,8 @@ Without an argument, `use:enhance` will emulate the browser-native behaviour, ju
- render the nearest `+error` boundary if an error occurs
- [reset focus](accessibility#Focus-management) to the appropriate element

You can add the [`data-sveltekit-reload`](link-options#data-sveltekit-reload) attribute on the `<form>` or the `<button>` to opt-out of this behavior and instead let the browser handle the form submission.
fehnomenal marked this conversation as resolved.
Show resolved Hide resolved

### Customising use:enhance

To customise the behaviour, you can provide a `SubmitFunction` that runs immediately before the form is submitted, and (optionally) returns a callback that runs with the `ActionResult`. Note that if you return a callback, the default behavior mentioned above is not triggered. To get it back, call `update`.
Expand Down
5 changes: 5 additions & 0 deletions packages/kit/src/runtime/app/forms.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,11 @@ export function enhance(form_element, submit = () => {}) {
: clone(form_element).method;
if (method !== 'post') return;

const skip =
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we can reuse the get_router_options function to get the reload option state

export function get_router_options(element) {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also thought about reusing it but decided against it as I think it could result in surprising and hard to debug code if the parents of two elements (the form and the button) are inspected for the reload option.

Instead I reused the underlying functions and logic.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have to consider keeping the behaviour consistent since link options are known to apply to their children:

You can customise the behaviour of links with data-sveltekit-* attributes. These can be applied to the itself, or to a parent element.

These options also apply to <form> elements with method="GET".

https://svelte.dev/docs/kit/link-options

event.submitter?.getAttribute('data-sveltekit-reload') ??
form_element.getAttribute('data-sveltekit-reload');
if (skip === 'true') return;

event.preventDefault();

const action = new URL(
Expand Down
Loading