d2-ui
Version:
219 lines (181 loc) • 8.22 kB
JavaScript
Object.defineProperty(exports, "__esModule", {
value: true
});
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
var _simpleAssign = require('simple-assign');
var _simpleAssign2 = _interopRequireDefault(_simpleAssign);
var _react = require('react');
var _react2 = _interopRequireDefault(_react);
var _reactEventListener = require('react-event-listener');
var _reactEventListener2 = _interopRequireDefault(_reactEventListener);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
var rowsHeight = 24;
function getStyles(props, context, state) {
return {
root: {
position: 'relative' },
// because the shadow has position: 'absolute'
textarea: {
height: state.height,
width: '100%',
resize: 'none',
font: 'inherit',
padding: 0,
cursor: props.disabled ? 'default' : 'initial'
},
shadow: {
resize: 'none',
// Overflow also needed to here to remove the extra row
// added to textareas in Firefox.
overflow: 'hidden',
// Visibility needed to hide the extra text area on ipads
visibility: 'hidden',
position: 'absolute',
height: 'initial'
}
};
}
var EnhancedTextarea = function (_Component) {
_inherits(EnhancedTextarea, _Component);
function EnhancedTextarea() {
var _Object$getPrototypeO;
var _temp, _this, _ret;
_classCallCheck(this, EnhancedTextarea);
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
return _ret = (_temp = (_this = _possibleConstructorReturn(this, (_Object$getPrototypeO = Object.getPrototypeOf(EnhancedTextarea)).call.apply(_Object$getPrototypeO, [this].concat(args))), _this), _this.state = {
height: _this.props.rows * rowsHeight
}, _this.handleResize = function (event) {
_this.syncHeightWithShadow(undefined, event);
}, _this.handleChange = function (event) {
_this.syncHeightWithShadow(event.target.value);
if (_this.props.hasOwnProperty('valueLink')) {
_this.props.valueLink.requestChange(event.target.value);
}
if (_this.props.onChange) {
_this.props.onChange(event);
}
}, _temp), _possibleConstructorReturn(_this, _ret);
}
_createClass(EnhancedTextarea, [{
key: 'componentDidMount',
value: function componentDidMount() {
this.syncHeightWithShadow();
}
}, {
key: 'componentWillReceiveProps',
value: function componentWillReceiveProps(nextProps) {
if (nextProps.value !== this.props.value) {
this.syncHeightWithShadow(nextProps.value);
}
}
}, {
key: 'getInputNode',
value: function getInputNode() {
return this.refs.input;
}
}, {
key: 'setValue',
value: function setValue(value) {
this.getInputNode().value = value;
this.syncHeightWithShadow(value);
}
}, {
key: 'syncHeightWithShadow',
value: function syncHeightWithShadow(newValue, event) {
var shadow = this.refs.shadow;
if (newValue !== undefined) {
shadow.value = newValue;
}
var newHeight = shadow.scrollHeight;
if (this.props.rowsMax >= this.props.rows) {
newHeight = Math.min(this.props.rowsMax * rowsHeight, newHeight);
}
newHeight = Math.max(newHeight, rowsHeight);
if (this.state.height !== newHeight) {
this.setState({
height: newHeight
});
if (this.props.onHeightChange) {
this.props.onHeightChange(event, newHeight);
}
}
}
}, {
key: 'render',
value: function render() {
var _props = this.props;
var onChange = _props.onChange;
var // eslint-disable-line no-unused-vars
onHeightChange = _props.onHeightChange;
var // eslint-disable-line no-unused-vars
rows = _props.rows;
var // eslint-disable-line no-unused-vars
shadowStyle = _props.shadowStyle;
var style = _props.style;
var textareaStyle = _props.textareaStyle;
var valueLink = _props.valueLink;
var other = _objectWithoutProperties(_props, ['onChange', 'onHeightChange', 'rows', 'shadowStyle', 'style', 'textareaStyle', 'valueLink']);
var prepareStyles = this.context.muiTheme.prepareStyles;
var styles = getStyles(this.props, this.context, this.state);
var rootStyles = (0, _simpleAssign2.default)({}, styles.root, style);
var textareaStyles = (0, _simpleAssign2.default)({}, styles.textarea, textareaStyle);
var shadowStyles = (0, _simpleAssign2.default)({}, textareaStyles, styles.shadow, shadowStyle);
if (this.props.hasOwnProperty('valueLink')) {
other.value = this.props.valueLink.value;
}
return _react2.default.createElement(
'div',
{ style: prepareStyles(rootStyles) },
_react2.default.createElement(_reactEventListener2.default, { elementName: 'window', onResize: this.handleResize }),
_react2.default.createElement('textarea', {
ref: 'shadow',
style: prepareStyles(shadowStyles),
tabIndex: '-1',
rows: this.props.rows,
defaultValue: this.props.defaultValue,
readOnly: true,
value: this.props.value,
valueLink: this.props.valueLink
}),
_react2.default.createElement('textarea', _extends({}, other, {
ref: 'input',
rows: this.props.rows,
style: prepareStyles(textareaStyles),
onChange: this.handleChange
}))
);
}
}]);
return EnhancedTextarea;
}(_react.Component);
EnhancedTextarea.propTypes = {
defaultValue: _react.PropTypes.any,
disabled: _react.PropTypes.bool,
onChange: _react.PropTypes.func,
onHeightChange: _react.PropTypes.func,
rows: _react.PropTypes.number,
rowsMax: _react.PropTypes.number,
shadowStyle: _react.PropTypes.object,
/**
* Override the inline-styles of the root element.
*/
style: _react.PropTypes.object,
textareaStyle: _react.PropTypes.object,
value: _react.PropTypes.string,
valueLink: _react.PropTypes.object
};
EnhancedTextarea.defaultProps = {
rows: 1
};
EnhancedTextarea.contextTypes = {
muiTheme: _react.PropTypes.object.isRequired
};
exports.default = EnhancedTextarea;
;