Skip to content

Commit

Permalink
[form] Reset form value in default reset handler
Browse files Browse the repository at this point in the history
  • Loading branch information
x0k committed Oct 30, 2024
1 parent 13a1beb commit fe35ad2
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 40 deletions.
5 changes: 5 additions & 0 deletions .changeset/great-cycles-mate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@sjsf/form": minor
---

Reset form value in default reset handler
80 changes: 43 additions & 37 deletions apps/docs/src/content/docs/api-reference/handlers.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -6,43 +6,49 @@ sidebar:

```typescript
interface FormProps<T> {
/**
* Will be called when the form is submitted if `onSubmit` or `onSubmitError` is not provided
*/
onsubmit?: EventHandler<SubmitEvent, HTMLFormElement> | undefined | null;
/**
* Will be called when the form is submitted if `onSubmit` or `onSubmitError` is not provided
*/
onsubmit?: EventHandler<SubmitEvent, HTMLFormElement> | undefined | null;

/**
* The function to get the form data snapshot
*
* The snapshot is used to validate the form and passed to
* `onSubmit` and `onSubmitError` handlers.
*
* @default () => $state.snapshot(formValue)
*/
getSnapshot?: () => SchemaValue | undefined
/**
* Submit handler
*
* Will be called when the form is submitted and form data
* snapshot is valid
*/
onSubmit?: (value: T | undefined, e: SubmitEvent) => void
/**
* Submit error handler
*
* Will be called when the form is submitted and form data
* snapshot is not valid
*/
onSubmitError?: (errors: Errors<E>, e: SubmitEvent, snapshot: SchemaValue | undefined) => void
/**
* Reset handler
*
* Will be called on form reset.
*
* By default it will clear the errors and set `isSubmitted` state to `false`.
*
* @default () => { isSubmitted = false; errors.clear() }
*/
onReset?: (e: Event) => void
/**
* The function to get the form data snapshot
*
* The snapshot is used to validate the form and passed to
* `onSubmit` and `onSubmitError` handlers.
*
* @default () => $state.snapshot(formValue)
*/
getSnapshot?: () => SchemaValue | undefined
/**
* Submit handler
*
* Will be called when the form is submitted and form data
* snapshot is valid
*/
onSubmit?: (value: T | undefined, e: SubmitEvent) => void
/**
* Submit error handler
*
* Will be called when the form is submitted and form data
* snapshot is not valid
*/
onSubmitError?: (errors: Errors<E>, e: SubmitEvent, snapshot: SchemaValue | undefined) => void
/**
* Reset handler
*
* Will be called on form reset.
*
* By default it will clear the errors, set `isSubmitted` state to `false` and
* reset the form `value` to the `initialValue`.
*
* @default (e) => {
* e.preventDefault();
* isSubmitted = false;
* errors.clear();
* value = initialValue;
* }
*/
onReset?: (e: Event) => void
}
```
2 changes: 2 additions & 0 deletions apps/docs/src/content/docs/guides/_bind-form.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
const schema: Schema = {
type: "string",
minLength: 10,
};
const form = useCustomForm({
schema,
initialValue: "initial",
onSubmit: (v) => window.alert(v),
});
Expand Down
17 changes: 14 additions & 3 deletions packages/form/src/form/use-form.svelte.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,15 @@ export interface UseFormOptions<T, E> {
*
* Will be called on form reset.
*
* By default it will clear the errors and set `isSubmitted` state to `false`.
* By default it will clear the errors, set `isSubmitted` state to `false` and
* reset the form `value` to the `initialValue`.
*
* @default () => { isSubmitted = false; errors.clear() }
* @default (e) => {
* e.preventDefault();
* isSubmitted = false;
* errors.clear();
* value = initialValue;
* }
*/
onReset?: (e: Event) => void;
schedulerYield?: SchedulerYield;
Expand Down Expand Up @@ -133,9 +139,14 @@ export function createForm<T, E>(

const resetHandler = $derived(
options.onReset ??
(() => {
((e: Event) => {
e.preventDefault();
isSubmitted = false;
errors.clear();
value = merger.mergeFormDataAndSchemaDefaults(
options.initialValue as Value,
options.schema
);
})
);

Expand Down

0 comments on commit fe35ad2

Please sign in to comment.