"use client"; import { Field, FieldDescription, FieldError, FieldLabel } from "@suphark/ui/components/ui/field"; import { Textarea } from "@suphark/ui/components/ui/textarea"; import { cn } from "@suphark/ui/lib/utils"; import { type ComponentProps, type CSSProperties, type ReactNode, useId, useState } from "react"; import { useUiText } from "../ui-text"; /** ความสูงตามจำนวนบรรทัด: บรรทัด × line-height + py-2 (1rem) + เส้นขอบ 2px */ const rowsHeight = (rows: number) => `calc(${rows}lh + 1rem + 2px)`; /** * ช่องข้อความหลายบรรทัดพร้อม label/hint/error + ตัวนับตัวอักษร — แทน LabeledField + Textarea + state นับเอง * Textarea ขยายตามเนื้อหาอยู่แล้ว (field-sizing) — `maxRows` จำกัดความสูงแล้วเลื่อนใน, `minRows` ความสูงเริ่มต้น * ตัวนับโผล่เมื่อมี `maxLength` (ปิดด้วย `showCount={false}`) ใช้ได้ทั้ง controlled (`value`) และ uncontrolled (`defaultValue` + `name`) * * */ export function TextareaField({ label, hint, error, showCount, minRows, maxRows, className, textareaClassName, id, style, onChange, ...props }: Omit, "children"> & { label: ReactNode; /** ข้อความช่วยใต้ช่อง — ซ่อนเมื่อมี error */ hint?: ReactNode; error?: ReactNode; /** ค่าเริ่มต้น = true เมื่อมี maxLength */ showCount?: boolean; minRows?: number; maxRows?: number; /** className ของ textarea (`className` อยู่ที่กล่อง Field) */ textareaClassName?: string; }) { const text = useUiText(); const autoId = useId(); const inputId = id ?? autoId; const countId = `${inputId}-count`; const [uncontrolledLength, setUncontrolledLength] = useState( () => String(props.defaultValue ?? "").length, ); const length = props.value != null ? String(props.value).length : uncontrolledLength; const max = props.maxLength; const counting = (showCount ?? max != null) && max != null; const sizeStyle: CSSProperties = { ...(minRows ? { minHeight: rowsHeight(minRows) } : null), ...(maxRows ? { maxHeight: rowsHeight(maxRows) } : null), ...style, }; return ( {label}