Use autosize to automatically expand textareas

This commit is contained in:
Kimmo Puputti 2017-08-17 15:00:24 +03:00
parent d3cbae79a6
commit cdbcc55e9b

View file

@ -1,9 +1,36 @@
/* eslint-disable react/prefer-stateless-function, no-console */
import React, { Component } from 'react';
import autosize from 'autosize';
class ExpandingTextarea extends Component {
constructor(props) {
super(props);
this.textarea = null;
}
componentDidMount() {
// Delay the autosize initialisation so that the autosize can
// correctly calculate the height with the textarea value
window.setTimeout(
() => {
autosize(this.textarea);
},
100
);
}
componentDidUpdate() {
autosize.update(this.textarea);
}
componentWillUnmount() {
autosize.destroy(this.textarea);
}
render() {
return <textarea {...this.props} />;
return (
<textarea
{...this.props}
ref={textarea => {
this.textarea = textarea;
}}
/>
);
}
}