Repository navigation
What formdef relies on in TanStack Form v2 (types, internals, findings) #2497
maurice-rm
started this conversation in
v2 Ideas and Discussions
Replies: 1 comment
|
Update on |
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
formdef describes a form as a typed config and renders it with TanStack Form. Its core check: a widget declares its value type through its
fieldprop, and a config that puts it on a field of another type doesn't compile.I ported formdef to the alpha on the branch
experiment/tanstack-v2, using the pkg.pr.new builds of #2495 and, since it was merged, #2496. Typecheck, tests and build are green apart from the points below. The port won't be released; the published formdef stays on v1. A weekly CI job runs it against the newest alpha build, so I'll report when something here breaks.Types formdef relies on
Apart from
FieldWithValueand the last row, formdef spells out the type params of these types itself instead of letting them be inferred. ⚠ marks the ones your TSDoc calls Library-managed. Do not specify explicitly.FieldWithValue<V>fieldprop. formdef infersVfrom it and compares it with the field's value. The one that matters most.FormOptions⚠FieldValidators⚠,FieldListeners⚠,FormOptionsValidators,FieldApiOptionsFormApi<TData, any>,ReactTanStackFormComponents['Subscribe']Pickof the form, which formdef hands to custom layouts.createFormHook,AppFormHookResult⚠,ReactFormComponentMap,BoundFormComponents,ReactComponentTree,ReactFormComponentscreateFormHookand has to name the result type.DeepKeys,StandardSchemaV1,AnyFieldApi,AnyFormApi,useSelector,formOptions,getFormHookHelpers().formContextAt runtime formdef also relies on
Field,ArrayField,Subscribe,FormGroup, the bound form components (form[name]), the validator object ({ run, triggers: ['blur', { trigger: 'change', when }], triggerDebounceMs }) and errors shaped{ message }. It types these structurally on its side, so a change there shows up at runtime, not intsc.One internal
erring-fields.tsreadsform._atoms.meta.errorFieldsand the field nodes (_children,name,meta,atom). Forms with steps need field meta by path across the whole form, including fields that aren't mounted: to mark steps with errors and to focus the first invalid field. v1 hadstate.fieldMetaandgetFieldMeta; in v2getFieldMetais only onInternalFormApi.A public, subscribable view would replace it, e.g.
form.getFieldMeta(path)plus a selector over the fields that hold errors.Not needed
FieldWithValue<V>staying stable, withVinferable, is enough.fieldContext. formdef's check needs the value type in the widget's props, which a context can't show.formContextworks fine with formdef. AfieldContext.create<V>()in the same shape would be nice inside widgets, but formdef doesn't need it.Questions
@tanstack/react-formESM-only for good? It decides whether formdef keeps a CJS build.FormGroup.handleSubmitdoesn't run form validators, so formdef adds a group validator per step that runs the form schema. Is that the intended way?ArrayField's own errors can't be subscribed without re-rendering on every item edit. fix(react-form): [v2] rerenderArrayFieldwhen the array's own errors change #2444 looks like it covers that.Bugs
_updateInstances()without an argument, so the previous validators becomenull, andValidatorInstance.updateonly skips the check forundefined. Repro withInternalFormApifrom@tanstack/form-core/internals:new InternalFormApi({ defaultValues: { a: '' }, validators: [{ run: () => undefined, triggers: ['change'] }] })warns; withoutvalidatorsit doesn't. I found no issue for it; happy to open one.Fieldkeeps a stale field API afterArrayFieldremoveValue()shifts items, so shifted errors aren't rendered #2391, stale fields inArrayFieldaftermoveValue/removeValue. I reproduced it in React without formdef. [v2] Fix: Move mounted fields onto the current field API after array mutations #2393 targets it but conflicts withalpharight now.I have a longer version with file paths on both sides, if that helps.
All reactions