'use client' import { IconButton } from '@/components/ui/button' import { cn } from '@/lib/utils' import { useState, useRef, useEffect } from 'react' const AuthHeightTextarea = (props: React.ComponentProps<'textarea'> & { maxHeight?: number }) => { const { maxHeight = 200, className, value, onChange, ...restProps } = props const textareaRef = useRef(null) // 调整高度的函数 const adjustHeight = () => { const textarea = textareaRef.current if (!textarea) return // 先重置高度为 0,这样才能获取真实的 scrollHeight textarea.style.height = '0px' // 获取内容实际需要的高度 const scrollHeight = textarea.scrollHeight // 计算新高度:取 scrollHeight 和 maxHeight 的较小值 const newHeight = Math.min(scrollHeight, maxHeight) textarea.style.height = `${newHeight}px` // 如果内容超过最大高度,显示滚动条 textarea.style.overflowY = scrollHeight > maxHeight ? 'auto' : 'hidden' } // 监听内容变化,自动调整高度 useEffect(() => { adjustHeight() }, [value, maxHeight]) return (