I am looking to create a textarea that initially displays as a single line but can expand up to 4 lines, and then start scrolling once the maximum height is reached. I have a solution in progress where it grows up to the limit, but does not shrink back down when text is deleted.
Here is my current code snippet:
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>
);
}
}
The issue lies in the fact that scrollHeight
does not decrease back to the original height when the height
is set larger and text is deleted. Any suggestions on how I can modify this so that it shrinks back down when text is removed?