Last active
April 1, 2016 13:26
-
-
Save jef-sure/69768a8aabd4abe56e43daaee62fc689 to your computer and use it in GitHub Desktop.
TextareaAutoSize -- yet another
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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