Skip to content

feat(react): add useForm onSuccess / onError callbacks - #621

Merged
cpojer merged 2 commits into
voidzero-dev:mainfrom
0froq:cursor/feat-useform-onsuccess-onerror-d199
Oct 28, 2026
Merged

cpojer merged 2 commits into
voidzero-dev:mainfrom
0froq:cursor/feat-useform-onsuccess-onerror-d199

Conversation

@0froq

@0froq 0froq commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #524

Add React-only onSuccess and onError options to useForm() for submission feedback and dialog updates while retaining native React form Actions.

Each submission uses its Action's callback closure, so suspended renders cannot replace an in-flight callback. Callbacks may return Promise<void>; the Action awaits them, keeps pending true, and sends callback failures to React's error boundary. Validation failures pass the field-error map to onError; other handled action failures pass VoidActionError. Aborted submissions invoke neither callback.

Callbacks report submission results before the updated UI commits. The forms guide documents using effects or ref callbacks for focusing newly rendered elements. Callback types stay in the React adapter, and FormOptions preserves generated action-body and route-parameter checks.

Regression coverage exercises the public useForm() API in Strict Mode, suspended renders, async success and error callbacks, rejection handling, aborts, and repeated submissions. Type-only cases cover React callback inference and reject unsupported callbacks in Vue, Svelte, and Solid. Happy DOM is a development dependency for the React hook tests.

Validation:

  • vp test run: 14,993 passing tests, 31 skipped.
  • vp run build:all and the metaframework artifact check.
  • vp run check: all 33 typecheck tasks passed.
  • vp run build:docs.
  • Formatting and lint checks, including the commit hooks.

Fixes voidzero-dev#524

## Summary

React `form.post` / `put` / `patch` / `delete` are `useActionState` actions,
so they cannot be awaited. Apps that need a toast, reset, focus move, or
dialog close after submit currently watch `pending` + `wasSuccessful` in an
effect. This adds optional `onSuccess` / `onError` on the third `useForm`
argument (via shared `ActionFormOptions`), called from the action after
success or error state is set.

## Changes

- Invoke `onSuccess` after a successful submit (after `resetOnSuccess` /
  success flags / recently-successful timer); not on abort.
- Invoke `onError` with field validation map (422) or `VoidActionError`;
  not on abort.
- Types on `ActionFormBaseOptions` + codegen for `ActionFormOptions`.
- React-only (this issue's scope); Vue/Solid/Svelte untouched; voidzero-dev#523 not
  included.

## Testing

- `pnpm exec vp test run packages/react/test/unit/use-form.test.ts`
  (5 passed)
- `pnpm exec vp run --filter ./packages/react typecheck`
- `pnpm exec vp test run packages/void/test/unit/route-types.test.ts`
  (18 passed)

## Choices to check

- Added `happy-dom` (catalog + `@void/react` devDependency) so the new
  hook unit tests can mount with `createRoot`. Alternatives considered:
  skip unit tests for this path, or cover with Playwright (heavier for a
  few callback branches). Happy to drop the tests or switch approach if
  maintainers prefer keeping the React package free of a DOM test runner.
@0froq
0froq requested a review from a team as a code owner October 27, 2026 14:03
@socket-security

socket-security Bot commented Oct 27, 2026 •

Copy link
Copy Markdown

Review the following changes in direct dependencies. Learn more about Socket for GitHub.

Diff Package Supply Chain
Security
Vulnerability Quality Maintenance License
Addedhappy-dom@​21.0.466100889670

View full report

@cpojer
cpojer self-requested a review October 27, 2026 15:06
cpojer
cpojer previously approved these changes Oct 28, 2026

@cpojer cpojer left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, let me make some changes here – this is not concurrent safe.

@cpojer
cpojer merged commit 04c23bd into voidzero-dev:main Oct 28, 2026
18 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

React useForm: add onSuccess / onError callbacks

2 participants