spee.ch/react/components/ExpandingTextArea/index.jsx

37 lines
745 B
React
Raw Normal View History

import React, { Component } from 'react';
import PropTypes from 'prop-types';
class ExpandingTextarea extends Component {
componentDidMount () {
this.adjustTextarea({});
}
render () {
2018-03-08 05:18:17 +01:00
const { ...rest } = this.props;
return (
<textarea
2018-03-08 05:18:17 +01:00
{...rest}
ref={x => this.el = x}
2018-03-08 05:18:17 +01:00
onChange={this._handleChange.bind(this)}
/>
);
}
_handleChange (event) {
const { onChange } = this.props;
if (onChange) onChange(event);
this.adjustTextarea(event);
}
adjustTextarea ({ target = this.el }) {
target.style.height = 0;
target.style.height = `${target.scrollHeight}px`;
}
}
ExpandingTextarea.propTypes = {
onChange: PropTypes.func,
};
export default ExpandingTextarea;