0
对于textarea
,我有一个简单的React组件,它随着用户输入它的大小而增加它的大小。功能如下:React JS:onPaste不能按预期工作
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>
)
}
}
可能在textarea的值将被更改之前触发的时间,即'onpaste'。 – Teemu
谢谢@Teemu我会看看 – A7DC
提示:使用'oninput'代替多个事件,除了写入之外,它还包括粘贴,剪切,拖放和拖放textarea文本。 – Teemu