EditForm
Semantic wrapper for edit forms with required id and data props
Semantic wrapper component for edit forms that enforces id and data props (TypeScript-enforced) and accepts nested records as they come from the API, addressed with dot-notation field names.
Recommended for /[id]/edit pages
EditForm forces you to pass id and data at compile time, preventing
common bugs where forms are missing initial data.
Basic Usage
'use client'
import { use } from 'react'
import { useRouter } from 'next/navigation'
import { useQuery } from '@tanstack/react-query'
import { EditForm } from '@telamonjs/ui'
import { formSections, validationSchema, type UserFormData } from '../config'
import { usersApi } from '@/lib/api/users-client'
export default function EditUserPage({ params }: { params: Promise<{ id: string }> }) {
const { id } = use(params)
const router = useRouter()
const { data: user, isLoading } = useQuery({
queryKey: ['users', id],
queryFn: () => usersApi.get(id),
})
if (isLoading || !user) return null
return (
<EditForm
id={id}
data={user}
sections={formSections}
validation={validationSchema}
// values already include `id`; resolve { success, data? }
onSubmitValues={async values => ({
success: true,
data: await usersApi.update(id, values as Partial<UserFormData>),
})}
onSuccess={() => router.push('/admin/users')}
/>
)
}Props
Same submit contract as Form, with required id and data.
Required Props:
id: string- Entity ID (TypeScript enforced!) — added to the submitted valuesdata: Partial<T>- Entity data (TypeScript enforced!)
Optional Props:
sections?: FormSection[]/fields?: FormField[]- Form layoutonSubmitValues?: (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)validation?: z.ZodType- Zod validation schemasubmitText?: string- Submit button text (default: localized "Save")cancelAction?: () => void- Cancel button handleronSuccess?: (result) => void | Promise<void>- Called after a successful submituseTabs?: boolean- Sections as tabs (default:true)grid,hideButtons,flush,footerClassName,contentClassName,onFieldChange,onDirtyChange- Same asForm
TypeScript Enforcement
EditForm requires both id and data at compile time:
// ✅ GOOD - TypeScript is happy
<EditForm
id={id}
data={user}
sections={formSections}
onSubmitValues={updateUser}
/>
// ❌ ERROR - TypeScript error: Property 'id' is missing
<EditForm
data={user}
sections={formSections}
onSubmitValues={updateUser}
/>
// ❌ ERROR - TypeScript error: Property 'data' is missing
<EditForm
id={id}
sections={formSections}
onSubmitValues={updateUser}
/>This prevents the common bug of forgetting to load data before rendering edit forms.
Nested Data
Pass the record as the API returns it. Field names in dot notation (profile.bio) are
resolved against the nested object by React Hook Form:
const user = {
id: '1',
name: 'John',
profile: { bio: 'Developer', avatar: 'https://...' },
address: { street: '123 Main St', city: 'NYC' },
}
// Field definition
{ name: 'profile.bio', label: 'Bio', type: 'textarea' }When to Use
Use EditForm when:
- ✅ Building edit pages (
/[id]/edit) - ✅ You want TypeScript to enforce
idanddata - ✅ You have nested API data (dot-notation field names)
- ✅ You want semantic clarity (code reads better)
Don't use when:
- ❌ Building creation pages → Use CreateForm
- ❌ Building multi-step wizards → Use ZustandForm
- ❌ Need full control → Use Form
EditForm vs CreateForm vs Form
| Feature | EditForm | CreateForm | Form |
|---|---|---|---|
| Requires id | ✅ Yes (enforced) | ❌ No | ❌ No |
| Requires data | ✅ Yes (enforced) | ❌ No | ⚠️ Optional |
| Filters editOnly | ❌ No | ✅ Yes | ❌ No |
| Use case | Edit only | Create only | Any form |
With Page Wrapper
EditPage wraps EditForm with a page title and description. It takes the FormData
action (not onSubmitValues) — see the Quick Start
for a complete example:
<EditPage id={id} data={user} title={`Edit: ${user.name}`} sections={formSections} validation={validationSchema} action={action} />Best Practices
- Always use for edit pages (TypeScript enforcement prevents bugs)
- Load data before rendering (loading state until the query resolves)
- Handle not found (the record query fails or returns nothing)
- Resolve
{ success, data? }fromonSubmitValues - Invalidate the record and list queries after update (or use
invalidatesinCrudConfig) - Use dot notation for nested fields (
profile.bio)
Related Components
- CreateForm - For creation pages
- Form - Base form component
- EditPage - Page wrapper with layout
See Also
- Module Structure - Organize form configuration
- Form Pattern - Form best practices
- Quick Start - Complete CRUD example