ContentEditor
Edit caller-provided CMS fields with controlled save and publish callbacks.
ContentEditor renders controls from a CmsCollectionPresentation, reports every change through onChange, and exposes separate draft and publish callbacks. Complex JSON-compatible fields use UI JsonEditor; supply renderField for application-specific media and relationship pickers.
API
| Prop | Type | Required | Default |
|---|---|---|---|
contentType | CmsCollectionPresentation | Yes | — |
value | Partial<FieldsValue<TFields>> | Yes | — |
onChange | (value: Partial<FieldsValue<TFields>>) => void | Yes | — |
validationErrors | Readonly<Record<string, string>> | No | {} |
onSaveDraft | () => void | Promise<void> | Yes | — |
onPublish | () => void | Promise<void> | No | — |
onDiscard | () => void | No | — |
onInvalidDraftChange | (invalid: boolean) => void | No | — |
renderField | (context: ContentEditorFieldContext) => ReactNode | No | Built-in control |
disabled / saving / dirty | boolean | No | false |
confirmDiscard | boolean | No | false |
warnOnUnload | boolean | No | false |
The built-in renderer provides native controls for text, slug, locale, email,
URL, textarea, number, boolean, date/datetime, select, and multi-select fields.
JSON-compatible JSON, array, object, block, computed, custom, media,
relationship, and rich-text values use the JSON editor by default; computed
fields are read-only. Use renderField for application-specific media or
relationship pickers, rich-text editors, and other specialized controls. The
callback receives the field definition, current value, validation error,
disabled state, and an onChange function.
import { createCmsClient } from "@puzzle-ui/fullstack/cms/client";
import type { CmsApiRecord } from "@puzzle-ui/fullstack/cms/shared";
import { ContentEditor } from "@puzzle-ui/pieces";
const cms = createCmsClient({ basePath: "/api/cms" });
const articles = cms.collection("articles");
const saveDraft = async (
entryId: string,
fields: CmsApiRecord,
expectedRevision: number,
) => {
await articles.update(entryId, fields, { expectedRevision });
};
const publish = async (entryId: string, expectedRevision: number) => {
await articles.publish(entryId, { expectedRevision });
};
<ContentEditor
contentType={contentType}
value={fields}
validationErrors={serverIssues}
onChange={setFields}
onSaveDraft={() => saveDraft(entry.id, fields, entry.revision)}
onPublish={() => publish(entry.id, entry.revision)}
/>;Validation and state
The component displays field-level errors and reports rejected save/publish callbacks as visible alerts. Built-in JSON-compatible fields propagate valid edits immediately; invalid JSON remains visible locally and blocks save and publish until corrected or discarded. Invalid JSON also counts as unsaved for the optional discard and unload warnings. Use onInvalidDraftChange to include that local state in host navigation guards. Custom field renderers must manage their own invalid state and guards.
The component infers TFields from contentType.fields; draft state can be
typed as Partial<FieldsValue<typeof articleFields>>. The component does not
mark server validation as successful, auto-save, handle conflicts, or authorize
publish. For a collection configured with drafts, the verified Fullstack client
uses collection.update to save a draft and collection.publish to publish it;
both operations require the current expectedRevision. The dirty indicator
is caller-controlled for valid values. Set confirmDiscard to confirm the
discard action while dirty, and warnOnUnload to request the browser's native
warning on reload or tab close. Browsers control the warning text and may
suppress it without prior user interaction. Client-side router transitions do
not fire beforeunload; the host must integrate its router's navigation blocker
using both dirty and invalid-draft state.
Related
API maturity
API maturity: Stable.