spee.ch/react/components/ExpandingTextArea/index.jsx
2018-03-07 20:18:17 -08:00

36 lines
745 B
JavaScript

import React, { Component } from 'react';
import PropTypes from 'prop-types';
class ExpandingTextarea extends Component {
componentDidMount () {
this.adjustTextarea({});
}
render () {
const { ...rest } = this.props;
return (
<textarea
{...rest}
ref={x => this.el = x}
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;