Input
A versatile and accessible input component with support for validation, icons, and custom styles.
Introduction
The Input
component is a flexible and accessible input field that supports validation, helper text, icons, and error handling. It adapts to various use cases like text, password, and form validation scenarios.
Default Use Case: Input Variants
Simple Input
Customize Your Input
Password Input
Error Input
Invalid email address
Installation
Install the Input
component using the following command:
File Structure
Advanced Usage
Input with Icons
You can add icons at the start or end of the input field:
Character Limit
Add a character limit to restrict input length:
Complex Validation with Error State
Prop Types
Prop | Type | Default |
---|---|---|
variant | "default" | "error" | "default" |
type | string | "text" |
helperText | string | - |
error | string | - |
startIcon | React.ReactNode | - |
endIcon | React.ReactNode | - |
characterLimit | number | - |
Features
Validation
The Input component includes built-in validation capabilities:
- Character Limit: Easily restrict input length with visual feedback
- Error States: Clearly indicates validation errors with red borders and error messages
- Zod Integration: Uses zod for type-safe validation rules
- Custom Validation: Flexible enough to accommodate any validation logic
Accessibility
The Input component follows best accessibility practices:
-
ARIA Attributes:
- Uses
aria-invalid
to indicate validation errors - Includes
aria-describedby
to associate helper and error text with the input - Error messages have
role="alert"
for screen reader announcements
- Uses
-
Keyboard Navigation:
- Fully keyboard-accessible
- Proper tab order and focus states
-
Visual Cues:
- Clear focus states for keyboard users
- High contrast error states
- Helper text and character counts for additional context
Styling and Customization
The component is highly customizable:
- Class Variance Authority: Uses CVA for variant-based styling
- Tailwind Integration: Works seamlessly with your Tailwind theme
- Icon Support: Add contextual icons at either end of the input
- Inline Styling: Support for additional style customization
Conclusion
The Input
component is a robust foundation for building accessible and user-friendly forms. Its comprehensive validation features, icon support, and accessibility considerations make it suitable for a wide range of applications, from simple contact forms to complex data entry interfaces.