Files
low-code-react-app-b/src/components/atoms/Input.tsx

59 lines
1.8 KiB
TypeScript

import { forwardRef } from 'react'
import { cn } from '@/lib/utils'
interface InputProps extends React.InputHTMLAttributes<HTMLInputElement> {
error?: boolean
helperText?: string
label?: string
leftIcon?: React.ReactNode
rightIcon?: React.ReactNode
}
export const Input = forwardRef<HTMLInputElement, InputProps>(
({ error, helperText, label, leftIcon, rightIcon, className, ...props }, ref) => {
return (
<div className="w-full">
{label && (
<label className="block text-sm font-medium mb-1.5 text-foreground">
{label}
</label>
)}
<div className="relative">
{leftIcon && (
<div className="absolute left-3 top-1/2 -translate-y-1/2 text-muted-foreground">
{leftIcon}
</div>
)}
<input
ref={ref}
className={cn(
'flex h-10 w-full rounded-md border bg-background px-3 py-2 text-sm',
'placeholder:text-muted-foreground',
'focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring',
'disabled:cursor-not-allowed disabled:opacity-50',
'transition-colors',
error ? 'border-destructive focus-visible:ring-destructive' : 'border-input',
leftIcon && 'pl-10',
rightIcon && 'pr-10',
className
)}
{...props}
/>
{rightIcon && (
<div className="absolute right-3 top-1/2 -translate-y-1/2 text-muted-foreground">
{rightIcon}
</div>
)}
</div>
{helperText && (
<p className={cn('text-xs mt-1.5', error ? 'text-destructive' : 'text-muted-foreground')}>
{helperText}
</p>
)}
</div>
)
}
)
Input.displayName = 'Input'