spee.ch/react/components/ExpandingTextArea/index.jsx
2018-03-08 09:26:33 -08:00

37 lines
833 B
JavaScript

import React, { Component } from 'react';
import PropTypes from 'prop-types';
class ExpandingTextarea extends Component {
constructor (props) {
super(props);
this._handleChange = this._handleChange.bind(this);
}
componentDidMount () {
this.adjustTextarea({});
}
_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`;
}
render () {
const { ...rest } = this.props;
return (
<textarea
{...rest}
ref={x => this.el = x}
onChange={this._handleChange}
/>
);
}
}
ExpandingTextarea.propTypes = {
onChange: PropTypes.func,
};
export default ExpandingTextarea;