Skip to content

Instantly share code, notes, and snippets.

@kristoferjoseph
Last active May 24, 2016 22:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kristoferjoseph/c8b2f0a3c2d0bcc0316a7662a2aab531 to your computer and use it in GitHub Desktop.
Save kristoferjoseph/c8b2f0a3c2d0bcc0316a7662a2aab531 to your computer and use it in GitHub Desktop.
React Auto-resizing textinput
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