Last active
May 24, 2016 22:43
-
-
Save kristoferjoseph/c8b2f0a3c2d0bcc0316a7662a2aab531 to your computer and use it in GitHub Desktop.
React Auto-resizing textinput
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, { Component } from 'react' | |
class TextInput extends Component { | |
constructor(props) { | |
super(props) | |
} | |
get value() { | |
return this.refs.textarea.value | |
} | |
set value(val) { | |
val = val || '' | |
this.refs.textarea.value = val | |
} | |
componentDidUpdate() { | |
let textarea = this.refs.textarea | |
let offset = textarea.offsetHeight - textarea.clientHeight | |
textarea.style.height = 'auto' | |
textarea.style.height = textarea.scrollHeight + offset + 'px' | |
textarea.scrollTop = textarea.scrollHeight | |
} | |
render() { | |
return ( | |
<textarea | |
rows='1' | |
{...this.props} | |
ref='textarea' | |
> | |
</textarea> | |
) | |
} | |
} | |
export default TextInput |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment