Last active
May 16, 2023 04:28
-
-
Save sikanhe/015c606710dc656f0c40 to your computer and use it in GitHub Desktop.
Simple textarea that expand and shrinks depending on the content
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 { | |
static propTypes = { | |
value: PropTypes.string, | |
className: PropTypes.string, | |
style: PropTypes.object, | |
onChange: PropTypes.func | |
} | |
componentDidMount() { | |
const textarea = this.refs.textarea | |
textarea.style.height = textarea.scrollHeight + 'px' | |
} | |
handleChange = e => { | |
const textarea = this.refs.textarea | |
textarea.style.height = 0 // has to set to 0 for shrinking | |
textarea.style.height = textarea.scrollHeight + 'px' | |
this.props.onChange(e.target.value) | |
} | |
render = () => ( | |
<textarea | |
{...this.props} | |
ref="textarea" | |
rows="1" | |
onChange={this.handleChange} | |
/> | |
) | |
} |
You can render a component twice and just hide one of the renderings so you're not resizing the input the user is manipulating
@moodysalem interesting approach
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Would making those two mutations reset your scroll position in some browsers?
Specifically: