Overview
The Input component is a fully-featured text input field with support for labels, validation errors, helper text, icons, and different sizes. It includes focus state management and accessibility features.TypeScript Types
InputSize
InputProps
The
size prop from the standard InputHTMLAttributes is omitted to avoid conflicts with our custom InputSize type.Props
Label text displayed above the input field. The label is properly associated with the input using the
htmlFor attribute for accessibility.Error message to display below the input. When present:
- Input border turns red (
border-destructive) - Label text becomes red
- Icons turn red
- Error message appears below in red text
- Takes precedence over
helperText
Helper text displayed below the input in muted color. Useful for providing hints or additional context. Hidden when
error is present.The size of the input field:
sm- Small (px-3 py-1.5 text-sm)md- Medium (px-4 py-2.5 text-base)lg- Large (px-5 py-3.5 text-lg)
Icon or element to display on the left side inside the input. The input padding is automatically adjusted to accommodate the icon.
Icon or element to display on the right side inside the input. The input padding is automatically adjusted to accommodate the icon.
When true, the input is disabled with reduced opacity and a not-allowed cursor. The background changes to muted.
Additional CSS classes to apply to the input element.
Custom ID for the input element. If not provided, a unique ID is automatically generated using React’s
useId hook.Basic Usage
Input States
- Default
- Error
- Disabled
- With Helper Text
- Card background (
bg-card) - Border color transitions from
border-inputtoborder-input-focuson focus - Focus ring with primary color
- Label transitions to primary color on focus
Icon Examples
Left Icon
- Positioned absolutely on the left
- Muted color by default
- Transitions to primary color on focus
- Transitions to destructive color on error
- Input padding automatically adjusted
Right Icon
Both Icons
Size Variants
- Small:
px-3 py-1.5 text-sm(icon padding:pl-9/pr-9) - Medium:
px-4 py-2.5 text-base(icon padding:pl-11/pr-11) - Large:
px-5 py-3.5 text-lg(icon padding:pl-12/pr-12)
Form Integration
Complete Example
Focus State Management
The Input component uses internal state to track focus:- Label color changes to primary on focus
- Border color transitions to focus color
- Focus ring appears
- Icons change to primary color (or destructive if error)
- Respects custom
onFocusandonBlurhandlers
Accessibility
- Uses semantic
<input>element with propertypeattribute - Label is associated with input via
htmlForand unique ID - Error messages are displayed visually and in the DOM
- Disabled state prevents interaction and is announced by screen readers
- Placeholder text provides hints but is not a replacement for labels
- Focus states are clearly visible for keyboard navigation
Best Practices
Always provide labels
Always provide labels
Labels are essential for accessibility. Even if the design doesn’t show a visible label, consider using the
label prop for screen readers or providing an aria-label.Use helper text for guidance
Use helper text for guidance
Helper text should provide useful context or formatting hints (e.g., “Minimum 8 characters”, “Format: MM/DD/YYYY”).
Show validation errors clearly
Show validation errors clearly
Use the
error prop to display specific, actionable error messages that help users fix validation issues.Choose appropriate input types
Choose appropriate input types
Use the correct
type attribute (email, password, tel, number, etc.) to enable proper mobile keyboards and browser validation.Icon size consistency
Icon size consistency
Keep icon sizes consistent with the input size:
- Small inputs: 16-18px icons
- Medium inputs: 18-20px icons
- Large inputs: 20-22px icons