首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >React : onPaste没有按预期工作

React : onPaste没有按预期工作
EN

Stack Overflow用户
提问于 2017-07-13 11:50:48
回答 1查看 6K关注 0票数 2

我为textarea提供了一个简单的React,它会随着用户的输入而增加其大小。该函数如下所示:

代码语言:javascript
复制
changeHeight(e) {
    const height = this.textarea.clientHeight;
    const scrollHeight = this.textarea.scrollHeight;
    if (height < scrollHeight) {
        this.textarea.style.height = scrollHeight + "px";
    }
}

当我使用onKeyUp在textarea上调用这个函数时,它工作得很好,但是如果我将它更改为onPaste,则该函数将被调用(如果您要调用某个函数),但是没有像预期的那样向textarea添加高度。

有什么明显的东西我在这里错过了吗?

下面是完整的代码:

代码语言:javascript
复制
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>
        )
    }

}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-07-13 12:17:18

感谢Teemu在评论中发布了答案:

将事件更改为onInput按预期工作(当用户键入和粘贴事件时会触发事件)。更新代码:

代码语言:javascript
复制
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 onInput={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>
        )
    }

}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45079800

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档