当文本超过文本方面的高度尾风CSS(黛西UI)时,可以调整textarea的大小

发布于 2025-02-04 01:46:16 字数 1381 浏览 4 评论 0原文

IM试图使用尾风CSS在React应用程序中调整TextArea的大小,但是,当我启用调整大小时,当文本超过文本方面的高度时,我将无法再调整TextAarea的大小。

事情的屏幕截图

  1. 以下是文本超过高度之前

文本超过高度

  1. 之前

发生的 a href =“ https://i.sstatic.net/ffqee.png” rel =“ nofollow noreferrer”>文本超过高度之后

下面是为textarea创建的组件

<div className='form-control w-full'>
  <label className='label'>
    <span className='label-text text-primary'>{title}</span>
  </label>
  <textarea
    placeholder={placeholder}
    className={`textarea w-full resize-y ${
      value !== text ? 'bg-green-100' : 'bg-white'
    }`}
    value={text}
    onChange={(e) => {
      setText(e.target.value);
      debounced(e.target.value);
    }}
  />
</div>

此textarea组件包裹在以下代码中

<div className="flex flex-row flex-wrap">
  {descriptions.map((field: CollapsibleInputField, i: number) => (
    <div
      key={i}
      className={
        field.placeholder === "English" ? "w-full pr-2" : "w-1/2 pr-2"
      }
    >
      <TextAreaContainer
        id={field.id}
        value={field.data}
        placeholder={field.placeholder}
        title={field.title}
      />
    </div>
  ))}
</div>

我在做什么错?

Im trying to enable resizing of TextArea in a react application using TailWind CSS, however when I do enable resizing, when the text exceeds the height of TextArea I'm unable to resize the TextArea anymore.

Below are screenshots of what is happening

  1. Before Text exceeds height

Before Text exceeds height

  1. After Text exceeds height

After Text exceeds height

Below is the component created for the TextArea

<div className='form-control w-full'>
  <label className='label'>
    <span className='label-text text-primary'>{title}</span>
  </label>
  <textarea
    placeholder={placeholder}
    className={`textarea w-full resize-y ${
      value !== text ? 'bg-green-100' : 'bg-white'
    }`}
    value={text}
    onChange={(e) => {
      setText(e.target.value);
      debounced(e.target.value);
    }}
  />
</div>

This TextArea component is wrapped in the following code

<div className="flex flex-row flex-wrap">
  {descriptions.map((field: CollapsibleInputField, i: number) => (
    <div
      key={i}
      className={
        field.placeholder === "English" ? "w-full pr-2" : "w-1/2 pr-2"
      }
    >
      <TextAreaContainer
        id={field.id}
        value={field.data}
        placeholder={field.placeholder}
        title={field.title}
      />
    </div>
  ))}
</div>

What am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

掐死时间 2025-02-11 01:46:16

您正在尝试使用该调整大小来使文本动态根据文本进入其中。但是,您需要使用REF观察元素,并增加Textarea动态的高度。

这可以解决问题。

const ref = useRef<HTMLTextAreaElement>(null);

const handleInput = (e: ChangeEvent<HTMLTextAreaElement>) => {
  if (ref.current) {
    ref.current.style.height = "auto";
    ref.current.style.height = `${e.target.scrollHeight - 16}px`;
  }
};

return (
  <div className="App">
    <section>
      <textarea
        ref={ref}
        rows={1}
        placeholder="Enter text here..."
        onInput={handleInput}
      />
    </section>
  </div>
);

You are trying use the resize to let the textarea dynamic according to the text into there. But, you need observer the element using the ref for example and increase the height of textarea dynamic.

This could solve it the problem.

const ref = useRef<HTMLTextAreaElement>(null);

const handleInput = (e: ChangeEvent<HTMLTextAreaElement>) => {
  if (ref.current) {
    ref.current.style.height = "auto";
    ref.current.style.height = `${e.target.scrollHeight - 16}px`;
  }
};

return (
  <div className="App">
    <section>
      <textarea
        ref={ref}
        rows={1}
        placeholder="Enter text here..."
        onInput={handleInput}
      />
    </section>
  </div>
);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文