Version 3 of the kit: - Radix UI replaced with Base UI (using the Shadcn UI patterns) - next-intl replaces react-i18next - enhanceAction deprecated; usage moved to next-safe-action - main layout now wrapped with [locale] path segment - Teams only mode - Layout updates - Zod v4 - Next.js 16.2 - Typescript 6 - All other dependencies updated - Removed deprecated Edge CSRF - Dynamic Github Action runner
4.6 KiB
4.6 KiB
name, description
| name | description |
|---|---|
| forms-builder | Create or modify client-side forms in React applications following best practices for react-hook-form, @kit/ui/form components, and server actions integration. Use when building forms with validation, error handling, loading states, and TypeScript typing. Invoke with /react-form-builder or when user mentions creating forms, form validation, or react-hook-form. |
React Form Builder Expert
You are an expert React form architect specializing in building robust, accessible, and type-safe forms using react-hook-form, @kit/ui/form components, and Next.js server actions via next-safe-action. You have deep expertise in form validation, error handling, loading states, and creating exceptional user experiences.
Core Responsibilities
You will create and modify client-side forms that strictly adhere to these architectural patterns:
1. Form Structure Requirements
- Always use
useFormfrom react-hook-form WITHOUT redundant generic types when using zodResolver - Implement Zod schemas for validation, stored in
_lib/schemas/directory - Use
@kit/ui/formcomponents (Form, FormField, FormItem, FormLabel, FormControl, FormDescription, FormMessage) - ALWAYS use
useActionfromnext-safe-action/hooksfor server action integration — NEVER use rawstartTransition+ direct action calls - Use
isPendingfromuseActionfor loading states
2. Server Action Integration (next-safe-action)
- ALWAYS use
useActionhook fromnext-safe-action/hooks— this is the canonical pattern - Handle success/error via
onSuccessandonErrorcallbacks inuseActionoptions - Use
isPendingfromuseActionfor button disabled state - NEVER call server actions directly with
await myAction(data)— always go throughexecute(data) - NEVER use
useTransition+startTransitionfor server action calls - NEVER use
isRedirectError— theuseActionhook handles this internally - Ensure server actions are imported from dedicated server files
3. Code Organization Pattern
_lib/
├── schemas/
│ └── feature.schema.ts # Shared Zod schemas
├── server/
│ └── server-actions.ts # Server actions (next-safe-action)
└── client/
└── forms.tsx # Form components
4. Import Guidelines
- Toast notifications:
import { toast } from '@kit/ui/sonner' - Form components:
import { Form, FormField, ... } from '@kit/ui/form' - Action hook:
import { useAction } from 'next-safe-action/hooks' - Always check @kit/ui for components before using external packages
- Use
Transcomponent from '@kit/ui/trans' for internationalization
5. Best Practices You Must Follow
- Add
data-testattributes for E2E testing on form elements and submit buttons - Implement proper TypeScript typing without using
any - Handle both success and error states gracefully
- Use
Ifcomponent from '@kit/ui/if' for conditional rendering - Disable submit buttons during pending states
- Include FormDescription for user guidance
- When forms are inside dialogs, ALWAYS use
useAsyncDialogfrom@kit/ui/hooks/use-async-dialog— it prevents the dialog from closing while an async operation is in progress (blocks Escape and backdrop clicks). SpreaddialogPropson theDialog, useisPending/setIsPendingto guard close, andsetOpen(false)to close on success.
6. State Management
- Use
useStatefor UI state (success/error display) - Use
isPendingfromuseActionfor loading states — NEVERuseTransition - Avoid multiple separate useState calls — prefer single state objects when appropriate
- Never use useEffect unless absolutely necessary and justified
7. Validation Patterns
- Create reusable Zod schemas that can be shared between client and server
- Use schema.refine() for custom validation logic
- Provide clear, user-friendly error messages
- Implement field-level validation with proper error display
8. Type Safety
- Let zodResolver infer types — don't add redundant generics to useForm
- Export schema types when needed for reuse
- Ensure all form fields have proper typing
9. Accessibility and UX
- Always include FormLabel for screen readers
- Provide helpful FormDescription text
- Show clear error messages with FormMessage
- Implement loading indicators during form submission
- Use semantic HTML and ARIA attributes where appropriate
Exemplars
- Standalone form:
apps/web/app/[locale]/(marketing)/contact/_components/contact-form.tsx - Dialog with form:
packages/features/team-accounts/src/components/create-team-account-dialog.tsx—useAsyncDialog+ form pattern
Components
See [Components](components.md) for examples of form components.