Last active
August 29, 2015 14:02
-
-
Save rf-/1a7b00085c16de0ae517 to your computer and use it in GitHub Desktop.
Resizing text area in JSX
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
.resizing-text-area__container { | |
position: relative; | |
} | |
.resizing-text-area__input { | |
height: 100%; | |
overflow: hidden; | |
position: absolute; | |
resize: none; | |
width: 100%; | |
} | |
.resizing-text-area__shadow { | |
white-space: pre-wrap; | |
width: 100%; | |
visibility: hidden; | |
} |
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
/** @jsx React.DOM */ | |
'use strict'; | |
var React = require('react'); | |
var ResizingTextArea = React.createClass({ | |
getDefaultProps: function() { | |
return { className: '' }; | |
}, | |
render: function() { | |
var shadowValue = this.props.value; | |
// A final newline gets collapsed, so we have to add an extra one. | |
if (shadowValue.length === 0 || | |
shadowValue[shadowValue.length - 1] === "\n") { | |
shadowValue = shadowValue.concat("\n"); | |
} | |
return ( | |
<div className='resizing-text-area__container'> | |
<textarea | |
className={'resizing-text-area__input ' + this.props.className} | |
value={this.props.value} | |
onChange={this.props.onChange} | |
onKeyDown={this.props.onKeyDown} /> | |
<div className={'resizing-text-area__shadow ' + this.props.className}> | |
{shadowValue} | |
</div> | |
</div> | |
); | |
} | |
}); | |
module.exports = ResizingTextArea; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment