Smart Form
A flexible grid/list component that provides seamless switching between grid and list layouts with customizable card rendering.
Usage
import { SmartForm, SmartFormField } from "@/components/smart-form"
import { z } from "zod"
const demoSchema = z.object({
name: z.string().min(2, "Name must be at least 2 characters"),
email: z.string().email("Invalid email address"),
role: z.enum(["admin", "user", "moderator"]),
adminCode: z.string().optional(),
isActive: z.boolean(),
bio: z.string().optional(),
})
type DemoFormData = z.infer<typeof demoSchema>
export default function MyForm() {
const mockMutationFn = async (data: DemoFormData) => {
await new Promise(resolve => setTimeout(resolve, 1000))
console.log('Form submitted:', data)
return { success: true, data }
}
return (
<div className="max-w-md">
<SmartForm
schema={demoSchema}
mutationFn={mockMutationFn}
defaultValues={{
isActive: false,
}}
onSuccess={(data) => {
console.log("Success:", data);
}}
onError={(error) => {
console.error("Error:", error);
}}
>
{(form) => (
<>
<SmartFormField
form={form}
name="name"
type="text"
label="Name"
placeholder="Enter your name"
description="Your full name"
/>
<SmartFormField
form={form}
name="email"
type="email"
label="Email"
placeholder="Enter your email"
/>
<SmartFormField
form={form}
name="role"
type="select"
label="Role"
options={[
{ value: "admin", label: "Admin" },
{ value: "user", label: "User" },
{ value: "moderator", label: "Moderator" },
]}
/>
<SmartFormField
form={form}
name="isActive"
type="checkbox"
label="Active user"
description="Check if the user should be active"
/>
<SmartFormField
form={form}
name="bio"
type="textarea"
label="Bio"
placeholder="Tell us about yourself..."
description="Optional biography"
/>
</>
)}
</SmartForm>
</div>
)
}
Installation
Install the Smart Form component using your preferred package manager.
npx shadcn@latest add https://rigidui.com/r/smart-form.json
Features
The Smart Form component is packed with powerful features designed to provide an exceptional user experience.
Zod Schema Validation
Built-in integration with Zod for runtime type safety and comprehensive validation rules.
TanStack Query Integration
Seamless integration with TanStack Query for mutations, loading states, and cache invalidation.
Multiple Field Types
Support for text, email, password, number, textarea, select, checkbox, radio, and color field types.
TypeScript Support
Full TypeScript support with type inference from Zod schemas for complete type safety.
Props
SmartForm
Prop | Type | Default |
---|---|---|
schema | z.ZodSchema<T> | undefined |
mutationFn | (data: T) => Promise<any> | undefined |
children | (form: UseFormReturn<T>) => React.ReactNode | undefined |
queryKey? | string[] | [] |
mode? | 'create' | 'edit' | 'create' |
defaultValues? | Partial<T> | undefined |
onSuccess? | (data: any) => void | undefined |
onError? | (error: Error) => void | undefined |
submitText? | string | undefined |
className? | string | '' |
SmartFormField
Prop | Type | Default |
---|---|---|
form | UseFormReturn<T> | undefined |
name | FieldPath<T> | undefined |
type | 'text' | 'email' | 'password' | 'number' | 'textarea' | 'select' | 'checkbox' | 'radio' | 'color' | undefined |
label? | string | undefined |
placeholder? | string | undefined |
description? | string | undefined |
options? | FormFieldOption[] | [] |
disabled? | boolean | false |
className? | string | '' |
ConditionalField
Prop | Type | Default |
---|---|---|
form | UseFormReturn<T> | undefined |
when | FieldPath<T> | undefined |
equals | any | undefined |
children | React.ReactNode | undefined |
FormSection Props
Prop | Type | Default |
---|---|---|
title | string | undefined |
children | React.ReactNode | undefined |
description? | string | undefined |
className? | string | '' |