Repository navigation
Add useFlowForm for typed forms on top of useActionState - #6
Merged
Merged
Conversation
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>
3 tasks done
4 tasks done
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Typed forms for flows.
useFlowForm(flow, { parse, state, mapError })inmutaflow/reactruns a flow from<form action>through ReactuseActionState.What it covers
parsethrowsFlowFormValidationError, the flow does not run, emits no events, andfieldErrors/formErrorsare set.formParser(schema). Builds a typedparsefrom any Standard Schema (zod, valibot, arktype) without depending on one.$ACTION_*fields are skipped.{ formErrors, fieldErrors }) and the formatted one ({ _errors, field: { _errors } }).mapErrorhandles custom errors.<form action>after every submission.valueskeeps the submitted text until the next success, fordefaultValue.state, they work on top of server data likeuseOptimisticFlow. The two hooks now share the transition executor. While the action is pending,stagereportsoptimistic/running. The controller's own state updates are part of the same transition, so they would show the previous stage.fieldErrorsandvaluescome from the flow input, so a typo in a field name does not compile.statemust match the flow's optimistic state.state, any flow is accepted.formState.statusis one ofidle,invalid,success,errororcancelled. The result also includes everythinguseFlowreturns.Example
examples/next-appnow submits throughuseFlowForm:<form action>instead of a controlled input withonSubmit;Limits
useActionStateaction.zodis a root devDependency used only by the tests.Test plan
npm run typecheckfrom a clean build, including the newtests/types/forms.ts. The type test caught a bug while this was being written:useFlowFormwithoutstaterejected flows with a typed resource. It is fixed.npm test: 56/56. The new tests cover:formParserwith zod;mapError.examples/next-app: typecheck andnext build.(pending)item at theoptimisticstage, then the real todo, and the input clears;🤖 Generated with Claude Code