Skip to content

Commit 35a069e

Browse files
committed
Added function to resetForm.
1 parent 419be7f commit 35a069e

File tree

3 files changed

+21
-4
lines changed

3 files changed

+21
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1919

2020
- Added `validate` to `superForm`, which can be used to validate any field, at any time.
2121
- The option `{ taint: boolean }` has been added to `form.set` and `form.update`.
22+
- The `resetForm` option can now take an (async) `() => boolean` function to determine whether the form should be resetted or not.
2223

2324
## [0.7.1] - 2023-04-17
2425

src/lib/client/index.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ export type FormOptions<T extends ZodValidation<AnyZodObject>, M> = Partial<{
8282
id: string;
8383
applyAction: boolean;
8484
invalidateAll: boolean;
85-
resetForm: boolean;
85+
resetForm: boolean | (() => MaybePromise<boolean>);
8686
scrollToError: 'auto' | 'smooth' | 'off';
8787
autoFocusOnError: boolean | 'detect';
8888
errorSelector: string;
@@ -531,7 +531,11 @@ export function superForm<
531531
return;
532532
}
533533

534-
if (form.valid && options.resetForm) {
534+
if (
535+
form.valid &&
536+
options.resetForm &&
537+
(options.resetForm === true || (await options.resetForm()))
538+
) {
535539
_resetForm(form.message);
536540
} else {
537541
rebind(form, untaint);
@@ -562,7 +566,12 @@ export function superForm<
562566
// All we need to do if redirected is to reset the form.
563567
// No events should be triggered because technically we're somewhere else.
564568
if (result.type == 'redirect') {
565-
if (options.resetForm) _resetForm();
569+
if (
570+
options.resetForm &&
571+
(options.resetForm === true || (await options.resetForm()))
572+
) {
573+
_resetForm();
574+
}
566575
return;
567576
}
568577

src/routes/reset/+page.svelte

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,20 @@
33
import type { PageData } from './$types';
44
import SuperDebug from '$lib/client/SuperDebug.svelte';
55
import type { schema } from './schemas';
6+
import { page } from '$app/stores';
67
78
export let data: PageData;
89
910
let resets = 0;
1011
1112
const superF: SuperForm<typeof schema> = superForm(data.form, {
12-
resetForm: true,
13+
resetForm: $page.url.searchParams.has('function')
14+
? async () => {
15+
console.log('Reset...');
16+
await new Promise((resolve) => setTimeout(resolve, 300));
17+
return true;
18+
}
19+
: true,
1320
onUpdated({ form }) {
1421
if (form.valid) resets = resets + 1;
1522
}

0 commit comments

Comments
 (0)