我有一个简单的 React 组件,textarea
当用户键入它时它会增加它的大小。该函数如下所示:
changeHeight(e) {
const height = this.textarea.clientHeight;
const scrollHeight = this.textarea.scrollHeight;
if (height < scrollHeight) {
this.textarea.style.height = scrollHeight + "px";
}
}
当我使用onKeyUp
在 textarea 上调用此函数时,它工作正常,但是如果我将其更改为onPaste
然后调用该函数(如果您 console.log 某些东西),但没有按预期将高度添加到 textarea。
我在这里遗漏了什么明显的东西吗?
这是完整的代码:
class Textarea extends React.Component {
constructor(props) {
super(props);
this.changeHeight = this.changeHeight.bind(this);
}
changeHeight(e) {
const height = this.textarea.clientHeight;
const scrollHeight = this.textarea.scrollHeight;
if (height < scrollHeight) {
this.textarea.style.height = scrollHeight + "px";
}
console.log("changeHeight");
}
render() {
const {input, label, type, optional, value, helperText, meta: { touched, error }, ...custom } = this.props;
return (
<div className="measure mb4">
<label for="name" className="f6 b db mb2">{label} {optional ? <span className="normal black-60">(optional)</span> : null}</label>
<textarea onPaste={this.changeHeight} ref={(el) => { this.textarea = el; }} className={"input-reset ba b--black-20 pa2 mb2 db w-100 border-box lh-copy h5 animate-h"} aria-describedby="name-desc" {...input} {...custom} value={value} />
{touched && error ? <small id="name-desc" className="f6 red db mb2">{error}</small> : null}
{helperText ? <small id="name-desc" className="f6 black db mb2">{helperText}</small> : null}
</div>
)
}
}