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

111 lines
3.1 KiB
TypeScript
Raw Normal View History

2025-07-09 17:56:15 +00:00
// DatePickerField.tsx
import {
FormControl,
2025-09-24 11:49:17 +00:00
FormDescription,
2025-07-09 17:56:15 +00:00
FormField,
FormItem,
FormLabel,
FormMessage,
Textarea,
} from "@repo/shadcn-ui/components";
import { cn } from "@repo/shadcn-ui/lib/utils";
2025-10-02 16:30:46 +00:00
import { Control, FieldPath, FieldValues, useController } from "react-hook-form";
2025-07-09 17:56:15 +00:00
import { useTranslation } from "../../locales/i18n.ts";
2025-10-02 16:30:46 +00:00
import { CommonInputProps } from "./types.js";
2025-07-09 17:56:15 +00:00
2025-10-02 16:30:46 +00:00
type TextAreaFieldProps<TFormValues extends FieldValues> = CommonInputProps & {
2025-07-09 17:56:15 +00:00
control: Control<TFormValues>;
name: FieldPath<TFormValues>;
label?: string;
placeholder?: string;
description?: string;
disabled?: boolean;
required?: boolean;
readOnly?: boolean;
className?: string;
2025-10-02 16:30:46 +00:00
/** Contador de caracteres (si usas maxLength) */
showCounter?: boolean;
2025-07-09 17:56:15 +00:00
};
export function TextAreaField<TFormValues extends FieldValues>({
control,
name,
label,
placeholder,
description,
disabled = false,
required = false,
readOnly = false,
className,
2025-10-02 16:30:46 +00:00
showCounter = false,
maxLength,
2025-07-09 17:56:15 +00:00
}: TextAreaFieldProps<TFormValues>) {
const { t } = useTranslation();
const isDisabled = disabled || readOnly;
2025-10-02 16:30:46 +00:00
const { field, fieldState } = useController({ control, name });
const describedById = description ? `${name}-desc` : undefined;
const errorId = fieldState.error ? `${name}-err` : undefined;
const valueLength = (field.value?.length ?? 0) as number;
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 && (
2025-09-21 19:46:51 +00:00
<div className='mb-1 flex justify-between gap-2'>
<div className='flex items-center gap-2'>
2025-10-02 16:30:46 +00:00
<FormLabel
htmlFor={name}
className={cn("m-0", disabled ? "text-muted-foreground" : "")}
>
2025-09-21 19:46:51 +00:00
{label}
</FormLabel>
{required && (
<span className='text-xs text-destructive'>{t("common.required")}</span>
)}
</div>
2025-10-02 16:30:46 +00:00
{/* Punto “unsaved” */}
{fieldState.isDirty && (
<span className='text-[10px] text-muted-foreground'>{t("common.modified")}</span>
)}
2025-07-09 17:56:15 +00:00
</div>
)}
<FormControl>
2025-09-24 11:49:17 +00:00
<Textarea
disabled={isDisabled}
placeholder={placeholder}
2025-09-30 15:58:04 +00:00
className={"placeholder:font-normal placeholder:italic bg-background"}
2025-10-02 16:30:46 +00:00
maxLength={maxLength}
2025-09-24 11:49:17 +00:00
{...field}
/>
2025-07-09 17:56:15 +00:00
</FormControl>
2025-10-02 16:30:46 +00:00
<div className='mt-1 flex items-start justify-between'>
<FormDescription
id={describedById}
className={cn("text-xs truncate", !description && "invisible")}
>
{description || "\u00A0"}
</FormDescription>
{showCounter && typeof maxLength === "number" && (
<p className='text-xs text-muted-foreground'>
{valueLength} / {maxLength}
</p>
)}
</div>
<FormMessage id={errorId} />
2025-07-09 17:56:15 +00:00
</FormItem>
)}
/>
);
}