Skip to content

Commit fe35ad2

Browse files
committed
[form] Reset form value in default reset handler
1 parent 13a1beb commit fe35ad2

File tree

4 files changed

+64
-40
lines changed

4 files changed

+64
-40
lines changed

.changeset/great-cycles-mate.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@sjsf/form": minor
3+
---
4+
5+
Reset form value in default reset handler

apps/docs/src/content/docs/api-reference/handlers.mdx

Lines changed: 43 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -6,43 +6,49 @@ sidebar:
66

77
```typescript
88
interface FormProps<T> {
9-
/**
10-
* Will be called when the form is submitted if `onSubmit` or `onSubmitError` is not provided
11-
*/
12-
onsubmit?: EventHandler<SubmitEvent, HTMLFormElement> | undefined | null;
9+
/**
10+
* Will be called when the form is submitted if `onSubmit` or `onSubmitError` is not provided
11+
*/
12+
onsubmit?: EventHandler<SubmitEvent, HTMLFormElement> | undefined | null;
1313

14-
/**
15-
* The function to get the form data snapshot
16-
*
17-
* The snapshot is used to validate the form and passed to
18-
* `onSubmit` and `onSubmitError` handlers.
19-
*
20-
* @default () => $state.snapshot(formValue)
21-
*/
22-
getSnapshot?: () => SchemaValue | undefined
23-
/**
24-
* Submit handler
25-
*
26-
* Will be called when the form is submitted and form data
27-
* snapshot is valid
28-
*/
29-
onSubmit?: (value: T | undefined, e: SubmitEvent) => void
30-
/**
31-
* Submit error handler
32-
*
33-
* Will be called when the form is submitted and form data
34-
* snapshot is not valid
35-
*/
36-
onSubmitError?: (errors: Errors<E>, e: SubmitEvent, snapshot: SchemaValue | undefined) => void
37-
/**
38-
* Reset handler
39-
*
40-
* Will be called on form reset.
41-
*
42-
* By default it will clear the errors and set `isSubmitted` state to `false`.
43-
*
44-
* @default () => { isSubmitted = false; errors.clear() }
45-
*/
46-
onReset?: (e: Event) => void
14+
/**
15+
* The function to get the form data snapshot
16+
*
17+
* The snapshot is used to validate the form and passed to
18+
* `onSubmit` and `onSubmitError` handlers.
19+
*
20+
* @default () => $state.snapshot(formValue)
21+
*/
22+
getSnapshot?: () => SchemaValue | undefined
23+
/**
24+
* Submit handler
25+
*
26+
* Will be called when the form is submitted and form data
27+
* snapshot is valid
28+
*/
29+
onSubmit?: (value: T | undefined, e: SubmitEvent) => void
30+
/**
31+
* Submit error handler
32+
*
33+
* Will be called when the form is submitted and form data
34+
* snapshot is not valid
35+
*/
36+
onSubmitError?: (errors: Errors<E>, e: SubmitEvent, snapshot: SchemaValue | undefined) => void
37+
/**
38+
* Reset handler
39+
*
40+
* Will be called on form reset.
41+
*
42+
* By default it will clear the errors, set `isSubmitted` state to `false` and
43+
* reset the form `value` to the `initialValue`.
44+
*
45+
* @default (e) => {
46+
* e.preventDefault();
47+
* isSubmitted = false;
48+
* errors.clear();
49+
* value = initialValue;
50+
* }
51+
*/
52+
onReset?: (e: Event) => void
4753
}
4854
```

apps/docs/src/content/docs/guides/_bind-form.svelte

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
55
66
const schema: Schema = {
77
type: "string",
8+
minLength: 10,
89
};
910
1011
const form = useCustomForm({
1112
schema,
13+
initialValue: "initial",
1214
onSubmit: (v) => window.alert(v),
1315
});
1416

packages/form/src/form/use-form.svelte.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,15 @@ export interface UseFormOptions<T, E> {
7272
*
7373
* Will be called on form reset.
7474
*
75-
* By default it will clear the errors and set `isSubmitted` state to `false`.
75+
* By default it will clear the errors, set `isSubmitted` state to `false` and
76+
* reset the form `value` to the `initialValue`.
7677
*
77-
* @default () => { isSubmitted = false; errors.clear() }
78+
* @default (e) => {
79+
* e.preventDefault();
80+
* isSubmitted = false;
81+
* errors.clear();
82+
* value = initialValue;
83+
* }
7884
*/
7985
onReset?: (e: Event) => void;
8086
schedulerYield?: SchedulerYield;
@@ -133,9 +139,14 @@ export function createForm<T, E>(
133139

134140
const resetHandler = $derived(
135141
options.onReset ??
136-
(() => {
142+
((e: Event) => {
143+
e.preventDefault();
137144
isSubmitted = false;
138145
errors.clear();
146+
value = merger.mergeFormDataAndSchemaDefaults(
147+
options.initialValue as Value,
148+
options.schema
149+
);
139150
})
140151
);
141152

0 commit comments

Comments
 (0)