Spaces:
Running

Bot / frontend /src /shared /components /Input.tsx
ddosvn's picture
Initial deploy of Panel ThTuanIT full stack and Lighthouse-optimized frontend
7e79d39 verified
Raw History Blame Contribute Delete
3.55 kB
import React, { useId } from 'react';
import { cn } from '@/shared/lib/cn';
const BASE_CONTROL =
'w-full rounded-xl bg-surface-2 border border-line-strong text-ink-1 placeholder:text-ink-3 text-xs sm:text-sm outline-none transition focus:border-accent focus-visible:ring-2 focus-visible:ring-accent/40 disabled:opacity-60';
interface InputProps extends React.InputHTMLAttributes<HTMLInputElement> {
label?: string;
helperText?: string;
error?: string;
leftIcon?: React.ReactNode;
}
export function Input({
label,
helperText,
error,
leftIcon,
id,
className,
...rest
}: InputProps) {
const autoId = useId();
const fieldId = id || autoId;
return (
<div className="space-y-1.5">
{label && (
<label htmlFor={fieldId} className="block text-xs font-medium text-ink-2">
{label}
</label>
)}
<div className="relative">
{leftIcon && (
<span
className="absolute left-3 top-1/2 -translate-y-1/2 text-ink-3 pointer-events-none"
aria-hidden="true"
>
{leftIcon}
</span>
)}
<input
id={fieldId}
aria-invalid={error ? 'true' : undefined}
className={cn(
BASE_CONTROL,
'min-h-[42px] py-2',
leftIcon ? 'pl-9 pr-3' : 'px-3',
error && 'border-rose-500/70 focus:border-rose-500',
className,
)}
{...rest}
/>
</div>
{error && (
<p role="alert" className="text-[11px] text-rose-500">
{error}
</p>
)}
{!error && helperText && <p className="text-[11px] text-ink-3">{helperText}</p>}
</div>
);
}
interface TextareaProps extends React.TextareaHTMLAttributes<HTMLTextAreaElement> {
label?: string;
helperText?: string;
error?: string;
}
export function Textarea({
label,
helperText,
error,
id,
className,
...rest
}: TextareaProps) {
const autoId = useId();
const fieldId = id || autoId;
return (
<div className="space-y-1.5">
{label && (
<label htmlFor={fieldId} className="block text-xs font-medium text-ink-2">
{label}
</label>
)}
<textarea
id={fieldId}
aria-invalid={error ? 'true' : undefined}
className={cn(
BASE_CONTROL,
'px-3 py-2.5 leading-relaxed',
error && 'border-rose-500/70 focus:border-rose-500',
className,
)}
{...rest}
/>
{error && (
<p role="alert" className="text-[11px] text-rose-500">
{error}
</p>
)}
{!error && helperText && <p className="text-[11px] text-ink-3">{helperText}</p>}
</div>
);
}
interface SelectProps extends React.SelectHTMLAttributes<HTMLSelectElement> {
label?: string;
error?: string;
}
export function Select({ label, error, id, className, children, ...rest }: SelectProps) {
const autoId = useId();
const fieldId = id || autoId;
return (
<div className="space-y-1.5">
{label && (
<label htmlFor={fieldId} className="block text-xs font-medium text-ink-2">
{label}
</label>
)}
<select
id={fieldId}
aria-invalid={error ? 'true' : undefined}
className={cn(BASE_CONTROL, 'min-h-[42px] px-3 py-2', className)}
{...rest}
>
{children}
</select>
{error && (
<p role="alert" className="text-[11px] text-rose-500">
{error}
</p>
)}
</div>
);
}