Schema Form
A form drawn from a zod schema, with Signet's own controls.
import { z } from "zod"
import { SchemaForm } from "@dapplelight/signet-ui/components/schema-form"
import { save } from "./actions"
const Expense = z.object({
merchant: z.string().min(1),
amount: z.number().positive(),
kind: z.enum(["food", "travel", "software"]),
date: z.date().optional(),
reimbursable: z.boolean().default(false),
note: z.string().max(500).optional(),
})
export function Demo() {
return <SchemaForm schema={Expense} submit="Save expense" onSubmit={save} />
}Installation
npm i @dapplelight/signet-uiUsage
import { SchemaForm } from "@dapplelight/signet-ui/components/schema-form"
import { Expense, save } from "./actions"
<SchemaForm schema={Expense} onSubmit={(values) => save(values)} />Needs the optional peers @autoform/react, @autoform/zod, @tanstack/react-form and zod. Customise a field in the schema with AutoForm's fieldConfig.
Examples
Default values
import { z } from "zod"
import { SchemaForm } from "@dapplelight/signet-ui/components/schema-form"
import { sendInvite } from "./actions"
const Invite = z.object({
email: z.string().email(),
role: z.enum(["admin", "member"]),
})
<SchemaForm
schema={Invite}
submit="Send invite"
defaultValues={{ email: "[email protected]", role: "member" }}
onSubmit={sendInvite}
/>Server errors
import { z } from "zod"
import { SchemaForm } from "@dapplelight/signet-ui/components/schema-form"
import { applyServerErrors } from "@dapplelight/signet-lib/form"
import { api } from "./api"
const Invite = z.object({
email: z.string().email(),
role: z.enum(["admin", "member"]),
})
<SchemaForm
schema={Invite}
submit="Send invite"
onSubmit={async (values) => {
await api.post("/invites", values) // rejects with a 422 envelope
}}
onSubmitError={(error, form) => applyServerErrors(form, error)}
/>API Reference
SchemaForm
| Prop | Type | Default | Description |
|---|---|---|---|
| schema | zod object schema or SchemaProvider | - | A zod object schema, or any AutoForm SchemaProvider. |
| onSubmit | (values, form, event) => void | Promise<void> | - | Runs after the schema validates. |
| onSubmitError | (error: unknown, form: unknown) => void | - | Receives a rejected onSubmit, e.g. for applyServerErrors. Without it the error is rethrown. |
| submit | ReactNode | false | LabelsProvider submit ("Submit") | Submit button text. false hides it so you can render your own in children. |
| defaultValues | Partial<T> | from schema | Initial values merged over schema defaults. |
| values | Partial<T> | - | Controlled values kept in sync with external state. |
| uiComponents | Partial<AutoFormUIComponents> | - | Replace layout pieces: wrappers, error message, submit button. |
| formComponents | Partial<AutoFormFieldComponents> | - | Replace a control for a field type: string, textarea, number, date, boolean, select. |
| children | ReactNode | - | Extra content after the fields, before the submit button. |
withSubmit is not available: use submit. Other AutoForm props pass through.