Created a comprehensive form components library with built-in validation, error handling, and accessibility features for the mentorship platform.
-
FormField (
src/components/forms/FormField.tsx)- Wrapper component with label and error display
- Automatic ARIA attributes management
- Support for hints and required indicators
- Error announcements with
role="alert"
-
TextInput (
src/components/forms/TextInput.tsx)- Multiple input types (text, email, password, tel, url, number)
- Validation state styling
- Left/right icon support
- Disabled and readonly states
-
TextArea (
src/components/forms/TextArea.tsx)- Multi-line text input
- Character count display
- Resizable with custom rows
- Max length validation
-
Select (
src/components/forms/Select.tsx)- Dropdown with search functionality
- Keyboard navigation (Arrow keys, Enter, Escape)
- Custom option rendering
- Disabled options support
- Click-outside to close
-
Checkbox (
src/components/forms/Checkbox.tsx)- Standard checkbox with label
- Indeterminate state support
- Disabled state
- Custom styling
-
RadioButton & RadioGroup (
src/components/forms/RadioButton.tsx)- Individual radio buttons
- RadioGroup for managing multiple options
- Horizontal/vertical orientation
- Keyboard navigation
-
DatePicker (
src/components/forms/DatePicker.tsx)- Calendar interface
- Month navigation
- Min/max date constraints
- Timezone support
- Date range validation
-
FileUpload (
src/components/forms/FileUpload.tsx)- Drag-and-drop support
- File size validation
- File type validation
- Multiple file support
- File preview with remove option
- Visual feedback for drag state
-
FormWizard (
src/components/forms/FormWizard.tsx)- Multi-step form management
- Progress indicator
- Step validation
- Navigation controls (Next, Back, Complete)
- Step completion tracking
- Form state management
- Field registration with validation rules
- Real-time validation (onChange, onBlur, onSubmit modes)
- Error handling and display
- Submission state tracking (idle, loading, success, error)
- Form reset functionality
- Field validation logic
- Pre-defined patterns (email, phone, URL)
- File size formatting
- Custom validation functions
- Support for:
- Required fields
- Min/max length
- Pattern matching
- Min/max values
- Custom validators
- ValidationRule
- FieldError
- FormFieldState
- FormState
- SubmissionState
- FormConfig
- SelectOption
- FileUploadConfig
- WizardStep
Checkbox.test.tsx- Checkbox component testsTextInput.test.tsx- Text input testsTextArea.test.tsx- Text area testsFormField.test.tsx- Form field wrapper testsSelect.test.tsx- Select dropdown testsRadioButton.test.tsx- Radio button testsDatePicker.test.tsx- Date picker testsFileUpload.test.tsx- File upload testsFormWizard.test.tsx- Form wizard testsuseForm.test.ts- Form hook testsvalidation.test.ts- Validation utility tests
- User interaction tests
- Validation logic tests
- Accessibility compliance tests
- Keyboard navigation tests
- Error handling tests
- State management tests
-
ContactForm (
src/components/forms/examples/ContactForm.tsx)- Simple contact form with validation
- Demonstrates basic form usage
- Shows submission states
-
RegistrationWizard (
src/components/forms/examples/RegistrationWizard.tsx)- Multi-step registration flow
- Personal info, account type, preferences
- Step validation
-
FormShowcase (
src/components/forms/examples/FormShowcase.tsx)- Comprehensive demo of all form components
- Shows all features and configurations
- Complete working example
aria-invalidfor error statesaria-describedbyfor hints and errorsaria-requiredfor required fieldsaria-labelfor icon buttonsrole="alert"for error messagesrole="listbox"for select optionsrole="radiogroup"for radio groups
- Tab navigation through all form elements
- Arrow keys for select and radio navigation
- Enter/Space for selection
- Escape to close dropdowns
- Focus management and trapping
- Descriptive labels
- Error announcements
- State changes announced
- Proper semantic HTML
- onChange mode: Validate as user types
- onBlur mode: Validate when field loses focus
- onSubmit mode: Validate on form submission
- Re-validation on subsequent changes
- Required fields
- String length constraints
- Pattern matching (regex)
- Numeric range validation
- Custom validation functions
- Async validation support
- Field-level error messages
- Form-level error state
- Error clearing
- Custom error messages
idle: Ready to submitloading: Submission in progresssuccess: Submission completederror: Submission failed
- Async submission support
- Loading indicators
- Success/error feedback
- Form reset after submission
import { useForm } from './hooks/useForm';
import { FormField, TextInput, Select } from './components/forms';
import { emailPattern } from './utils/validation.utils';
const MyForm = () => {
const { register, handleSubmit, formState, submissionState } = useForm({
mode: 'onBlur'
});
const onSubmit = async (data) => {
await api.submitForm(data);
};
return (
<form onSubmit={handleSubmit(onSubmit)}>
<FormField
label="Email"
name="email"
required
error={formState.email?.error}
>
<TextInput
{...register('email', {
required: 'Email is required',
pattern: emailPattern
})}
type="email"
hasError={!!formState.email?.error}
/>
</FormField>
<button
type="submit"
disabled={submissionState === 'loading'}
>
{submissionState === 'loading' ? 'Submitting...' : 'Submit'}
</button>
</form>
);
};src/
├── components/
│ └── forms/
│ ├── FormField.tsx
│ ├── TextInput.tsx
│ ├── TextArea.tsx
│ ├── Select.tsx
│ ├── Checkbox.tsx
│ ├── RadioButton.tsx
│ ├── DatePicker.tsx
│ ├── FileUpload.tsx
│ ├── FormWizard.tsx
│ ├── index.ts
│ ├── README.md
│ ├── __tests__/
│ │ ├── Checkbox.test.tsx
│ │ ├── DatePicker.test.tsx
│ │ ├── FileUpload.test.tsx
│ │ ├── FormField.test.tsx
│ │ ├── FormWizard.test.tsx
│ │ ├── RadioButton.test.tsx
│ │ ├── Select.test.tsx
│ │ ├── TextArea.test.tsx
│ │ ├── TextInput.test.tsx
│ │ ├── useForm.test.ts
│ │ └── validation.test.ts
│ └── examples/
│ ├── ContactForm.tsx
│ ├── FormShowcase.tsx
│ └── RegistrationWizard.tsx
├── hooks/
│ └── useForm.ts
├── types/
│ └── forms.types.ts
└── utils/
└── validation.utils.ts
✅ FormField wrapper with label and error display ✅ TextInput component with validation states ✅ Select dropdown with search functionality ✅ Checkbox and Radio button components ✅ DatePicker with timezone support ✅ FileUpload with drag-and-drop ✅ FormWizard for multi-step forms ✅ Real-time validation with error messages ✅ Form submission states (loading, success, error) ✅ Comprehensive accessibility features ✅ Full test coverage ✅ TypeScript support ✅ Keyboard navigation ✅ Screen reader support
- Run tests to verify all components work correctly
- Import and use components in your application
- Customize styling to match your design system
- Add additional validation rules as needed
- Extend components with project-specific features
# Run all tests
npm test
# Run tests in watch mode
npm test -- --watch
# Run tests with coverage
npm test -- --coverage
# Run specific test file
npm test FormField.test.tsxSee src/components/forms/README.md for detailed component documentation and usage examples.