Uecko_ERP/packages/rdx-ui/src/components/form/TextField.tsx

96 lines
2.6 KiB
TypeScript
Raw Normal View History

2025-07-09 17:56:15 +00:00
import {
FormControl,
FormField,
FormItem,
FormLabel,
FormMessage,
Input,
} from "@repo/shadcn-ui/components";
import { cn } from "@repo/shadcn-ui/lib/utils";
import { Control, FieldPath, FieldValues } from "react-hook-form";
import { useTranslation } from "../../locales/i18n.ts";
type TextFieldProps<TFormValues extends FieldValues> = {
control: Control<TFormValues>;
name: FieldPath<TFormValues>;
label?: string;
placeholder?: string;
description?: string;
disabled?: boolean;
required?: boolean;
readOnly?: boolean;
className?: string;
2025-09-20 10:43:37 +00:00
icon?: React.ReactNode; // Icono con tamaño: <MailIcon className="h-[18px] w-[18px]" />
iconPosition?: "left" | "right"; // 'left' por defecto
2025-07-09 17:56:15 +00:00
};
export function TextField<TFormValues extends FieldValues>({
control,
name,
label,
placeholder,
description,
disabled = false,
required = false,
readOnly = false,
className,
2025-09-20 10:43:37 +00:00
icon,
iconPosition = "left",
2025-07-09 17:56:15 +00:00
}: TextFieldProps<TFormValues>) {
const { t } = useTranslation();
const isDisabled = disabled || readOnly;
2025-09-20 10:43:37 +00:00
const hasIcon = Boolean(icon);
const isLeft = iconPosition === "left";
const inputPadding = hasIcon ? (isLeft ? "pl-10" : "pr-10") : "";
2025-09-18 09:46:25 +00:00
const { getFieldState } = control;
const state = getFieldState(name);
2025-07-09 17:56:15 +00:00
return (
<FormField
control={control}
name={name}
render={({ field }) => (
2025-07-17 08:50:28 +00:00
<FormItem className={cn("space-y-0", className)}>
2025-07-09 17:56:15 +00:00
{label && (
<div className='flex justify-between items-center'>
<FormLabel className='m-0'>{label}</FormLabel>
{required && <span className='text-xs text-destructive'>{t("common.required")}</span>}
</div>
)}
<FormControl>
2025-09-20 10:43:37 +00:00
<div className={cn("relative")}>
<Input
disabled={isDisabled}
placeholder={placeholder}
{...field}
className={cn("placeholder:font-normal placeholder:italic", inputPadding)}
/>
{hasIcon && (
<span
aria-hidden='true'
className={cn(
"pointer-events-none absolute top-1/2 -translate-y-1/2",
isLeft ? "left-3" : "right-3"
)}
>
{icon} {/* El tamaño viene indicado en el icono */}
</span>
)}
</div>
2025-07-09 17:56:15 +00:00
</FormControl>
<p className={cn("text-xs text-muted-foreground", !description && "invisible")}>
{description || "\u00A0"}
</p>
<FormMessage />
</FormItem>
)}
/>
);
}