You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 0d2ec58
Browse filesBrowse the repository at this point in the historyBrowse files
fix(react): let useForm reset to mount-time defaults after submit
Fixes#523
## Summary
After a successful submit, `useForm` still promotes submitted values to defaults (edit forms / `hasChanges` unchanged). You can now get back to mount-time values:
- `reset({ initial: true }, ...fields)` — reset to the values passed to `useForm` on mount
- `resetOnSuccess: true` on `useForm` options — clear create forms after success
- `reset()` without options still targets current defaults (last submitted values after success)
Shared via `parseFormResetArgs` in `void/pages-client`; React / Vue / Svelte / Solid + docs / codegen.
## Tests
- `packages/void/test/unit/form-reset.test.ts`
- `packages/vue/test/unit/use-form.test.ts` (reset / `reset({ initial: true })` / `resetOnSuccess`)
Typecheck: `void`, `@void/react`, `@void/vue`, `@void/svelte`, `@void/solid`.
Co-authored-by: froQ <sayhola@froq.me>
Copy file name to clipboardExpand all lines: docs/guide/pages-routing/actions-and-forms.md
+5-2Lines changed: 5 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -154,10 +154,11 @@ The types are inferred from your action's `withValidator()` schema in the compan
154
154
|`error`| Action error, or `null`. |
155
155
|`post`, `put`, `patch`, `delete`| Submit the form with that method. In React these are native form action callbacks. |
156
156
|`pending`|`true` while the submission is in flight. |
157
-
|`hasChanges`|`true` if form data differs from initial values. |
157
+
|`hasChanges`|`true` if form data differs from the current defaults. After a successful submit, defaults update to the submitted values so `hasChanges` becomes `false`.|
158
158
|`wasSuccessful`|`true` after a successful submission. Stays `true` until the next submission. |
159
159
|`recentlySuccessful`|`true` for 2 seconds after a successful submission. Useful for flash messages. |
160
-
|`reset(...fields)`| Reset form data to initial values. Field names autocomplete. |
160
+
|`reset(...fields)`| Reset form data to the current defaults (last submitted values after success). Field names autocomplete. Pass `{ initial: true }` first to reset to the values passed to `useForm`. |
161
+
|`resetOnSuccess` (option) | When `true`, a successful submit resets both form data and defaults to the mount-time values. Useful for create forms that should clear after save. |
161
162
|`clearErrors(...fields)`| Clear validation errors. Field names autocomplete. |
<Formform={form} method="put">{/* submits to /users/42 */}</Form>
172
173
```
173
174
175
+
Create forms often need empty fields again after a successful save. Either pass `resetOnSuccess: true` in the third argument, or call `form.reset({ initial: true })` when `form.wasSuccessful` becomes `true`.
176
+
174
177
## Named Actions
175
178
176
179
When a page needs multiple mutations, such as updating and deleting a user on the same page, export `actions` (plural) instead of `action`:
0 commit comments