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
Commit dd9489a
Browse filesBrowse the repository at this point in the historyBrowse files
feat(ui-react): DocForm, a schema-driven TanStack Form editor primitive in /primitives (5b(i)) (#33) (#33)
Chain step 9 of the linear merge chain onto main 3b4a112: this PR's own changes, resolved against the PRs before it in the merge order.
Co-authored-by: Cursor Agent <cursoragent@cursor.com>
Add `DocForm` and `useDocForm` to `@tanstack/cms-ui-react/primitives`: a schema-driven document form on TanStack Form. Pass a collection name and it renders a control per field in schema order, honouring `showWhen` and, given `access`, field-level read and write rules. Use it controlled (`value`/`onChange`) or uncontrolled (`defaultValue`). `onSubmit` receives a patch of the fields that changed since the baseline, plus the whole document and any `submit(meta)` argument. `renderers` passes the app's renderers module (`defineRenderers`) to every `FieldInput`, and `fieldRenderers` replaces the control for a field type in one form, built-in types included. `fieldRenderers` is checked first. `docPatch(baseline, doc)` is also exported. Nothing breaks: the admin edit route doesn't use the form yet.
A`<form>`foronedocumentofacollection, generatedfromitsschema. Itkeepsthedraftina [TanStackForm](https://tanstack.com/form) instance, renders a control per top-level field in schema order, and calls `onSubmit` with a patch of the fields that changed. It doesn't save anything itself: the submit handler decides where the patch goes.
391
+
392
+
```tsx validate=type
393
+
import { DocForm } from '@tanstack/cms-ui-react/primitives'
394
+
import type { Doc } from '@tanstack/cms-core'
395
+
396
+
export function PostForm({ post, save }: { post: Doc; save: (patch: Doc) => Promise<void> }) {
|`collection`|`string`|Thecollection's name. An unknown name throws, as for the other primitives. |
412
+
|`onSubmit`|`(patch: Doc, context: { doc, baseline, meta }) => unknown`|Receivesthetop-levelfieldswhosevaluesdifferfromthebaseline, comparedasJSON, plusthewholedocument, thebaseline, andthe`meta`givento`submit(meta)`. Ifitthrowsorrejects, thesubmitfails: theerroriskeptandthebaselinedoesn't move. Otherwise the submitted document becomes the baseline. |
413
+
|`value`/ `onChange` | `Doc` /`(doc: Doc) => void`|Controlleduse. `onChange`getsthewholedocumentaftereverychange. When`value`changestosomethingotherthantheform's values, the form adopts it without calling `onChange`. |
|`fieldRenderers`|`{ [fieldType]: ComponentType<FieldInputComponentProps> }`|Per-formrenderersfortop-levelfields, keyedbyfieldtype, built-intypes (`text`, `markdown`) included. Eachreplaces`FieldInput`forthattypeandgets`FieldInput`'s props, so it can wrap `FieldInput`. Fields nested in `group`, `array`, and `blocks` still render with `FieldInput`. |
|`access`|`{ session, doc }`|Field-levelaccess: fieldsthesessioncan't read are left out, and fields it can'twriteareread-only. `doc`isthestoreddocument, or`null`forthedraft. Without`access`, everyfieldiseditable; theserverstillenforcesfieldrules. |
`renderers` and `fieldRenderers` work at different levels. `renderers` is the app's module, keyed by input name (a field's `input`) and by custom field type. `fieldRenderers` is an override for one form, keyed by field type. For each top-level field, `DocForm` uses `fieldRenderers[field.type]` if there is one. Otherwise it renders `FieldInput`, which resolves as usual: the field's named input, then its custom type's input (each from `renderers`, then the bound module), then the built-in control. A `renderers.fieldTypes` input replaces only the control under `FieldInput`'s label and error message, and it can't target built-in types. A `fieldRenderers` entry replaces the whole field, for any type.
427
+
428
+
Fields hidden by `showWhen` aren't rendered, and their stored values stay in the document. There is no client-side validation yet: put a save's field errors in `errors`.
429
+
430
+
To keep the form's state in the parent (for example to put the buttons in a header), call `useDocForm` with the same options and pass the result to `DocForm` as `form`:
`useDocForm` returns `{ form, collection, values, baseline, patch, dirty, submitting, error, submit, reset, setValue }`. `form` is the TanStack Form instance, for `form.Field` and `form.Subscribe`. `submit(meta?)` resolves `true` when `onSubmit` succeeded. `reset()` goes back to the baseline, and `reset(doc)` loads `doc` and makes it the baseline. `docPatch(baseline, doc)` is the patch computation on its own.
- Filters and sorts on a field the session can't read, including nested paths like `pricing.cost`, fail with `FORBIDDEN`.
447
447
- API keys can additionally be scoped to a list of collections.
448
448
449
+
## Editing documents in your own UI
450
+
451
+
The admin's edit screen is generated from these definitions, and a bespoke app can generate the same form. `DocForm` from `@tanstack/cms-ui-react/primitives` takes a collection name, renders a control for each field in schema order, and applies the rules above: fields hidden by `showWhen` aren't shown, and with `access` the session's field rules hide or lock fields. The draft lives in a TanStack Form instance. On submit, your handler gets a patch of the fields that changed, and you decide where it goes:
To change how a field type looks, pass `fieldRenderers`, keyed by field type, for example `fieldRenderers={{ number: Stepper }}`. Each renderer receives the props of `FieldInput`, which renders every other type. See the [`DocForm` reference](../api/cms-ui-react#docform) for controlled use, `useDocForm`, and submit metadata.
478
+
449
479
## Hooks
450
480
451
481
A hook is the engine asking, just before a commit, "anything you want to make us aware of before we go in?" Hooks may rewrite the document or veto the write by throwing. Anything that reacts to the result after the commit is an [event handler](../architecture/events-and-hooks) instead. Every hook may be async; the engine awaits it. Keep each one under about 50ms: outside production the engine warns once per hook that runs longer.
0 commit comments