【原理】antd-Input.textarea如何实现自动伸缩?

使用antd的Input.TextArea,配置maxRowsminRows即可实现让输入框中的string自动伸缩展示。

例子:

// 最少展示2行,最多展示10行
<Input.TextArea autoSize={{ minRows: 2, maxRows: 10 }} />

但是它内部是怎么实现的呢?

实现

  • step1:实现自适应高度
    我们的目标是让 textarea 根据输入的内容自动调整高度。
    为了实现这一功能,核心: 动态调整style.height
textareaRef.current.style.height = `${textareaRef.current.scrollHeight}px`
  • step2:防抖
    使用Lodash的防抖功能,避免每次输入都导致不必要的渲染和高度调整
import { debounce } from 'lodash'; // 使用 Lodash 的 debounce

const AutoSizeTextarea = ({ onChange, maxLines = 10, rowHeight = 28, ...props }) => {
  const [text, setText] = useState();
  const textareaRef = useRef(null);

  // 自适应高度的调整函数
  const adjustHeight = useCallback(() => {
    const textarea = textareaRef.current;
    if (!textarea) return;

    // 动态计算最大高度:行数 * 每行高度
    const maxHeight = rowHeight * maxLines;
    const currentHeight = textarea.scrollHeight;

    // 只在需要时调整
    if (textarea.style.height === `${currentHeight}px`) return;

    // 防止超出最大高度
    const newHeight = Math.min(currentHeight, maxHeight);

    // 使用 requestAnimationFrame 来优化性能
    requestAnimationFrame(() => {
      textarea.style.height = 'auto';  // 先设置为 auto,避免高度被固定
      textarea.style.height = `${newHeight}px`;  // 设置为计算出的高度
    });
  }, [maxLines, rowHeight]);

  // 使用 Lodash 的 debounce 来包装 adjustHeight,防抖延迟 300ms
  const debouncedAdjustHeight = useCallback(debounce(adjustHeight, 300), [adjustHeight]);

  // 初始化时调整高度
  useEffect(() => {
    adjustHeight(); // 只需要调用一次,不再使用防抖
  }, [defaultValue]);

  // 处理输入变化
  const handleChange = (e) => {
    const value = e.target.value;
    setText(value);
    onChange && onChange(e); // 保持原有 onChange 逻辑

    // 调整高度
    debouncedAdjustHeight();
  };

  return (
    <textarea
      value={text}
      ref={textareaRef}
      rows="1"
      onChange={handleChange}
      style={{ minHeight: '50px' }}
      {...props}
    />
  );
};

export default AutoSizeTextarea;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

你脸上有BUG

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值