Form Component
Declarative form component with built-in Zod validation
The Form component (also exported as AdminForm) is the foundational form component. It provides a declarative API over the field type system, built-in Zod validation, and a typed submit handler (onSubmitValues) that plugs straight into a React Query mutation.
Semantic Wrappers
For specific use cases, use CreateForm for creation pages or EditForm for edit pages. They provide better semantics and TypeScript enforcement.
Basic Usage
'use client'
import { Form } from '@telamonjs/ui'
import { z } from 'zod'
import { usersApi } from '@/lib/api/users-client'
const validationSchema = z.object({
name: z.string().min(1, 'Required'),
email: z.string().email('Invalid email'),
})
export function UserForm() {
// Receives the typed, validated values; return { success, data?, message? }
async function handleSubmit(values: Record<string, unknown>) {
const user = await usersApi.create(values)
return { success: true, data: user }
}
return (
<Form
sections={[
{
label: 'User Info',
fields: [
{
name: 'name',
label: 'Name',
type: 'text',
required: true,
},
{
name: 'email',
label: 'Email',
type: 'email',
required: true,
},
],
},
]}
onSubmitValues={handleSubmit}
validation={validationSchema}
/>
)
}Props
Prop
Type
Field Types
The type of each field picks its renderer:
Text & Input
'text'- Simple text input ('string'is an alias)'email'- Email input with validation'password'- Password input'number'- Integer input'float'- Decimal input'currency'- Currency input'textarea'- Multi-line text'masked'- Input with mask (e.g., phone)
Boolean
'boolean'- Switch component'checkbox'- Checkbox (same as boolean)
Date & Time
'date'- Date picker'datetime'- Date + time picker'masked-date'- Masked date input'masked-datetime'- Masked date + time input'time'- Time picker'daterange'- Date range picker'month'- Month/year picker'week'- Week picker
Select & Relationship
'relationship'- Select dropdown (use this, not 'select')'multiselect'- Multiple select
Files
'image'- Image upload'file'- File upload'fileupload'- Advanced file upload
HTML5 Native
'url'- URL input'tel'- Phone input'color'- Color picker'range'- Slider'search'- Search input
Advanced
'richtext'- WYSIWYG editor'wizard'- Multi-step form
Special
'component'- Custom component'crud'- Nested CRUD'svg'- SVG/icon'space'- Spacer'hidden'- Hidden field
Examples
Simple Contact Form
<Form
sections={[
{
label: 'Contact Information',
fields: [
{ name: 'name', label: 'Name', type: 'text', required: true },
{ name: 'email', label: 'Email', type: 'email', required: true },
{ name: 'message', label: 'Message', type: 'textarea', required: true },
],
},
]}
onSubmitValues={sendMessage}
/>Form with Validation
const schema = z.object({
name: z.string().min(2, 'Min 2 characters'),
email: z.string().email('Invalid email'),
age: z.number().min(18, 'Must be 18+'),
})
<Form
sections={formSections}
onSubmitValues={createUser}
validation={schema}
/>Form with Initial Data
<Form
sections={formSections}
data={{
name: 'John Doe',
email: 'john@example.com',
}}
onSubmitValues={updateUser}
/>Form Sections
Group related fields in sections for better UX:
const sections: FormSection[] = [
{
label: 'Personal Information',
description: 'Basic user details',
fields: [
{ name: 'name', label: 'Name', type: 'text' },
{ name: 'email', label: 'Email', type: 'email' },
],
},
{
label: 'Address',
fields: [
{ name: 'street', label: 'Street', type: 'text' },
{ name: 'city', label: 'City', type: 'text' },
],
},
]Submitting
onSubmitValues is the default submit path. It receives the validated values as a plain
object (booleans and numbers keep their types) and resolves a FormActionResult:
'use client'
import { useMutation } from '@tanstack/react-query'
import { Form, type FormActionResult } from '@telamonjs/ui'
export function NewUserForm({ onDone }: { onDone: () => void }) {
const create = useMutation({ mutationFn: usersApi.create })
async function handleSubmit(values: Record<string, unknown>): Promise<FormActionResult> {
const user = await create.mutateAsync(values as UserFormData)
return { success: true, data: user }
}
return <Form sections={sections} validation={schema} onSubmitValues={handleSubmit} onSuccess={onDone} />
}{ success: true }→onSuccessruns; without it,cancelAction; without either, the browser goes back.{ success: false, message }→ the message is shown as a toast and inline error.- A thrown error is caught and shown the same way.
action is the alternative for a Next.js Server Action: it receives FormData instead of
typed values (booleans become 'true'/'false', nested objects use dot-notation keys).
Use one or the other, never both.
Related Components
- CreateForm - Wrapper for creation forms
- EditForm - Wrapper for edit forms
- ZustandForm - Multi-step wizards
Best Practices
- Use sections for better UX (group related fields)
- Always validate with Zod schemas
- Resolve
{ success, data?, message? }fromonSubmitValues - Use
'relationship'for selects (never'select') - Prefer CreateForm/EditForm for semantic clarity
Next Steps
Learn about CreateForm and EditForm for better TypeScript enforcement.