Overview
The Input component provides a complete form input solution with built-in label, error handling, and accessibility features.
Import
import { Input } from '@abdalkader/component-library';
Basic Usage
<Input
label="Email"
type="email"
placeholder="Enter your email"
/>
With Label
<Input
label="Full Name"
placeholder="John Doe"
required
/>
<Input label="Username" type="text" />
Error State
<Input
label="Email"
type="email"
value="invalid-email"
error={true}
errorMessage="Please enter a valid email address"
/>
Helper Text
<Input
label="Password"
type="password"
helperText="Must be at least 8 characters long"
/>
Required Field
<Input
label="Email"
type="email"
required
/>
Controlled Component
function MyForm() {
const [email, setEmail] = useState('');
return (
<Input
label="Email"
type="email"
value={email}
onChange={(e) => setEmail(e.target.value)}
/>
);
}
function ValidatedInput() {
const [email, setEmail] = useState('');
const [error, setError] = useState(false);
const validate = (value: string) => {
const isValid = /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(value);
setError(!isValid && value.length > 0);
};
return (
<Input
label="Email"
type="email"
value={email}
onChange={(e) => {
setEmail(e.target.value);
validate(e.target.value);
}}
error={error}
errorMessage={error ? 'Invalid email address' : undefined}
required
/>
);
}
Accessibility
The Input component includes proper ARIA attributes:
- Automatic ID generation for label association
aria-invalid for error states
aria-describedby for helper text and errors
aria-required for required fields
Props
type
'text' | 'email' | 'password' | 'number' | 'tel' | 'url'
default:"text"
HTML input type
Default value (uncontrolled)
Examples
function LoginForm() {
const [email, setEmail] = useState('');
const [password, setPassword] = useState('');
return (
<form>
<Input
label="Email"
type="email"
value={email}
onChange={(e) => setEmail(e.target.value)}
required
/>
<Input
label="Password"
type="password"
value={password}
onChange={(e) => setPassword(e.target.value)}
required
/>
<Button type="submit">Sign In</Button>
</form>
);
}
<Input
type="text"
placeholder="Search..."
aria-label="Search"
/>