Skip to content

Instantly share code, notes, and snippets.

@mitsuhiko
Created March 14, 2015 17:52
Show Gist options
  • Save mitsuhiko/72f303d5c6026f5d3369 to your computer and use it in GitHub Desktop.
Save mitsuhiko/72f303d5c6026f5d3369 to your computer and use it in GitHub Desktop.
var MultiLineTextInputWidget = React.createClass({
mixins: [BasicWidgetMixin],
onChange: function(event) {
this.recalculateSize();
if (this.props.onChange) {
this.props.onChange(event.target.value);
}
},
componentDidMount: function() {
this.recalculateSize();
window.addEventListener('resize', this.recalculateSize);
},
componentWillUnmount: function() {
window.removeEventListener('resize', this.recalculateSize);
},
componentDidUpdate: function(prevProps) {
this.recalculateSize();
},
isInAutoResizeMode: function() {
return this.props.rows === undefined;
},
recalculateSize: function() {
if (!this.isInAutoResizeMode()) {
return;
}
var diff;
var node = this.getDOMNode();
if (window.getComputedStyle) {
var s = window.getComputedStyle(node);
if (s.getPropertyValue('box-sizing') === 'border-box' ||
s.getPropertyValue('-moz-box-sizing') === 'border-box' ||
s.getPropertyValue('-webkit-box-sizing') === 'border-box') {
diff = 0;
} else {
diff = (
parseInt(s.getPropertyValue('padding-bottom') || 0, 10) +
parseInt(s.getPropertyValue('padding-top') || 0, 10)
);
}
} else {
diff = 0;
}
var node = this.refs.ta.getDOMNode();
node.style.height = 'auto';
node.style.height = (node.scrollHeight - diff) + 'px';
window.scrollTo(window.scrollLeft, document.body.scrollHeight);
},
render: function() {
var {className, type, onChange, style, ...otherProps} = this.props;
var className = (className || '');
style = style || {};
if (this.isInAutoResizeMode()) {
style.display = 'block';
style.overflow = 'hidden';
style.resize = 'none';
}
return (
<div className={className}>
<textarea
ref="ta"
className="form-control"
onChange={onChange ? this.onChange : undefined}
style={style}
{...otherProps} />
</div>
)
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment