Skip to content

Instantly share code, notes, and snippets.

@jef-sure
Last active April 1, 2016 13:26
Show Gist options
  • Save jef-sure/69768a8aabd4abe56e43daaee62fc689 to your computer and use it in GitHub Desktop.
Save jef-sure/69768a8aabd4abe56e43daaee62fc689 to your computer and use it in GitHub Desktop.
TextareaAutoSize -- yet another
import React, {PropTypes} from 'react'
export default class TextareaAutoSize extends React.Component {
constructor(props) {
super(props);
}
recalcHeight() {
var borders = 2;
var innerHeight = Math.floor(this.textarea.scrollHeight / 2) * 2 + 1;
this.textarea.style.height = (innerHeight + borders) + 'px';
}
componentDidMount() {
this.recalcHeight();
}
handleChange(e) {
this.textarea.style.height = 0;
this.recalcHeight();
if (this.props.onChange) this.props.onChange(e.target.value)
}
focus() {
if (this.textarea)
this.textarea.focus();
}
render() {
return <textarea
{...this.props}
ref={t => this.textarea = t}
rows="1"
onChange={e => this.handleChange(e)}
/>
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment