2026-04-12 23:01:47 +07:00
|
|
|
import * as React from 'react';
|
|
|
|
|
import { Input } from '../../atoms/input';
|
|
|
|
|
import { Label } from '../../atoms/label';
|
2025-03-27 05:25:02 +07:00
|
|
|
import { cn } from '@imphnen-frontend-service/utils';
|
2025-03-27 04:35:41 +07:00
|
|
|
|
2026-04-12 23:01:47 +07:00
|
|
|
export type TInputType =
|
|
|
|
|
| 'text'
|
|
|
|
|
| 'email'
|
|
|
|
|
| 'number'
|
|
|
|
|
| 'password'
|
|
|
|
|
| 'file'
|
|
|
|
|
| 'date'
|
|
|
|
|
| 'time';
|
2025-04-03 12:21:10 +07:00
|
|
|
export type TInputSize = 'sm' | 'md' | 'lg';
|
|
|
|
|
export type TInputFieldProps = Omit<
|
2026-04-12 23:01:47 +07:00
|
|
|
React.InputHTMLAttributes<HTMLInputElement>,
|
2025-03-27 04:35:41 +07:00
|
|
|
'size' | 'type'
|
|
|
|
|
> & {
|
|
|
|
|
label: string;
|
|
|
|
|
type?: TInputType;
|
|
|
|
|
size?: TInputSize;
|
|
|
|
|
error?: string;
|
2025-03-27 05:25:02 +07:00
|
|
|
helperText?: string;
|
|
|
|
|
htmlFor?: string;
|
2025-11-26 16:41:10 +07:00
|
|
|
isRequired?: boolean;
|
2025-03-27 04:35:41 +07:00
|
|
|
};
|
|
|
|
|
|
2026-04-12 23:01:47 +07:00
|
|
|
export const InputField = React.forwardRef<HTMLInputElement, TInputFieldProps>(
|
|
|
|
|
(
|
|
|
|
|
{
|
|
|
|
|
label,
|
|
|
|
|
placeholder,
|
|
|
|
|
type = 'text',
|
|
|
|
|
size = 'md',
|
|
|
|
|
error,
|
|
|
|
|
helperText,
|
|
|
|
|
htmlFor,
|
|
|
|
|
className,
|
|
|
|
|
disabled,
|
|
|
|
|
isRequired = false,
|
|
|
|
|
id,
|
|
|
|
|
...rest
|
|
|
|
|
},
|
|
|
|
|
ref
|
|
|
|
|
) => {
|
|
|
|
|
const autoId = React.useId();
|
|
|
|
|
const fieldId = htmlFor ?? id ?? autoId;
|
|
|
|
|
return (
|
|
|
|
|
<div className="flex flex-col gap-2">
|
|
|
|
|
<Label htmlFor={fieldId} className="text-sm font-medium text-foreground">
|
|
|
|
|
{label}
|
|
|
|
|
{isRequired && <span className="text-destructive">*</span>}
|
|
|
|
|
</Label>
|
|
|
|
|
<Input
|
|
|
|
|
ref={ref}
|
|
|
|
|
id={fieldId}
|
|
|
|
|
placeholder={placeholder}
|
|
|
|
|
type={type}
|
|
|
|
|
size={size}
|
|
|
|
|
disabled={disabled}
|
|
|
|
|
aria-invalid={!!error}
|
|
|
|
|
className={cn(
|
|
|
|
|
error && 'border-destructive focus-visible:ring-destructive/20',
|
|
|
|
|
className
|
|
|
|
|
)}
|
|
|
|
|
{...rest}
|
|
|
|
|
/>
|
|
|
|
|
{error ? (
|
|
|
|
|
<p className="text-xs text-destructive">{error}</p>
|
|
|
|
|
) : helperText ? (
|
|
|
|
|
<p className="text-xs text-muted-foreground">{helperText}</p>
|
|
|
|
|
) : null}
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
InputField.displayName = 'InputField';
|