# Form Validation & Input Hooks Comprehensive collection of production-ready React hooks for managing form state, validation, and input handling. ## ๐ฏ Overview 5 powerful, fully-typed hooks for building complex forms with validation, state management, and error handling. **Location**: `/hooks/` ## ๐ฆ Available Hooks ### 1. **useValidation** - Schema-Based Form Validation - Generic schema-based validation - Field-level error tracking - Bulk form validation - Manual error management **File**: `useValidation.ts` (198 lines) ```typescript const { errors, isValid, validate, validateField } = useValidation(schema) ``` ### 2. **useInput** - Controlled Text Input State - Dirty/touched tracking - Optional validation on blur - Value transformation - Auto-reset on error **File**: `useInput.ts` (199 lines) ```typescript const { value, error, handlers } = useInput('', { onValidate }) ``` ### 3. **useCheckbox** - Checkbox State Management - Single checkbox OR checkbox groups - Indeterminate state detection - Check all/uncheck all operations - Count tracking **File**: `useCheckbox.ts` (418 lines) ```typescript // Single const { checked, handlers } = useCheckbox(false) // Multiple const { values, handlers, isAllChecked } = useCheckbox({ read: false, write: false }) ``` ### 4. **useSelect** - Select Dropdown State - Single select OR multi-select - Search/filter options - Clearable selections - Option label tracking **File**: `useSelect.ts` (467 lines) ```typescript // Single const { value, handlers } = useSelect('option1', { options }) // Multiple const { values, handlers, count } = useSelect([], { options, isMulti: true }) ``` ### 5. **useFieldArray** - Dynamic Form Arrays - Add/remove/reorder fields - Min/max constraints - Field-level validation - Array-like methods (push, pop, shift, unshift) **File**: `useFieldArray.ts` (484 lines) ```typescript const { fields, handlers, canAdd, canRemove } = useFieldArray([], { maxFields: 5 }) ``` ## ๐ Quick Start ### Installation All hooks are included in the project - no additional installation needed. ### Import ```typescript import { useValidation } from '@/hooks/useValidation' import { useInput } from '@/hooks/useInput' import { useCheckbox } from '@/hooks/useCheckbox' import { useSelect } from '@/hooks/useSelect' import { useFieldArray } from '@/hooks/useFieldArray' ``` ### Basic Example ```typescript 'use client' import { useInput, useSelect, useCheckbox } from '@/hooks' export const ContactForm = () => { const name = useInput('', { onValidate: (v) => v.length >= 2 ? '' : 'Min 2 chars' }) const country = useSelect('us', { options: [ { value: 'us', label: 'USA' }, { value: 'uk', label: 'UK' } ] }) const subscribe = useCheckbox(false) return (
) } ``` ## ๐ Documentation | Document | Purpose | |----------|---------| | **FORM_VALIDATION_HOOKS.md** | Complete API reference for all 5 hooks | | **QUICK_REFERENCE.md** | Quick start guide with 30+ code examples | | **EXPORT_GUIDE.md** | Import methods, type exports, troubleshooting | | **README.md** | This file | ## โจ Features ### All Hooks Provide - โ State tracking (value, isDirty, isTouched) - โ Error handling with field-level messages - โ Validation (field-level or bulk) - โ Reset to initial state - โ Touch tracking for conditional error display - โ Fully typed with TypeScript - โ useCallback memoization for performance ### Hook-Specific Features | Feature | useValidation | useInput | useCheckbox | useSelect | useFieldArray | |---------|:---:|:---:|:---:|:---:|:---:| | Schema validation | โ | | | | | | Multi-field validation | โ | | | | | | Text input management | | โ | | | | | Value transformation | | โ | | | | | Single checkbox | | | โ | | | | Checkbox groups | | | โ | | | | Indeterminate state | | | โ | | | | Single select | | | | โ | | | Multi-select | | | | โ | | | Search/filter | | | | โ | | | Dynamic arrays | | | | | โ | | Array operations | | | | | โ | | Min/max constraints | | | | | โ | ## ๐จ Use Cases ### Form Building ```typescript const form = { name: useInput(''), email: useInput(''), country: useSelect('us', { options }), agree: useCheckbox(false) } ``` ### Dynamic Lists ```typescript const skills = useFieldArray( [{ name: '' }], { minFields: 1, maxFields: 10 } ) ``` ### Permission Management ```typescript const permissions = useCheckbox({ read: false, write: false, delete: false, admin: false }) ``` ### Multi-Select Filtering ```typescript const tags = useSelect([], { options: allTags, isMulti: true, searchable: true }) ``` ### Cross-Field Validation ```typescript const password = useInput('') const confirm = useInput('', { onValidate: (v) => v !== password.value ? 'Passwords must match' : '' }) ``` ## ๐ง API Summary ### Common Handlers All hooks return `handlers` object with methods: - `onChange()` - Handle change events - `reset()` - Reset to initial state - `touch()` - Mark as touched - `validate()` - Manually validate - `setError()`, `clearError()` - Manual error control ### Common State All hooks return: ```typescript { isDirty: boolean // Changed from initial isTouched: boolean // User interacted error: string // Validation error isValid: boolean // No errors handlers: { ... } // Event handlers } ``` ## ๐ฏ TypeScript Support Full generic type support: ```typescript type FormData = { name: string age: number email: string } const schema: ValidationSchema