import React, { useState, useRef, useEffect, forwardRef, useImperativeHandle, } from "react"; interface AutoResizingTextareaProps { maxRows?: number; placeholder?: string; value: string; onChange: (event: React.ChangeEvent) => void; onKeyDown?: (event: React.KeyboardEvent) => void; onCompositionStart?: () => void; onCompositionEnd?: () => void; autoFocus?: boolean; } const AutoResizingTextarea = forwardRef< HTMLTextAreaElement, AutoResizingTextareaProps >( ( { maxRows = 1, placeholder, value, onChange, onKeyDown, onCompositionStart, onCompositionEnd, autoFocus, }, ref, ) => { const internalTextareaRef = useRef(null); const [maxHeight, setMaxHeight] = useState(0); useImperativeHandle( ref, () => internalTextareaRef.current as HTMLTextAreaElement, ); useEffect(() => { const calculateMaxHeight = () => { const textarea = internalTextareaRef.current; if (textarea) { textarea.style.height = "auto"; const singleRowHeight = textarea.scrollHeight; setMaxHeight(singleRowHeight * maxRows); if (autoFocus) { textarea.focus(); } } }; calculateMaxHeight(); }, [maxRows]); useEffect(() => { const textarea = internalTextareaRef.current; if (textarea) { textarea.style.height = "auto"; textarea.style.height = `${Math.min(textarea.scrollHeight, maxHeight)}px`; } }, [value, maxHeight]); return (