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
Performant form building: fine-grained field state + TanStack Form integration
#507
Make json-render usable as a performant form builder. A JSON spec (from an AI or a backend) should render a form with dozens of fields without re-rendering the whole tree on each keystroke. The form should also have proper form state: values, errors, touched/dirty, async validation and submit.
Problem
json-render already has most of the building blocks: inputs bound to state paths, validation checks, visible / $cond conditions and actions. Using them for real forms runs into two problems:
There's no form-level state, and async validation doesn't work. There's no built-in notion of touched, dirty, submitting or errors per field. Async custom checks don't work even though the docs show an async example (Custom validation checks do not support async implementation #291).
Example
A registration form described in a spec (shape simplified, for illustration):
Typing in email re-renders the email field only. company, vat and the other fields don't re-render.
isUniqueEmail can be async and runs on blur, with its result shown as that field's error.
Toggling isCompany re-renders only the elements whose visible condition reads /form/isCompany.
SubmitButton can read isSubmitting / canSubmit without subscribing to every value.
On submit, register receives the validated values.
Proposed API
Option A: TanStack Form adapter (separate package)
json-render owns the structure (which fields exist, their order and visibility). TanStack Form owns the values, validation and submit state.
import{JSONUIProvider,Renderer}from"@json-render/react";import{useJsonRenderForm,FormStateProvider}from"@json-render/tanstack-form";functionRegistrationForm({ spec }){constform=useJsonRenderForm({
spec,statePath: "/form",validators: {isUniqueEmail: async(value)=>{constres=awaitfetch(`/api/email-available?email=${value}`);return(awaitres.json()).available;},},onSubmit: async({ value })=>{awaitapi.register(value);},});return(<JSONUIProviderregistry={registry}><FormStateProviderform={form}><Rendererspec={spec}/></FormStateProvider></JSONUIProvider>);}
Under the hood:
useJsonRenderForm builds a TanStack useForm. Default values come from spec.state, and each field's checks become TanStack validators (sync and async, honouring validateOn).
Bound catalog components are wrapped in form.Field name={path}, so each field re-renders only when its own state changes.
Form-wide components (SubmitButton, error summaries) use form.Subscribe with a selector.
FormStateProvider plugs TanStack's store into the existing controlled StateProvider store. visible, $cond and actions then read the same values as the form, with no second copy to keep in sync.
Catalog components receive field state through a hook:
Option B fixes the re-render problem for every json-render user, including non-form UIs. Option A gives a full form feature set without growing core. The two could also be combined: B in core, A as an optional adapter.
Streaming
Forms generated by an AI arrive as streamed patches, so structure changes and value changes need to stay separate:
Patches that add or remove elements update the field registry without resetting values the user has already entered.
Values in spec.state are used only as default values. They don't overwrite fields the user has already edited.
When a field is removed, its value is either kept or dropped, depending on a configurable policy.
Acceptance criteria
Changing one bound input re-renders only that field and the elements whose conditions depend on its path
Async validation in checks, with validateOn: "change" | "blur" | "submit"
Per-field state is available to catalog components: errors, touched, dirty, validating
Form-level state (isSubmitting, canSubmit) can be used without subscribing to all values
The submit action receives validated values
Streaming patches never reset values the user has entered
A benchmark or example form with 100+ fields that stays smooth while typing
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Summary
Make json-render usable as a performant form builder. A JSON spec (from an AI or a backend) should render a form with dozens of fields without re-rendering the whole tree on each keystroke. The form should also have proper form state: values, errors, touched/dirty, async validation and submit.
Problem
json-render already has most of the building blocks: inputs bound to state paths, validation checks,
visible/$condconditions and actions. Using them for real forms runs into two problems:Example
A registration form described in a spec (shape simplified, for illustration):
{ "root": "form", "state": { "form": { "email": "", "company": "", "isCompany": false, "vat": "" } }, "elements": { "form": { "type": "Form", "props": { "submitAction": "register" }, "children": ["email", "isCompany", "company", "vat", "submit"] }, "email": { "type": "Input", "props": { "label": "Email", "value": { "$bindState": "/form/email" }, "checks": [ { "type": "required", "message": "Email is required" }, { "type": "email", "message": "Invalid email" }, { "type": "isUniqueEmail", "message": "Email already used" } ], "validateOn": "blur" } }, "isCompany": { "type": "Checkbox", "props": { "label": "I'm a company", "checked": { "$bindState": "/form/isCompany" } } }, "company": { "type": "Input", "visible": { "path": "/form/isCompany" }, "props": { "label": "Company", "value": { "$bindState": "/form/company" }, "checks": [{ "type": "required", "message": "Company is required" }] } }, "vat": { "type": "Input", "visible": { "path": "/form/isCompany" }, "props": { "label": "VAT number", "value": { "$bindState": "/form/vat" } } }, "submit": { "type": "SubmitButton", "props": { "label": "Create account" } } } }What we'd expect:
emailre-renders theemailfield only.company,vatand the other fields don't re-render.isUniqueEmailcan be async and runs on blur, with its result shown as that field's error.isCompanyre-renders only the elements whosevisiblecondition reads/form/isCompany.SubmitButtoncan readisSubmitting/canSubmitwithout subscribing to every value.registerreceives the validated values.Proposed API
Option A: TanStack Form adapter (separate package)
json-render owns the structure (which fields exist, their order and visibility). TanStack Form owns the values, validation and submit state.
Under the hood:
useJsonRenderFormbuilds a TanStackuseForm. Default values come fromspec.state, and each field'schecksbecome TanStack validators (sync and async, honouringvalidateOn).form.Field name={path}, so each field re-renders only when its own state changes.SubmitButton, error summaries) useform.Subscribewith a selector.FormStateProviderplugs TanStack's store into the existing controlledStateProviderstore.visible,$condand actions then read the same values as the form, with no second copy to keep in sync.Option B: fine-grained state in core (no form library)
Same goals, with no dependency on a form library:
useSyncExternalStorewith a path selector) instead of the whole state.visible,$cond) track the paths they read and re-run only when those paths change.errors,touchedanddirtyper path, plusisSubmittingandcanSubmit.checksare supported, as theValidationFunctiontype (boolean | Promise<boolean>) already suggests (Custom validation checks do not support async implementation #291).Option B fixes the re-render problem for every json-render user, including non-form UIs. Option A gives a full form feature set without growing core. The two could also be combined: B in core, A as an optional adapter.
Streaming
Forms generated by an AI arrive as streamed patches, so structure changes and value changes need to stay separate:
spec.stateare used only as default values. They don't overwrite fields the user has already edited.Acceptance criteria
checks, withvalidateOn: "change" | "blur" | "submit"isSubmitting,canSubmit) can be used without subscribing to all valuesRelated
#54, #53 (re-renders) · #291 (async validation) · #245 (loading data on mount: prefill, select options) · #222 (
validate()stripsstate/on/watch)All reactions