Guides
Forms
Schema-driven multi-field collection with useForm and the Form component.
Forms collect a set of named fields with validation. Each field has a prompt
(text, or a function of the data so far), optional options for choice fields,
and an optional validate (a function returning true | string, or a
Zod-compatible schema).
useForm
import { useForm } from "@teactjs/core";
import { Message } from "@teactjs/ui";
function Signup() {
const form = useForm({
name: {
prompt: "What's your name?",
validate: (v) => v.trim().length > 1 || "Too short",
},
email: {
prompt: "Your email?",
validate: (v) => /\S+@\S+\.\S+/.test(v) || "Invalid email",
},
region: { prompt: "Pick a region:", options: [["Kanto", "Johto"]] },
});
return form.render(() => (
<Message text={`${form.data.name} <${form.data.email}> — ${form.data.region}`} />
));
}form.render(onComplete) renders the current field and calls onComplete when
all fields are collected. Read answers from form.data; check form.complete;
navigate with form.back(), form.goTo(key), and form.reset().
Form component
import { Form } from "@teactjs/core";
import { Message } from "@teactjs/ui";
// With the hook (recommended):
<Form value={form}>
<Form.Complete>
<Message text={`Hello ${form.data.name}`} />
</Form.Complete>
</Form>;
// Self-contained (fields as children):
<Form>
<Form.Field name="name" prompt="Name?" />
<Form.Field name="region" prompt="Region?" options={[["Kanto", "Johto"]]} />
<Form.Complete>
{(form) => <Message text={`${form.data.name} from ${form.data.region}`} />}
</Form.Complete>
</Form>;Forms vs conversations
- Conversations — a linear wizard, one question at a time; great for chat UX and imperative async flows.
- Forms — a fixed schema of related fields with per-field validation and a
structured
dataobject at the end.
Combine with Storage to persist drafts or results.
Do / Don't
- Do define each field with
prompt(+ optionaloptions/validate) — not atype/requiredshape. - Do return
form.render(() => <Done />)so the current field and completion are handled for you. - Don't read
form.data.fieldbeforeform.complete— the value isn't set until the field is answered. - Don't hand-concatenate user input into HTML — let
<Message>formatting escape it (see Components).