Form builder
Build a form: pages and question cards, an add menu by type, drag or Alt + arrows to reorder, show-if logic, a live preview.
import { FormBuilder, emptyForm, formProblems, moveFormQuestion, newFormQuestion, formConditionFields } from "@forgeron/ui";Page 1
{
"version": 1,
"title": "Workshop feedback",
"description": "Two minutes, anonymous.",
"thanks": "Thank you for your feedback.",
"sections": [
{
"id": "s1",
"title": "The day",
"questions": [
{
"id": "q1",
"type": "singleChoice",
"title": "Which session did you attend?",
"required": true,
"options": [
{
"id": "o1",
"label": "Morning"
},
{
"id": "o2",
"label": "Afternoon"
}
]
},
{
"id": "q2",
"type": "rating",
"title": "How was it?",
"required": true,
"range": {
"min": 1,
"max": 5
}
},
{
"id": "q3",
"type": "longText",
"title": "What should we change?"
}
]
}
]
}import { FormBuilder, type FormSchema } from "@forgeron/ui";
import { useState } from "react";
const initial: FormSchema = {
version: 1,
title: "Workshop feedback",
description: "Two minutes, anonymous.",
thanks: "Thank you for your feedback.",
sections: [
{
id: "s1",
title: "The day",
questions: [
{
id: "q1",
type: "singleChoice",
title: "Which session did you attend?",
required: true,
options: [
{ id: "o1", label: "Morning" },
{ id: "o2", label: "Afternoon" },
],
},
{
id: "q2",
type: "rating",
title: "How was it?",
required: true,
range: { min: 1, max: 5 },
},
{ id: "q3", type: "longText", title: "What should we change?" },
],
},
],
};
export default function Default() {
const [schema, setSchema] = useState(initial);
return (
<div style={{ display: "grid", gap: 16, width: 760, maxWidth: "100%" }}>
<FormBuilder value={schema} onChange={setSchema} />
<pre
style={{
margin: 0,
padding: 12,
maxHeight: 200,
overflow: "auto",
border: "1px solid var(--line)",
background: "var(--bg-sunk)",
font: "12px/18px var(--font-mono)",
}}
aria-label="The value of the builder"
>
{JSON.stringify(schema, null, 2)}
</pre>
</div>
);
}Page 1
Page 2
Show this page only if
Where
Field
Operator for Budget
import { FormBuilder, type FormSchema } from "@forgeron/ui";
// A page and a question that show only for some answers (open a card, then Show this question
// only if…). The rules read the questions before them.
const schema: FormSchema = {
version: 1,
title: "Request for equipment",
sections: [
{
id: "s1",
title: "The request",
questions: [
{
id: "kind",
type: "singleChoice",
title: "What do you need?",
required: true,
options: [
{ id: "laptop", label: "A laptop" },
{ id: "screen", label: "A screen" },
{ id: "other", label: "Something else" },
],
},
{
id: "why",
type: "longText",
title: "Tell us what it is for",
required: true,
showIf: {
id: "g1",
combinator: "and",
rules: [{ id: "r1", field: "kind", operator: "eq", value: "other" }],
},
},
{ id: "budget", type: "number", title: "Budget", validation: { min: 0 } },
],
},
{
id: "s2",
title: "Approval",
showIf: {
id: "g2",
combinator: "and",
rules: [{ id: "r2", field: "budget", operator: "gt", value: "1000" }],
},
questions: [{ id: "manager", type: "shortText", title: "Who approved it?", required: true }],
},
],
};
export default function Logic() {
return (
<div style={{ width: 760, maxWidth: "100%" }}>
<FormBuilder defaultValue={schema} />
</div>
);
}Preview. Answers are not saved.
import { FormBuilder, type FormAnswers, type FormSchema } from "@forgeron/ui";
import { useState } from "react";
const schema: FormSchema = {
version: 1,
title: "Newsletter",
sections: [
{
id: "s1",
title: "",
questions: [
{
id: "email",
type: "shortText",
title: "Your email",
required: true,
validation: { format: "email" },
},
{
id: "topics",
type: "multiChoice",
title: "Topics",
options: [
{ id: "t1", label: "Product news" },
{ id: "t2", label: "Engineering" },
{ id: "t3", label: "Events" },
],
},
],
},
],
};
export default function Preview() {
const [sent, setSent] = useState<FormAnswers | null>(null);
return (
<div style={{ display: "grid", gap: 12, width: 760, maxWidth: "100%" }}>
<FormBuilder defaultValue={schema} defaultMode="preview" onPreviewSubmit={setSent} />
{sent ? (
<pre style={{ margin: 0, font: "12px/18px var(--font-mono)" }}>{JSON.stringify(sent)}</pre>
) : null}
</div>
);
}API → Props
| Prop | Type | Default | Description |
|---|---|---|---|
value / defaultValue | FormSchema | — | The form: sections of questions, with options, limits and show-if rules. Controlled or not; `emptyForm()` is a blank one. |
onChange | (schema: FormSchema) => void | — | After every edit. |
mode / defaultMode | "edit" | "preview" | "edit" | The Edit / Preview switch. The preview is the real FormRenderer; nothing is saved. |
onModeChange | (mode) => void | — | When the switch moves. |
onPreviewSubmit | (answers: FormAnswers) => void | — | Hears the answers sent in the preview. |
types | QuestionType[] | — | The types the add menu offers; all fifteen by default. |
countries | { value, label }[] | — | Countries offered by address questions in the preview. |
label | string | "Form builder" | Names the builder for assistive technology. |
className | string | — | Extra classes. |