Skip to content

feat(ui-react): DocForm, a schema-driven TanStack Form editor primitive in /primitives (5b(i)) - #33

Merged
jherr merged 1 commit into
mainfrom
cursor/form-editor-primitive-d21e
Nov 6, 2026
Merged

jherr merged 1 commit into
mainfrom
cursor/form-editor-primitive-d21e

Conversation

@jherr

@jherr jherr commented Nov 6, 2026 •

Copy link
Copy Markdown
Contributor

This PR implements the first half of cms-refactor-plan.md § Phase 5, 5b (item 10a): exporting the TanStack Form editor primitive. The second 5b PR rebuilds the admin edit route on it, after 5a.

Merge chain: step 9 of 24. Base: main. Merge it right after #32. The branch is main 3b4a112, then one commit for each PR before this one in the merge order, then this PR's own commit 99dea28 (tree ed1e9ba).

What

@tanstack/cms-ui-react/primitives: DocForm, useDocForm, docPatch (src/components/editor/doc-form.tsx)

  • Collection by name. <DocForm collection="post" … /> resolves the name through core's collections() registry (requireCollection), like the 1b primitives. An unknown name throws.
  • Fields. One control per top-level field, in schema order. Fields hidden by showWhen are left out, and showWhen is evaluated against the live values.
  • Field access. With access={{ session, doc }}, fields the session can't read are hidden and fields it can't write are locked. These are the same rules the admin route and the server use.
  • TanStack Form state. The draft lives in a TanStack Form instance from @tanstack/react-form, which is already a dependency. Each field is a form.Field, so 5c's validators and blur metadata can attach without an API change.
  • Controlled or uncontrolled. Use value/onChange or defaultValue. When value changes from outside, the form adopts it without echoing it back through onChange.
  • onSubmit(patch, { doc, baseline, meta }).
    • The patch holds the top-level fields that differ from the baseline, compared as JSON. The handler also gets the whole document and the submit(meta) argument.
    • The submit handler is a prop, so the primitive doesn't depend on 5a's save().
    • If the handler throws, the baseline is kept and the error comes back as error. Otherwise the submitted doc becomes the new baseline, unless the parent controls baseline.
  • renderers?: CmsRenderers (on DocForm and useDocForm). This is the app's renderers module from feat(core, ui-react)!: field inputs and cells move out of the config into the renderers module (R2 fields) #32 (defineRenderers({ inputs, fieldTypes })). It's passed to every FieldInput, nested fields included. Without it, FieldInput falls back to the module bound through createRoutes.
  • fieldRenderers. A per-form override keyed by field type ({ number: Stepper }). An entry replaces the whole FieldInput for that type. It receives FieldInputComponentProps, renderers included, so it can wrap FieldInput.
  • Precedence:
    1. fieldRenderers[field.type];
    2. the field's named input, from renderers, then the bound module;
    3. the custom type's input, from renderers, then the bound module;
    4. the built-in control.
  • Why fieldRenderers stays next to renderers.fieldTypes. The two overlap only for custom types. fieldTypes rejects built-in types and replaces only the inner control under FieldInput's label and error. fieldRenderers covers built-in types too and replaces the whole field, for one form.
  • Passed through to FieldInput: errors (per field, e.g. a save's fieldErrors), changedFields (the proposal highlight), store, relationSearch, disabled.
  • useDocForm(options) returns { form, collection, values, baseline, patch, dirty, submitting, error, submit, reset, setValue }. <DocForm form={api} /> renders it, so a parent can place its buttons elsewhere and pass submit metadata (submit({ publish: true })).
  • S1 boundary test: asserts the new exports.

Starter usage. The runtime-tables starter's GeneratedRuntimeForm is rebuilt on DocForm, by collection name, with an onSubmit(patch, doc) prop. The seam test server-renders it.

Docs.

  • The cms-ui-react reference has a DocForm section: a props table, useDocForm, docPatch, two type-checked examples, and how renderers and fieldRenderers relate.
  • collections-and-fields.md has a new guide section, "Editing documents in your own UI".
  • The runtime-tables CMS-INTEGRATION.md is updated.

Changeset. .changeset/doc-form-primitive.md: minor for @tanstack/cms-ui-react. It's additive.

How the admin edit route adopts it (second 5b PR, after 5a)

  • What leaves the route. edit.$collection.$id.tsx stops mapping fields to FieldInput itself. Field filtering, write locks, the dirty compare and the fieldErrors wiring move into DocForm.
  • What stays. The core createDocEditorStore stays (Q23). It wraps the form with optimistic concurrency, the pending overlay and autosave. onSubmit calls 5a's strategy action.
  • Where form-core lands. 5a's { doc, state, availableActions } drives the buttons and becomes the baseline. That PR is where @tanstack/form-core enters core.

How it closes the S1 leak. After the second PR, the edit route is composition only: DocForm plus the core editor store and panels. The S1 test can then forbid the route from rendering FieldInput per field.

Breaking changes

None in published packages. The props of the runtime-tables starter's own GeneratedRuntimeForm change, but that starter is unpublished and only its seam test used the component.

How verified

CI is down because GitHub Actions is out of billing, so I verified locally, in the post-batch-3 state. In a scratch branch, I rebased #25 → #28 → #31 → #32 → 5b(i) onto #24's main-based head 2850c54 with git rebase --onto 2850c54 e126e12. All 44 commits replayed with no conflicts.

  • pnpm install --frozen-lockfile: ok.
  • pnpm test:ci (174 tasks, no cache): passes except the known @tanstack/cms-api:test:oxlint failure, which main also has.
  • test:dts: clean, 226 .d.ts files.
  • test:dialects: passes. It reports readDocument (test) rising from 244 to 248 above the baseline. That count is already on main, from packages/core/tests/slow-hooks.test.ts in e568873 (feat(core)!: hooks as pre-commit consultation, events as announcement; drop legacy hook aliases #19).
  • test:definitions: 26/26.
  • test:scripts: 93/93.
  • E2E: blog 29/29, base 12/12.
  • Unit tests: 12 new tests in packages/ui-react/tests/doc-form.test.tsx.

Assumptions (tasks 8–12 defaults)

The plan gives no default for these, so each takes the most conservative option.

  • Names: DocForm and useDocForm.
  • The patch:
    • It's top-level and JSON-compared, holding whole field values.
    • A field missing from the doc is null.
    • onSubmit runs even when the patch is empty.
  • The baseline starts as the initial doc and moves to each successfully submitted doc. A baseline prop lets the parent own it.
  • fieldRenderers apply to top-level fields only. Nested sub-fields still get renderers.
  • renderers given to DocForm win over those given to useDocForm.
  • Without access, every field is editable.
  • Client validation: none yet; that's 5c. Server errors come in through errors.
  • No built-in submit button, and no new admin strings.
  • Form values are typed Record<string, unknown> internally, because TanStack Form's deep field-name types fail with TS2589 on JsonValue. The public API uses Doc throughout.

Divergence from plan

  • @tanstack/form-core isn't added to core yet. Q23 puts form-core in core. It arrives in the second 5b PR, where core's editor store wraps the form; adding it now would leave an unused dependency.

Merge chain (local verification, CI down)

This PR's commits are squashed into one commit on the linear merge chain. That commit holds this PR's own changes, resolved against every PR before it, with the integration rehearsal's fixes for this PR applied. No conflicts.

The chain tip's tree is exactly the integration rehearsal plus #46 (c4e6d7c), and the tip passes the full local suite. I simulated merging every PR in order, with a merge commit or a fast-forward at each step, and every step is clean. A plain squash of the next PR would conflict, because the squash commit isn't an ancestor of the next branch. To squash instead, first rebase the branch's one commit onto the new main; that rebase is always clean.

Open in Web Open in Cursor 

@coderabbitai

coderabbitai Bot commented Nov 6, 2026 •

Copy link
Copy Markdown

Warning

Review limit reached

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

Next included review available in 59 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

Learn how review limits work.

Review configuration:

Run configuration

  • Source mode: Auto
  • Repository: TanStack/cms
  • Branch: main
  • File: .coderabbit.yaml
  • Commit: 87b29ae
  • Layers: Repository YAML
  • Review profile: CHILL
  • Plan: Free
  • Run ID: c8b110e7-a3bc-4855-9892-a088bd7a7a6d
Commits

Reviewing files that changed from the base of the PR and between 87b29ae and ee4f37f.

Files selected for processing: 10

  • .changeset/doc-form-primitive.md
  • docs/api/cms-ui-react.md
  • docs/schema/collections-and-fields.md
  • packages/ui-react/src/components/editor/doc-form.tsx
  • packages/ui-react/src/primitives.ts
  • packages/ui-react/tests/doc-form.test.tsx
  • packages/ui-react/tests/s1-primitives-boundary.test.ts
  • starters/runtime-tables/CMS-INTEGRATION.md
  • starters/runtime-tables/src/generated-views.tsx
  • starters/runtime-tables/tests/seam.test.ts

TanStack is on CodeRabbit Free, which includes PR summaries. Ask your admin to upgrade for code reviews.

  • Ask an admin to upgrade

Open in CodeRabbit

  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

@changeset-bot

changeset-bot Bot commented Nov 6, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: ee4f37f

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 13 packages
Name Type
@tanstack/cms-ui-react Minor
@tanstack/cms-api Minor
@tanstack/cms-cli Minor
@tanstack/cms-client Minor
@tanstack/cms-core Minor
@tanstack/cms-media-s3 Minor
@tanstack/cms-store-libsql Minor
@tanstack/cms-connect-atproto Patch
@tanstack/cms-migrate-wp Patch
@tanstack/cms-skills Patch
@tanstack/search-cms Patch
@tanstack/cms-store-postgres Patch
@tanstack/cms-ui-dom Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

…ve in /primitives (5b(i)) (#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: jherr <jherr@pobox.com>
@jherr
jherr force-pushed the cursor/form-editor-primitive-d21e branch from 99dea28 to ee4f37f Compare November 6, 2026 15:07
@jherr
jherr merged commit dd9489a into main Nov 6, 2026
@jherr
jherr deleted the cursor/form-editor-primitive-d21e branch November 8, 2026 01:55
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.

2 participants