import { ArrowsIn, ArrowsOut, Check, X } from "@phosphor-icons/react"; import React, { useState, useRef, useEffect } from "react"; export default function TextArea({ defaultValue, required = true, placeholder = "Place your text here...", onChange, name = "text-area", disabled = false, initialRows = 5, className = "", autoComplete = "off", wrap = "soft", code = false, onSave, value, }) { const [rows, setRows] = useState(initialRows); const [isExpanded, setIsExpanded] = useState(false); const [showExpandIcon, setShowExpandIcon] = useState(false); const [content, setContent] = useState(value || ""); const [showSaveButton, setShowSaveButton] = useState(false); const textAreaRef = useRef(null); useEffect(() => { setContent(value); adjustRowsToFitContent(); // Initial check to determine if the expand icon should be shown checkForOverflow(); }, [value]); const toggleExpansion = () => { setIsExpanded(!isExpanded); if (!isExpanded) { adjustRowsToFitContent(true); } else { setRows(initialRows); } // After toggling, check again to see if the content overflows setTimeout(checkForOverflow, 0); // Timeout ensures the DOM updates are applied }; const adjustRowsToFitContent = (forceExpand = false) => { const textarea = textAreaRef.current; if (textarea) { const lineHeight = 18; // Match this with your CSS if (forceExpand || isExpanded) { const currentRows = Math.ceil(textarea.scrollHeight / lineHeight); setRows(currentRows); } checkForOverflow(); } }; const handleChange = (e) => { if (onChange) { onChange(e); } adjustRowsToFitContent(); setContent(e.target.value); setShowSaveButton(true); }; // Function to determine if the expand/collapse icon should be shown const checkForOverflow = () => { const textarea = textAreaRef.current; if (textarea) { // Check if the content overflows const isOverflowing = textarea.scrollHeight > textarea.clientHeight; setShowExpandIcon(isOverflowing); } }; // Handle save action const handleSave = () => { if (onSave) { onSave(content); setShowSaveButton(false); } }; // Handle cancel action const handleCancel = () => { setContent(value); setShowSaveButton(false); }; const textColorClass = disabled ? "text-white/40" : "text-white/60"; const codeClass = code ? "font-mono text-xs" : "text-sm"; return (