Skip to content

Add useFlowForm for typed forms on top of useActionState - #6

Merged
javaquasar merged 1 commit into
mainfrom
claude/flow-forms
Dec 2, 2026
Merged

javaquasar merged 1 commit into
mainfrom
claude/flow-forms

Conversation

@javaquasar

Copy link
Copy Markdown
Owner

Summary

Typed forms for flows. useFlowForm(flow, { parse, state, mapError }) in mutaflow/react runs a flow from <form action> through React useActionState.

const todoSchema = z.object({ title: z.string().trim().min(1, "Title is required") });

const form = useFlowForm(createTodoFlow, { parse: formParser(todoSchema), state: todos, events });

<form action={form.action}>
  <input name="title" defaultValue={form.values.title} />
  {form.fieldErrors.title?.[0]}
  <button disabled={form.pending}>Add</button>
</form>

What it covers

  • Client validation. When parse throws FlowFormValidationError, the flow does not run, emits no events, and fieldErrors / formErrors are set.
  • formParser(schema). Builds a typed parse from any Standard Schema (zod, valibot, arktype) without depending on one.
    • Issues are grouped by their first path segment. Issues without a path become form errors.
    • Repeated fields become arrays.
    • React's $ACTION_* fields are skipped.
  • Server validation. next-safe-action validation errors map to the same fields, in both the flattened shape ({ formErrors, fieldErrors }) and the formatted one ({ _errors, field: { _errors } }). mapError handles custom errors.
  • Values survive the reset. React 19 resets a <form action> after every submission. values keeps the submitted text until the next success, for defaultValue.
  • Optimistic updates. With state, they work on top of server data like useOptimisticFlow. The two hooks now share the transition executor. While the action is pending, stage reports optimistic / running. The controller's own state updates are part of the same transition, so they would show the previous stage.
  • Types.
    • Field names in fieldErrors and values come from the flow input, so a typo in a field name does not compile.
    • The schema output must match the flow input.
    • state must match the flow's optimistic state.
    • Without state, any flow is accepted.
  • formState.status is one of idle, invalid, success, error or cancelled. The result also includes everything useFlow returns.

Example

examples/next-app now submits through useFlowForm:

  • <form action> instead of a controlled input with onSubmit;
  • a field error for an empty title;
  • an error message when the request fails.

Limits

  • The flow runs on the client, so the form needs JavaScript. There is no progressive enhancement.
  • Submissions run one after another, like any useActionState action.

zod is a root devDependency used only by the tests.

Test plan

  • npm run typecheck from a clean build, including the new tests/types/forms.ts. The type test caught a bug while this was being written: useFlowForm without state rejected flows with a typed resource. It is fixed.
  • npm test: 56/56. The new tests cover:
    • formParser with zod;
    • the full form cycle in jsdom: client error → optimistic item → success, then a next-safe-action validation error and a generic failure;
    • the formatted error shape;
    • mapError.
  • examples/next-app: typecheck and next build.
  • Checked in a browser against the dev server:
    • an empty title shows "Title is required" and keeps the input;
    • a valid title shows the (pending) item at the optimistic stage, then the real todo, and the input clears;
    • no console errors.

🤖 Generated with Claude Code

useFlowForm(flow, { parse, state, mapError }) runs a flow from
<form action> through React useActionState. When parse throws
FlowFormValidationError the flow does not run and fieldErrors /
formErrors are set; next-safe-action validation errors (flattened or
formatted) map to the same fields. values keeps the submitted text
across React's automatic form reset, and optimistic updates work on
top of state like useOptimisticFlow. Field names, values and the result
are typed from the flow.

formParser(schema) builds a typed parse from any Standard Schema (zod,
valibot, arktype) without depending on one. The transition executor is
shared with useOptimisticFlow. The next-app example submits its form
through useFlowForm. zod is a root devDependency for tests only.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@javaquasar
javaquasar merged commit 3494543 into main Dec 2, 2026
3 checks passed
@javaquasar javaquasar mentioned this pull request Dec 2, 2026
4 tasks done
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.

1 participant