CreateForm
Semantic wrapper for creation forms with automatic editOnly filtering
Semantic wrapper component for creation forms that automatically filters out editOnly fields.
Recommended for creation pages
CreateForm provides better semantics and TypeScript clarity for creation
pages compared to using Form directly.
Basic Usage
'use client'
import { useRouter } from 'next/navigation'
import { CreateForm } from '@telamonjs/ui'
import { formSections, validationSchema, type UserFormData } from './config'
import { usersApi } from '@/lib/api/users-client'
export function AddUserForm() {
const router = useRouter()
return (
<CreateForm
sections={formSections}
validation={validationSchema}
onSubmitValues={async values => ({
success: true,
data: await usersApi.create(values as UserFormData),
})}
onSuccess={() => router.push('/admin/users')}
/>
)
}Props
Same submit contract as Form.
Key Props:
sections?: FormSection[]- Form sections (dropseditOnlysections, andeditOnlyortype: 'crud'fields)fields?: FormField[]- Flat field list, filtered the same way (alternative tosections)onSubmitValues?: (values: Record<string, unknown>) => Promise<FormActionResult<T> | void>- Default submit path: typed values, ideal for a React Query mutationaction?: (formData: FormData) => Promise<FormActionResult<T> | void>- Alternative: a Server Action receivingFormData(mutually exclusive withonSubmitValues)defaultValues?: Partial<T>- Initial valuesvalidation?: z.ZodType- Zod validation schemasubmitText?: string- Submit button text (default: localized "Create")cancelAction?: () => void- Cancel button handleronSuccess?: (result) => void | Promise<void>- Called after a successful submituseTabs?: boolean- Sections as tabs (default:falsefor creation)grid,hideButtons,flush,onFieldChange,onDirtyChange- Same asForm
Automatic Filtering
CreateForm automatically filters out fields and sections marked with editOnly: true, and fields of type: 'crud' (a nested CRUD needs a saved parent):
export const formSections: FormSection[] = [
{
label: 'Basic Info',
fields: [
{
name: 'name',
label: 'Name',
type: 'text',
required: true,
},
{
name: 'createdAt',
label: 'Created At',
type: 'date',
editOnly: true, // ✅ Hidden in CreateForm, shown in EditForm
},
],
},
]Result:
CreateForm→ Shows onlynamefieldEditForm→ Shows bothnameandcreatedAtfields
When to Use
Use CreateForm when:
- ✅ Building creation pages (
/new,/add) - ✅ You want semantic clarity (code reads better)
- ✅ You have fields that should only appear on edit (timestamps, IDs, etc.)
Don't use when:
- ❌ Building edit pages → Use EditForm
- ❌ Building multi-step wizards → Use ZustandForm
- ❌ Need full control → Use Form
CreateForm vs Form
| Feature | CreateForm | Form |
|---|---|---|
| Filters editOnly | ✅ Yes | ❌ No |
Filters crud | ✅ Yes | ❌ No |
| Semantic clarity | ✅ High | ⚠️ Medium |
| TypeScript hints | ✅ Better | ⚠️ Generic |
| Use case | Creation only | Any form |
With Page Wrapper
CreatePage wraps CreateForm with a page title and description. It takes the FormData
action (not onSubmitValues) — see the Quick Start
for a complete example:
<CreatePage title='New User' sections={formSections} validation={validationSchema} action={action} />Best Practices
- Always use for creation pages (better semantics)
- Mark edit-only fields with
editOnly: truein config - Resolve
{ success, data? }fromonSubmitValues - Invalidate the list query after creation (React Query
invalidateQueries, orinvalidatesinCrudConfig) - Redirect to the list page in
onSuccess
Related Components
- EditForm - For edit pages
- Form - Base form component
- CreatePage - Page wrapper with layout
See Also
- Module Structure - Organize form configuration
- Form Pattern - Form best practices
- Quick Start - Complete CRUD example