mirror of
https://github.com/johndoe6345789/metabuilder.git
synced 2026-04-24 13:54:57 +00:00
- Moved 104 production-ready hooks from frontends/nextjs and redux packages to root /hooks - Removed duplicate hooks from frontends/nextjs/src/hooks directory - Updated frontends/nextjs to import from @metabuilder/hooks instead of local hooks - Added comprehensive documentation: EXPORT_GUIDE.md, FORM_VALIDATION_HOOKS.md, QUICK_REFERENCE.md - Removed stale index.d.ts from hooks root (TypeScript auto-generates declarations) - All 104 hooks now consolidated as single source of truth at root /hooks - Package @metabuilder/hooks v2.0.0 ready for use across all MetaBuilder projects Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
5.2 KiB
5.2 KiB
Form Validation Hooks - Export & Usage Guide
Location
All hooks are located in /hooks/ directory at project root.
Available Hooks
- useValidation.ts - Schema-based form validation
- useInput.ts - Text input state management
- useCheckbox.ts - Checkbox state management (single/multi)
- useSelect.ts - Select dropdown state (single/multi)
- useFieldArray.ts - Dynamic form field arrays
Import Methods
Method 1: Direct Imports (Recommended)
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'
Method 2: Named Exports (if configured in index)
import {
useValidation,
useInput,
useCheckbox,
useSelect,
useFieldArray,
type ValidationSchema,
type ValidationErrors,
type SelectOption,
type FormField
} from '@/hooks'
Method 3: Wildcard Import
import * as hooks from '@/hooks/useValidation'
const { useValidation } = hooks
Type Exports
Each hook also exports its types:
// useValidation
export type ValidationSchema<T>
export type ValidationErrors<T>
// useSelect
export interface SelectOption<T>
// useFieldArray
export interface FormField<T>
Module Resolution
The hooks work with TypeScript path aliases:
@/hooks/useValidation- Full path import@/hooks/useInput- Full path import- etc.
Ensure your tsconfig.json or jsconfig.json has:
{
"compilerOptions": {
"baseUrl": ".",
"paths": {
"@/*": ["./*"]
}
}
}
React Requirements
All hooks require:
- React 16.8+ (for hooks support)
- React 18+ (recommended for concurrent features)
Dependencies:
react- Corereact-redux(optional) - For integration with Reduxnanoid- Required byuseFieldArrayfor unique IDs
File Summary
| File | Size | Status | Exports |
|---|---|---|---|
| useValidation.ts | 5.6 KB | Production | useValidation, ValidationSchema, ValidationErrors |
| useInput.ts | 4.6 KB | Production | useInput, UseInputReturn |
| useCheckbox.ts | 9.9 KB | Production | useCheckbox, UseCheckboxSingleReturn, UseCheckboxMultiReturn |
| useSelect.ts | 11.2 KB | Production | useSelect, SelectOption, UseSelectSingleReturn, UseSelectMultiReturn |
| useFieldArray.ts | 10.6 KB | Production | useFieldArray, FormField, UseFieldArrayReturn |
Feature Matrix
| Feature | useValidation | useInput | useCheckbox | useSelect | useFieldArray |
|---|---|---|---|---|---|
| State tracking | ✓ | ✓ | ✓ | ✓ | ✓ |
| Dirty tracking | - | ✓ | ✓ | ✓ | ✓ |
| Touched tracking | - | ✓ | ✓ | ✓ | ✓ |
| Error handling | ✓ | ✓ | ✓ | ✓ | ✓ |
| Validation | ✓ | ✓ | ✓ | ✓ | ✓ |
| Search filtering | - | - | - | ✓ | - |
| Multi-value support | - | - | ✓ | ✓ | ✓ |
| Array operations | - | - | - | - | ✓ |
| Min/Max constraints | - | - | - | - | ✓ |
| Group state | - | - | ✓ | ✓ | ✓ |
Usage in Next.js
For Next.js projects, use 'use client' directive:
'use client'
import { useInput } from '@/hooks/useInput'
export const MyComponent = () => {
const email = useInput('')
return <input {...email.handlers.onChange} />
}
API Stability
All hooks follow semantic versioning:
- Stable: All 5 hooks are production-ready
- API version: 1.0.0+
- Breaking changes: Will bump major version
Documentation
- FORM_VALIDATION_HOOKS.md - Complete API reference
- QUICK_REFERENCE.md - Quick start guide with examples
- EXPORT_GUIDE.md - This file
Common Issues
Issue: Import path not found
Solution: Check tsconfig paths configuration
Issue: Type errors with generics
Solution: Explicitly type the hook:
const input = useInput<string>('', options)
Issue: nanoid not installed
Solution: npm install nanoid (required by useFieldArray)
Testing
All hooks are designed to be testable:
import { render, screen, fireEvent } from '@testing-library/react'
import { useInput } from '@/hooks/useInput'
const TestComponent = () => {
const input = useInput('')
return <input {...input.handlers.onChange} />
}
test('tracks dirty state', () => {
const { rerender } = render(<TestComponent />)
const input = screen.getByRole('textbox')
fireEvent.change(input, { target: { value: 'test' } })
// Test isDirty state
})
Performance Notes
All hooks are optimized with:
useCallbackfor handler memoization- Conditional state updates
- Efficient error state management
- No unnecessary re-renders
Browser Support
All hooks work in:
- Chrome 70+
- Firefox 63+
- Safari 12+
- Edge 79+
Future Enhancements
Potential additions:
- Async validation support
- Debounced validation
- Cross-field validation helpers
- Form context provider
- Redux integration helpers
Contributing
When modifying hooks:
- Update types in the hook file
- Update tests
- Update documentation
- Maintain backward compatibility
- Follow existing code style
License
Same as main project