Smart Form

A powerful form component with built-in validation, mutation handling, and TypeScript support using Zod schemas and TanStack Query.

Installation

Install the Smart Form component using your preferred package manager.

npxshadcn@latest addhttps://rigidui.com/registry/smart-form

Usage

The Smart Form component combines React Hook Form, Zod validation, and TanStack Query mutations to create a complete form solution with minimal boilerplate.

Live Preview

Your full name

Check if the user should be active

Optional biography

Discover Its 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.

Components

The Smart Form consists of the following components:

SmartForm

The main form component that provides form management with built-in validation, submission handling, and TanStack Query integration. It wraps the entire form and provides a render prop pattern for building form fields.

Props

PropTypeDefaultDescription
schemaz.ZodSchema<T>RequiredZod schema for form validation and type safety
mutationFn(data: T) => Promise<any>RequiredAsync function to handle form submission (API call)
queryKeystring[][]TanStack Query key for cache invalidation after successful submission
mode'create' | 'edit''create'Form mode that affects submit button text and behavior
defaultValuesPartial<T>undefinedDefault values for form fields
onSuccess(data: any) => voidundefinedCallback function called on successful form submission
onError(error: Error) => voidundefinedCallback function called when form submission fails
submitTextstringundefinedCustom text for submit button (overrides mode-based text)
classNamestring''Additional CSS classes for the form container
children(form: UseFormReturn<T>) => React.ReactNodeRequiredRender function that receives the form instance for building form fields

SmartFormField

A versatile field component that renders different input types based on the type prop. Supports text, email, password, number, textarea, select, checkbox, radio, and color fields with automatic validation integration.

Props

PropTypeDefaultDescription
formUseFormReturn<T>RequiredReact Hook Form instance passed from SmartForm
nameFieldPath<T>RequiredField name that matches the schema property
type'text' | 'email' | 'password' | 'number' | 'textarea' | 'select' | 'checkbox' | 'radio' | 'color'RequiredInput type that determines the rendered field component
labelstringundefinedLabel text displayed above the field
placeholderstringundefinedPlaceholder text for input fields
descriptionstringundefinedHelp text displayed below the field
optionsFormFieldOption[][]Options for select and radio field types
disabledbooleanfalseWhether the field is disabled
classNamestring''Additional CSS classes for the field container

FormSection

A layout component for organizing form fields into logical groups with optional titles and descriptions. Helps create well-structured forms by separating different sections of information.

Props

PropTypeDefaultDescription
titlestringRequiredThe title text displayed for the form section
descriptionstringundefinedOptional description text displayed below the title
childrenReact.ReactNodeRequiredForm fields and other content to display in the section
classNamestring''Additional CSS classes for styling the section

ConditionalField

A utility component that conditionally renders child components based on the value of a watched form field. Useful for creating dynamic forms where certain fields appear based on user selections.

Props

PropTypeDefaultDescription
formUseFormReturn<T>RequiredReact Hook Form instance passed from SmartForm
whenFieldPath<T>RequiredThe field name to watch for changes
equalsanyRequiredThe value to compare against the watched field
childrenReact.ReactNodeRequiredContent to render when the condition is met