Skip to content

Commit 87b3037

Browse files
dummdidummgtm-nayanRich-Harris
authored
[feat] add reset option to update method of enhance (#7326)
It's not uncommon to want the form values be preserved, so it makes sense to have this configurable Co-authored-by: gtmnayan <[email protected]> Co-authored-by: Rich Harris <[email protected]>
1 parent d081ba5 commit 87b3037

File tree

5 files changed

+22
-13
lines changed

5 files changed

+22
-13
lines changed

.changeset/nervous-students-march.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'create-svelte': patch
3+
'@sveltejs/kit': patch
4+
---
5+
6+
[feat] add reset option to update method of enhance

documentation/docs/06-form-actions.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ The easiest way to progressively enhance a form is to add the `use:enhance` acti
250250
Without an argument, `use:enhance` will emulate the browser-native behaviour, just without the full-page reloads. It will:
251251

252252
- update the `form` property, `$page.form` and `$page.status` on a successful or invalid response, but only if the action is on the same page you're submitting from. So for example if your form looks like `<form action="/somewhere/else" ..>`, `form` and `$page` will _not_ be updated. This is because in the native form submission case you would be redirected to the page the action is on.
253-
- invalidate all data using `invalidateAll` on a successful response
253+
- reset the `<form>` element and invalidate all data using `invalidateAll` on a successful response
254254
- call `goto` on a redirect response
255255
- render the nearest `+error` boundary if an error occurs
256256

packages/create-svelte/templates/default/src/routes/sverdle/+page.svelte

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
<script lang="ts">
22
import { confetti } from '@neoconfetti/svelte';
3-
import { applyAction, enhance } from '$app/forms';
3+
import { enhance } from '$app/forms';
44
import type { PageData, ActionData } from './$types';
5-
import { invalidateAll } from '$app/navigation';
65
76
/** @type {import('./$types').PageData} */
87
export let data: PageData;
@@ -84,11 +83,8 @@
8483
action="?/enter"
8584
use:enhance={() => {
8685
// prevent default callback from resetting the form
87-
return async ({ result }) => {
88-
if (result.type === 'success') {
89-
await invalidateAll();
90-
}
91-
await applyAction(result);
86+
return ({ update }) => {
87+
update({ reset: false });
9288
};
9389
}}
9490
>

packages/kit/src/runtime/app/forms.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,14 @@ export function enhance(form, submit = () => {}) {
2121
* @param {{
2222
* action: URL;
2323
* result: import('types').ActionResult;
24+
* reset?: boolean
2425
* }} opts
2526
*/
26-
const fallback_callback = async ({ action, result }) => {
27+
const fallback_callback = async ({ action, result, reset }) => {
2728
if (result.type === 'success') {
28-
form.reset();
29+
if (reset !== false) {
30+
form.reset();
31+
}
2932
await invalidateAll();
3033
}
3134

@@ -97,7 +100,7 @@ export function enhance(form, submit = () => {}) {
97100
action,
98101
data,
99102
form,
100-
update: () => fallback_callback({ action, result }),
103+
update: (opts) => fallback_callback({ action, result, reset: opts?.reset }),
101104
// @ts-expect-error generic constraints stuff we don't care about
102105
result,
103106
// TODO remove for 1.0

packages/kit/types/ambient.d.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,11 @@ declare module '$app/forms' {
110110
form: HTMLFormElement;
111111
action: URL;
112112
result: ActionResult<Success, Invalid>;
113-
update: () => Promise<void>;
113+
/**
114+
* Call this to get the default behavior of a form submission response.
115+
* @param options Set `reset: false` if you don't want the `<form>` values to be reset after a successful submission.
116+
*/
117+
update: (options?: { reset: boolean }) => Promise<void>;
114118
}) => void);
115119

116120
/**
@@ -133,7 +137,7 @@ declare module '$app/forms' {
133137
* If this function or its return value isn't set, it
134138
* - falls back to updating the `form` prop with the returned data if the action is one same page as the form
135139
* - updates `$page.status`
136-
* - invalidates all data in case of successful submission with no redirect response
140+
* - resets the `<form>` element and invalidates all data in case of successful submission with no redirect response
137141
* - redirects in case of a redirect response
138142
* - redirects to the nearest error page in case of an unexpected error
139143
*

0 commit comments

Comments
 (0)