This page renders a Schema Builder entry the way its schema describes it: fields in order, sections with headings, Repeater items, and conditional fields hidden exactly as the app hides them. The code is the sample's EntryView component.
Hide what the app hides#
A hidden field keeps its value in jsonField. Before you show or use a value, check that its field is visible. visibleFieldIds is copied from the app, so your site and the entry form always agree:
import type { SchemaField, ShowWhen } from "./registry.ts";
export function ruleMatches(rule: ShowWhen, value: unknown): boolean {
if (typeof rule.values[0] === "boolean") return rule.values.includes(value === true);
if (Array.isArray(value)) return value.some((v) => rule.values.includes(v));
return rule.values.includes(value as string);
}
/** A field is visible when it has no rule, or its controller is visible and the rule matches. Cycles are hidden. */
export function visibleFieldIds(fields: SchemaField[], data: Record<string, unknown>): Set<string> {
const byId = new Map(fields.map((f) => [f.id, f]));
const memo = new Map<string, boolean>();
const visiting = new Set<string>();
const visible = (field: SchemaField): boolean => {
const known = memo.get(field.id);
if (known !== undefined) return known;
if (!field.showWhen) return true;
if (visiting.has(field.id)) return false;
visiting.add(field.id);
const controller = byId.get(field.showWhen.field);
const result = !!controller && visible(controller) && ruleMatches(field.showWhen, data[controller.id]);
visiting.delete(field.id);
memo.set(field.id, result);
return result;
};
return new Set(fields.filter(visible).map((f) => f.id));
}To check rules inside a Repeater, pass the Repeater's sub-fields and one item's data: rules are evaluated per item.
Render each field type#
One component turns a value into output by field type. A value without the expected shape, such as old data after a field changed, falls back to plain text instead of throwing:
function Value({ field, value }: { field: SchemaField; value: unknown }): ReactNode {
switch (field.type) {
case "boolean":
return value === true ? "Yes" : "No";
case "integer":
case "decimal":
return typeof value === "number" ? value.toLocaleString("en-GB") : String(value);
case "multiSelect":
case "list":
return Array.isArray(value) ? value.join(", ") : String(value);
case "media":
case "reference":
return linkText(value);
case "repeater":
return Array.isArray(value) ? (
<ol className="items">
{value.map((item, i) => (
<li key={i}>
<Fields fields={field.fields ?? []} data={(item ?? {}) as Record<string, unknown>} />
</li>
))}
</ol>
) : null;
default:
return String(value);
}
}The full component also formats dates and keeps line breaks in long text. Links show the linked ID; Linked assets and entries explains how to fetch what they point at.
Lay out sections#
Sections change only how the entry form looks; the data is the same. To mirror the form, show unsectioned fields first, then each section with a heading. Work out visibility across all fields first, because a field in one section can control a field in another:
export function EntryView({ schema, entry }: { schema: Schema; entry: SchemaEntry }) {
const data = entry.fields.jsonField ?? {};
const visible = visibleFieldIds(schema.fields, data);
const known = new Set(schema.sections?.map((s) => s.id));
const loose = schema.fields.filter((f) => !f.section || !known.has(f.section));
return (
<>
<Fields fields={loose} data={data} visible={visible} />
{schema.sections?.map((section) => {
const fields = schema.fields.filter((f) => f.section === section.id && visible.has(f.id) && hasValue(f, data));
return fields.length ? (
<section key={section.id}>
<h2>{section.name}</h2>
<Fields fields={fields} data={data} visible={visible} />
</section>
) : null;
})}
</>
);
}The entry page#
The page fetches the registry and the entry together and lets the entry's own type pick the schema, so a wrong address is a 404 instead of a page with the wrong layout:
export default async function EntryPage({ params }: Props) {
const { schema: schemaId, id } = await params;
const [registry, entry] = await Promise.all([getRegistry(), getEntry(id)]);
const schema = registry.schemas.find((s) => s.id === entry?.fields.type);
if (!entry || !schema || schema.id !== schemaId) notFound();
return (
<>
<h1>{entry.fields.title}</h1>
<EntryView schema={schema} entry={entry} />
</>
);
}In the Fernhill demo the Ridgeline 2 tent is waterproof, so its page shows a Waterproof rating of 3,000. The Summit Pack 45 isn't, so its page leaves the rating out.
Things to know
- A Boolean that was never ticked is stored as nothing. Treat it as "No", as the app does.
- Hidden fields keep their values. Filter with
visibleFieldIdsbefore you show or use them. EntryViewis plain HTML with no UI library. Copy it as a starting point and style it your way.