@matthew.ngo/reform
Version:
A flexible and powerful React form management library with advanced validation, state observation, and multi-group support
135 lines (109 loc) • 3.76 kB
Markdown
- [Introduction](
- [Core Concepts](
- [Getting Started](
- [API Reference](
- [useReform Hook](
- [Form Groups](
- [Field Management](
- [Validation](
- [Error Handling](
- [Form State Management](
- [Advanced Features](
- [Integration Guides](
- [Examples](
- [Best Practices](
Reform is a powerful form management library for React applications that provides a flexible and type-safe way to handle complex forms with multiple groups, validation, and advanced features.
Reform simplifies the creation and management of complex forms by providing:
- Type-safe form handling with TypeScript
- Support for form groups and nested data structures
- Comprehensive validation system with Yup integration
- Advanced error handling and custom error renderers
- Form state persistence and auto-save functionality
- Multi-step form wizards with step validation
- Conditional fields and dynamic validation rules
Reform organizes form data into groups, allowing you to manage collections of related data. Each group contains:
- A unique identifier
- Form data of type T
- Metadata for tracking state
Reform uses a type-safe approach to field paths, ensuring that you can only reference valid fields in your form data structure.
Reform provides multiple validation strategies:
- Schema-based validation with Yup
- Dynamic validation rules based on field values
- Context-aware validation that can access other form data
Reform includes a comprehensive error handling system with:
- Custom error renderers
- Error transformation
- Error boundaries for form components
```bash
npm install @reform/core
yarn add @reform/core
```
```tsx
import { useReform } from '@reform/core';
import * as yup from 'yup';
// Define your form data type
type UserForm = {
name: string;
email: string;
age: number;
};
// Create a validation schema
const userSchema = yup.object({
name: yup.string().required('Name is required'),
email: yup.string().email('Invalid email').required('Email is required'),
age: yup.number().min(18, 'Must be at least 18 years old')
});
// Use the Reform hook
const UserFormComponent = () => {
const form = useReform<UserForm>({
defaultData: { name: '', email: '', age: 0 },
minGroups: 1,
groupSchema: userSchema
});
const handleSubmit = form.formMethods.handleSubmit((data) => {
console.log('Form submitted:', data);
});
return (
<form onSubmit={handleSubmit}>
{form.getGroups().map((group, index) => (
<div key={group.id}>
<input
{...form.register(index, 'name')}
placeholder="Name"
/>
{form.getFieldError(index, 'name') && (
<p>{form.getFieldError(index, 'name')}</p>
)}
<input
{...form.register(index, 'email')}
placeholder="Email"
/>
{form.getFieldError(index, 'email') && (
<p>{form.getFieldError(index, 'email')}</p>
)}
<input
type="number"
{...form.register(index, 'age')}
placeholder="Age"
/>
{form.getFieldError(index, 'age') && (
<p>{form.getFieldError(index, 'age')}</p>
)}
</div>
))}
<button type="submit">Submit</button>
</form>
);
};