【问题标题】:How to make textarea expand when click into it?单击时如何使文本区域扩展?
【发布时间】:2021-10-08 09:06:59
【问题描述】:

我正在尝试创建 onClick 函数,当用户单击 textarea 时它会自动完全展开。

这是我的代码库:

class DynamicWidthInput extends React.Component {
  componentDidMount() {
    const { id, value } = this.props;
    resizable(document.getElementById(id), 16, value);
  }

  componentDidUpdate(prevProps) {
    const { id, value } = this.props;

    if (this.props.value !== prevProps.value) {
      resizable(document.getElementById(id), 16, value);
    }
  }

  render() {
    const { id, placeholder, type, onChange, value, error, size } = this.props;
    if (type === "textarea") {
      return (
        <div style={{ display: "inline-block", verticalAlign: "top" }}>
          <textarea
            className={`input dynamic-input dynamic-textarea ${size}`}
            id={id}
            onChange={onChange}
            placeholder={placeholder}
            value={value}
            wrap="soft"
            maxLength="1000"
            {...this.props}
          />
          {error && <p className="help is-danger">{error.message}</p>}
        </div>
      );
    }
   ...
  }
}

export default DynamicWidthInput;

这是我显示文本区域的方式:

   <DynamicWidthInput
      id="addLoanClause"
      value={addLoanClause}
      type="textarea"
      placeholder="1000 characters"
      error={showErrors && getError("addLoanClause")}
      onChange={e =>
        handleDynamicStateChange(e, setAddLoanClause)
      }
      size={"xxl"}
      rows="5"
    />{" "}

这是我的项目的样子:

预计完全显示如下:

【问题讨论】:

    标签: javascript css reactjs styling


    【解决方案1】:

    我认为你可以使用这种风格。

    textarea {
        transition: all .3s ease;
        height: 100px;
    }
    
    textarea:focus {
        height: 300px;
    }
    

    【讨论】:

    • 感谢您的帮助,如果我只想将其扩展至刚好能容纳 100 个字符,该怎么办?
    • 您在 textarea 中有 cols 和 rows,所以我认为可以使用它。例如,cols: 10; rows: 5;cols: 10; rows: 10;。事实上,我从来没有这样尝试过。 :)
    猜你喜欢
    • 2019-02-15
    • 1970-01-01
    • 1970-01-01
    • 2011-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多