Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 4910x 4910x 4910x 4910x 4910x 4910x 4910x 4910x 4910x 4910x 4910x 4910x 4910x 4910x 4910x 4910x 4910x 4910x 4910x 4910x 4910x 4910x 4910x 4910x 4910x 4910x 4910x 4910x 4910x 4910x 4910x 4910x 4910x 4910x 4910x 4910x 4910x 4910x 4910x 4910x 4910x 4910x 4910x 4910x 4910x 4910x 4910x 4910x 4910x 4910x 4910x 4910x 4910x 4910x 4910x 4910x 4910x 4910x 4910x 4910x 4910x 4910x 4910x 4910x 4910x 4910x 4910x 4910x 4910x 4910x 4910x 4910x 4910x 547x 547x 547x 547x 547x 547x 547x 547x 547x 547x 547x 547x 547x 547x 547x 547x 547x 547x 547x 547x 547x 547x 547x 4363x 4880x 75x 75x 75x 75x 75x 75x 75x 75x 75x 75x 75x 75x 75x 75x 75x 4288x 4288x 4288x 4813x 92x 92x 92x 4910x 4910x 4910x 4910x 4910x 4910x 4910x 4910x 4910x 4910x 4910x 4910x 4910x 4910x 4910x 4910x 4x 4x 4x 4910x 4910x 4910x 4910x 4910x 4910x 4910x 4910x 4226x 4226x 4226x 4226x 4226x 4226x 3570x 3570x 3570x 3570x 4226x 4226x 4910x 4910x 4910x 4910x 4910x 4910x 4910x 20x 20x 20x 4910x 4910x 4910x 195x 195x 195x 4910x 4910x 4910x 4910x 1x 1x 1x 1x 1x | import React from 'react';
import { cn } from '@/lib/core';
import { InputProps } from '@/types/ui';
import { Icon } from '@/components/ui/icons';
/**
* Input Component
*
* A flexible, accessible input component with validation states and icons.
* Supports input, select, and textarea elements.
* Built with design system tokens for consistency.
*
* @example
* ```tsx
* // Text Input
* <Input
* label="Email"
* type="email"
* placeholder="Enter your email"
* helperText="We'll never share your email"
* />
*
* // Select Dropdown
* <Input
* as="select"
* label="Country"
* required
* >
* <option value="us">United States</option>
* <option value="ca">Canada</option>
* </Input>
*
* // With Icon
* <Input
* label="Search"
* leftIcon={<Icon name="search" />}
* placeholder="Search products..."
* />
*
* // With Error
* <Input
* label="Password"
* type="password"
* error="Password must be at least 8 characters"
* />
*
* // Textarea
* <Input
* as="textarea"
* label="Message"
* rows={4}
* />
* ```
*/
export const Input = React.forwardRef<HTMLInputElement | HTMLSelectElement | HTMLTextAreaElement, InputProps>(
(props, ref) => {
const {
as = 'input',
size = 'md',
state = 'default',
label,
helperText,
error,
leftIcon,
rightIcon,
fullWidth,
className,
id,
required,
children,
...restProps
} = props;
const generatedId = React.useId();
const inputId = id || generatedId;
const helperTextId = `${inputId}-helper`;
const errorId = `${inputId}-error`;
// Error overrides state
const actualState = error ? 'error' : state;
const baseStyles = `
w-full rounded-lg border bg-white dark:bg-gray-800
transition-all duration-200
focus:outline-none focus:ring-2 focus:ring-offset-1 dark:focus:ring-offset-gray-900
disabled:opacity-50 disabled:cursor-not-allowed disabled:bg-gray-100 dark:disabled:bg-gray-700
placeholder:text-gray-400 dark:placeholder:text-gray-500
`;
const sizeStyles = {
sm: 'px-3 py-1.5 text-sm',
md: 'px-4 py-2 text-base',
lg: 'px-5 py-3 text-lg'};
const stateStyles = {
default: `
border-gray-300 dark:border-gray-600 text-gray-900 dark:text-gray-100
hover:border-gray-400 dark:hover:border-gray-500
focus:border-blue focus:ring-blue/20
`,
success: `
border-green-500 text-gray-900 dark:text-gray-100
focus:border-green-500 focus:ring-green-500/20
`,
error: `
border-red-500 text-gray-900 dark:text-gray-100
focus:border-red-500 focus:ring-red-500/20
`,
warning: `
border-yellow-500 text-gray-900 dark:text-gray-100
focus:border-yellow-500 focus:ring-yellow-500/20
`};
const iconPadding = {
left: leftIcon ? 'pl-10' : '',
right: rightIcon ? 'pr-10' : ''};
const elementClassName = cn(
baseStyles,
sizeStyles[size],
stateStyles[actualState],
as !== 'select' && iconPadding.left,
as !== 'select' && iconPadding.right,
as === 'select' && 'appearance-none pr-10',
className
);
const renderElement = () => {
if (as === 'select') {
return (
<>
<select
ref={ref as React.Ref<HTMLSelectElement>}
id={inputId}
className={elementClassName}
aria-describedby={
error ? errorId : helperText ? helperTextId : undefined
}
aria-invalid={actualState === 'error'}
aria-required={required || undefined}
required={required}
{...(restProps as React.SelectHTMLAttributes<HTMLSelectElement>)}
>
{children}
</select>
{/* Chevron icon for select */}
<div className="absolute right-3 top-1/2 -translate-y-1/2 text-gray-400 pointer-events-none">
<Icon name="chevron-down" size={16} />
</div>
</>
);
}
if (as === 'textarea') {
return (
<textarea
ref={ref as React.Ref<HTMLTextAreaElement>}
id={inputId}
className={elementClassName}
aria-describedby={
error ? errorId : helperText ? helperTextId : undefined
}
aria-invalid={actualState === 'error'}
aria-required={required || undefined}
required={required}
{...(restProps as React.TextareaHTMLAttributes<HTMLTextAreaElement>)}
/>
);
}
return (
<>
{leftIcon && (
<div className="absolute left-3 top-1/2 -translate-y-1/2 text-gray-400 pointer-events-none">
{leftIcon}
</div>
)}
<input
ref={ref as React.Ref<HTMLInputElement>}
id={inputId}
className={elementClassName}
aria-describedby={
error ? errorId : helperText ? helperTextId : undefined
}
aria-invalid={actualState === 'error'}
aria-required={required || undefined}
required={required}
{...(restProps as React.InputHTMLAttributes<HTMLInputElement>)}
/>
{rightIcon && (
<div className="absolute right-3 top-1/2 -translate-y-1/2 text-gray-400 pointer-events-none">
{rightIcon}
</div>
)}
</>
);
};
return (
<div className={cn('relative', fullWidth && 'w-full')}>
{label && (
<label
htmlFor={inputId}
className="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1"
>
{label}
{required && (
<>
<span aria-hidden="true" className="text-red-500 ml-1">*</span>
<span className="sr-only">(required)</span>
</>
)}
</label>
)}
<div className="relative">
{renderElement()}
</div>
{error && (
<p id={errorId} className="mt-1 text-sm text-red-600" role="alert">
{error}
</p>
)}
{!error && helperText && (
<p id={helperTextId} className="mt-1 text-sm text-gray-500 dark:text-gray-400">
{helperText}
</p>
)}
</div>
);
}
);
Input.displayName = 'Input';
export default Input;
|