240 lines
7.4 KiB
JavaScript
240 lines
7.4 KiB
JavaScript
|
|
'use strict';
|
||
|
|
|
||
|
|
Object.defineProperty(exports, "__esModule", {
|
||
|
|
value: true
|
||
|
|
});
|
||
|
|
|
||
|
|
var _extends2 = require('babel-runtime/helpers/extends');
|
||
|
|
|
||
|
|
var _extends3 = _interopRequireDefault(_extends2);
|
||
|
|
|
||
|
|
var _objectWithoutProperties2 = require('babel-runtime/helpers/objectWithoutProperties');
|
||
|
|
|
||
|
|
var _objectWithoutProperties3 = _interopRequireDefault(_objectWithoutProperties2);
|
||
|
|
|
||
|
|
var _getPrototypeOf = require('babel-runtime/core-js/object/get-prototype-of');
|
||
|
|
|
||
|
|
var _getPrototypeOf2 = _interopRequireDefault(_getPrototypeOf);
|
||
|
|
|
||
|
|
var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');
|
||
|
|
|
||
|
|
var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);
|
||
|
|
|
||
|
|
var _createClass2 = require('babel-runtime/helpers/createClass');
|
||
|
|
|
||
|
|
var _createClass3 = _interopRequireDefault(_createClass2);
|
||
|
|
|
||
|
|
var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');
|
||
|
|
|
||
|
|
var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);
|
||
|
|
|
||
|
|
var _inherits2 = require('babel-runtime/helpers/inherits');
|
||
|
|
|
||
|
|
var _inherits3 = _interopRequireDefault(_inherits2);
|
||
|
|
|
||
|
|
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 }; }
|
||
|
|
|
||
|
|
var rowsHeight = 24;
|
||
|
|
|
||
|
|
function getStyles(props, context, state) {
|
||
|
|
return {
|
||
|
|
root: {
|
||
|
|
position: 'relative' },
|
||
|
|
textarea: {
|
||
|
|
height: state.height,
|
||
|
|
width: '100%',
|
||
|
|
resize: 'none',
|
||
|
|
font: 'inherit',
|
||
|
|
padding: 0,
|
||
|
|
cursor: 'inherit'
|
||
|
|
},
|
||
|
|
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) {
|
||
|
|
(0, _inherits3.default)(EnhancedTextarea, _Component);
|
||
|
|
|
||
|
|
function EnhancedTextarea() {
|
||
|
|
var _ref;
|
||
|
|
|
||
|
|
var _temp, _this, _ret;
|
||
|
|
|
||
|
|
(0, _classCallCheck3.default)(this, EnhancedTextarea);
|
||
|
|
|
||
|
|
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
|
||
|
|
args[_key] = arguments[_key];
|
||
|
|
}
|
||
|
|
|
||
|
|
return _ret = (_temp = (_this = (0, _possibleConstructorReturn3.default)(this, (_ref = EnhancedTextarea.__proto__ || (0, _getPrototypeOf2.default)(EnhancedTextarea)).call.apply(_ref, [this].concat(args))), _this), _this.state = {
|
||
|
|
height: null
|
||
|
|
}, _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), (0, _possibleConstructorReturn3.default)(_this, _ret);
|
||
|
|
}
|
||
|
|
|
||
|
|
(0, _createClass3.default)(EnhancedTextarea, [{
|
||
|
|
key: 'componentWillMount',
|
||
|
|
value: function componentWillMount() {
|
||
|
|
this.setState({
|
||
|
|
height: this.props.rows * rowsHeight
|
||
|
|
});
|
||
|
|
}
|
||
|
|
}, {
|
||
|
|
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;
|
||
|
|
|
||
|
|
// Guarding for jsdom, where scrollHeight isn't present.
|
||
|
|
// See https://github.com/tmpvar/jsdom/issues/1013
|
||
|
|
if (newHeight === undefined) return;
|
||
|
|
|
||
|
|
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,
|
||
|
|
onChange = _props.onChange,
|
||
|
|
onHeightChange = _props.onHeightChange,
|
||
|
|
rows = _props.rows,
|
||
|
|
rowsMax = _props.rowsMax,
|
||
|
|
shadowStyle = _props.shadowStyle,
|
||
|
|
style = _props.style,
|
||
|
|
textareaStyle = _props.textareaStyle,
|
||
|
|
valueLink = _props.valueLink,
|
||
|
|
other = (0, _objectWithoutProperties3.default)(_props, ['onChange', 'onHeightChange', 'rows', 'rowsMax', '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, { target: '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', (0, _extends3.default)({}, other, {
|
||
|
|
ref: 'input',
|
||
|
|
rows: this.props.rows,
|
||
|
|
style: prepareStyles(textareaStyles),
|
||
|
|
onChange: this.handleChange
|
||
|
|
}))
|
||
|
|
);
|
||
|
|
}
|
||
|
|
}]);
|
||
|
|
return EnhancedTextarea;
|
||
|
|
}(_react.Component);
|
||
|
|
|
||
|
|
EnhancedTextarea.defaultProps = {
|
||
|
|
rows: 1
|
||
|
|
};
|
||
|
|
EnhancedTextarea.contextTypes = {
|
||
|
|
muiTheme: _react.PropTypes.object.isRequired
|
||
|
|
};
|
||
|
|
process.env.NODE_ENV !== "production" ? 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
|
||
|
|
} : void 0;
|
||
|
|
exports.default = EnhancedTextarea;
|