【发布时间】:2017-01-17 00:14:38
【问题描述】:
我想要实现的是一个 textarea,它以单行开始,但会增长到 4 行,如果用户继续键入,此时开始滚动。我有一个部分解决方案有点工作,它会增长,然后在达到最大值时停止,但如果你删除文本,它不会像我想要的那样缩小。
这是我目前所拥有的。
export class foo extends React.Component {
constructor(props) {
super(props);
this.state = {
textareaHeight: 38
};
}
handleKeyUp(evt) {
// Max: 75px Min: 38px
let newHeight = Math.max(Math.min(evt.target.scrollHeight + 2, 75), 38);
if (newHeight !== this.state.textareaHeight) {
this.setState({
textareaHeight: newHeight
});
}
}
render() {
let textareaStyle = { height: this.state.textareaHeight };
return (
<div>
<textarea onKeyUp={this.handleKeyUp.bind(this)} style={textareaStyle}/>
</div>
);
}
}
很明显,当height 设置为更大的值时,scrollHeight 不会缩小。关于如何解决此问题的任何建议,以便在删除文本时它也会缩小?
【问题讨论】:
标签: javascript html css reactjs