forked from Gitlink/forgeplus-react
336 lines
9.9 KiB
JavaScript
336 lines
9.9 KiB
JavaScript
"use strict";
|
|
|
|
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
|
|
Object.defineProperty(exports, "__esModule", {
|
|
value: true
|
|
});
|
|
exports.default = exports.styles = void 0;
|
|
|
|
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
|
|
|
|
var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));
|
|
|
|
var _getPrototypeOf = _interopRequireDefault(require("@babel/runtime/core-js/object/get-prototype-of"));
|
|
|
|
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
|
|
|
|
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
|
|
|
|
var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn"));
|
|
|
|
var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));
|
|
|
|
var _assertThisInitialized2 = _interopRequireDefault(require("@babel/runtime/helpers/assertThisInitialized"));
|
|
|
|
var _react = _interopRequireDefault(require("react"));
|
|
|
|
var _propTypes = _interopRequireDefault(require("prop-types"));
|
|
|
|
var _classnames = _interopRequireDefault(require("classnames"));
|
|
|
|
var _debounce = _interopRequireDefault(require("lodash/debounce"));
|
|
|
|
var _reactEventListener = _interopRequireDefault(require("react-event-listener"));
|
|
|
|
var _withStyles = _interopRequireDefault(require("../styles/withStyles"));
|
|
|
|
var ROWS_HEIGHT = 19;
|
|
var styles = {
|
|
root: {
|
|
position: 'relative',
|
|
// because the shadow has position: 'absolute',
|
|
width: '100%'
|
|
},
|
|
textarea: {
|
|
width: '100%',
|
|
height: '100%',
|
|
resize: 'none',
|
|
font: 'inherit',
|
|
padding: 0,
|
|
cursor: 'inherit',
|
|
boxSizing: 'border-box',
|
|
lineHeight: 'inherit',
|
|
border: 'none',
|
|
outline: 'none',
|
|
background: 'transparent'
|
|
},
|
|
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: 'auto',
|
|
whiteSpace: 'pre-wrap'
|
|
}
|
|
};
|
|
/**
|
|
* @ignore - internal component.
|
|
*/
|
|
|
|
exports.styles = styles;
|
|
|
|
var Textarea =
|
|
/*#__PURE__*/
|
|
function (_React$Component) {
|
|
(0, _inherits2.default)(Textarea, _React$Component);
|
|
|
|
function Textarea(props, context) {
|
|
var _this;
|
|
|
|
(0, _classCallCheck2.default)(this, Textarea);
|
|
_this = (0, _possibleConstructorReturn2.default)(this, (Textarea.__proto__ || (0, _getPrototypeOf.default)(Textarea)).call(this, props, context)); // <Input> expects the components it renders to respond to 'value'
|
|
// so that it can check whether they are filled.
|
|
|
|
Object.defineProperty((0, _assertThisInitialized2.default)(_this), "state", {
|
|
configurable: true,
|
|
enumerable: true,
|
|
writable: true,
|
|
value: {
|
|
height: null
|
|
}
|
|
});
|
|
Object.defineProperty((0, _assertThisInitialized2.default)(_this), "shadow", {
|
|
configurable: true,
|
|
enumerable: true,
|
|
writable: true,
|
|
value: null
|
|
});
|
|
Object.defineProperty((0, _assertThisInitialized2.default)(_this), "singlelineShadow", {
|
|
configurable: true,
|
|
enumerable: true,
|
|
writable: true,
|
|
value: null
|
|
});
|
|
Object.defineProperty((0, _assertThisInitialized2.default)(_this), "input", {
|
|
configurable: true,
|
|
enumerable: true,
|
|
writable: true,
|
|
value: null
|
|
});
|
|
Object.defineProperty((0, _assertThisInitialized2.default)(_this), "value", {
|
|
configurable: true,
|
|
enumerable: true,
|
|
writable: true,
|
|
value: null
|
|
});
|
|
Object.defineProperty((0, _assertThisInitialized2.default)(_this), "handleResize", {
|
|
configurable: true,
|
|
enumerable: true,
|
|
writable: true,
|
|
value: (0, _debounce.default)(function () {
|
|
_this.syncHeightWithShadow();
|
|
}, 166)
|
|
});
|
|
Object.defineProperty((0, _assertThisInitialized2.default)(_this), "handleRefInput", {
|
|
configurable: true,
|
|
enumerable: true,
|
|
writable: true,
|
|
value: function value(node) {
|
|
_this.input = node;
|
|
|
|
if (_this.props.textareaRef) {
|
|
_this.props.textareaRef(node);
|
|
}
|
|
}
|
|
});
|
|
Object.defineProperty((0, _assertThisInitialized2.default)(_this), "handleRefSinglelineShadow", {
|
|
configurable: true,
|
|
enumerable: true,
|
|
writable: true,
|
|
value: function value(node) {
|
|
_this.singlelineShadow = node;
|
|
}
|
|
});
|
|
Object.defineProperty((0, _assertThisInitialized2.default)(_this), "handleRefShadow", {
|
|
configurable: true,
|
|
enumerable: true,
|
|
writable: true,
|
|
value: function value(node) {
|
|
_this.shadow = node;
|
|
}
|
|
});
|
|
Object.defineProperty((0, _assertThisInitialized2.default)(_this), "handleChange", {
|
|
configurable: true,
|
|
enumerable: true,
|
|
writable: true,
|
|
value: function value(event) {
|
|
_this.value = event.target.value;
|
|
|
|
if (typeof _this.props.value === 'undefined' && _this.shadow) {
|
|
// The component is not controlled, we need to update the shallow value.
|
|
_this.shadow.value = _this.value;
|
|
|
|
_this.syncHeightWithShadow();
|
|
}
|
|
|
|
if (_this.props.onChange) {
|
|
_this.props.onChange(event);
|
|
}
|
|
}
|
|
});
|
|
_this.value = props.value || props.defaultValue || '';
|
|
_this.state = {
|
|
height: Number(props.rows) * ROWS_HEIGHT
|
|
};
|
|
return _this;
|
|
}
|
|
|
|
(0, _createClass2.default)(Textarea, [{
|
|
key: "componentDidMount",
|
|
value: function componentDidMount() {
|
|
this.syncHeightWithShadow();
|
|
}
|
|
}, {
|
|
key: "componentDidUpdate",
|
|
value: function componentDidUpdate() {
|
|
this.syncHeightWithShadow();
|
|
}
|
|
}, {
|
|
key: "componentWillUnmount",
|
|
value: function componentWillUnmount() {
|
|
this.handleResize.cancel();
|
|
}
|
|
}, {
|
|
key: "syncHeightWithShadow",
|
|
// Corresponds to 10 frames at 60 Hz.
|
|
value: function syncHeightWithShadow() {
|
|
var props = this.props;
|
|
|
|
if (!this.shadow || !this.singlelineShadow) {
|
|
return;
|
|
} // The component is controlled, we need to update the shallow value.
|
|
|
|
|
|
if (typeof props.value !== 'undefined') {
|
|
this.shadow.value = props.value == null ? '' : String(props.value);
|
|
}
|
|
|
|
var lineHeight = this.singlelineShadow.scrollHeight;
|
|
var newHeight = this.shadow.scrollHeight; // Guarding for jsdom, where scrollHeight isn't present.
|
|
// See https://github.com/tmpvar/jsdom/issues/1013
|
|
|
|
if (newHeight === undefined) {
|
|
return;
|
|
}
|
|
|
|
if (Number(props.rowsMax) >= Number(props.rows)) {
|
|
newHeight = Math.min(Number(props.rowsMax) * lineHeight, newHeight);
|
|
}
|
|
|
|
newHeight = Math.max(newHeight, lineHeight); // Need a large enough different to update the height.
|
|
// This prevents infinite rendering loop.
|
|
|
|
if (Math.abs(this.state.height - newHeight) > 1) {
|
|
this.setState({
|
|
height: newHeight
|
|
});
|
|
}
|
|
}
|
|
}, {
|
|
key: "render",
|
|
value: function render() {
|
|
var _props = this.props,
|
|
classes = _props.classes,
|
|
className = _props.className,
|
|
defaultValue = _props.defaultValue,
|
|
onChange = _props.onChange,
|
|
rows = _props.rows,
|
|
rowsMax = _props.rowsMax,
|
|
textareaRef = _props.textareaRef,
|
|
value = _props.value,
|
|
other = (0, _objectWithoutProperties2.default)(_props, ["classes", "className", "defaultValue", "onChange", "rows", "rowsMax", "textareaRef", "value"]);
|
|
return _react.default.createElement("div", {
|
|
className: classes.root,
|
|
style: {
|
|
height: this.state.height
|
|
}
|
|
}, _react.default.createElement(_reactEventListener.default, {
|
|
target: "window",
|
|
onResize: this.handleResize
|
|
}), _react.default.createElement("textarea", {
|
|
ref: this.handleRefSinglelineShadow,
|
|
className: (0, _classnames.default)(classes.shadow, classes.textarea),
|
|
tabIndex: -1,
|
|
rows: "1",
|
|
readOnly: true,
|
|
"aria-hidden": "true",
|
|
value: ""
|
|
}), _react.default.createElement("textarea", {
|
|
ref: this.handleRefShadow,
|
|
className: (0, _classnames.default)(classes.shadow, classes.textarea),
|
|
tabIndex: -1,
|
|
rows: rows,
|
|
"aria-hidden": "true",
|
|
readOnly: true,
|
|
defaultValue: defaultValue,
|
|
value: value
|
|
}), _react.default.createElement("textarea", (0, _extends2.default)({
|
|
rows: rows,
|
|
className: (0, _classnames.default)(classes.textarea, className),
|
|
defaultValue: defaultValue,
|
|
value: value,
|
|
onChange: this.handleChange,
|
|
ref: this.handleRefInput
|
|
}, other)));
|
|
}
|
|
}]);
|
|
return Textarea;
|
|
}(_react.default.Component);
|
|
|
|
Textarea.propTypes = process.env.NODE_ENV !== "production" ? {
|
|
/**
|
|
* Useful to extend the style applied to components.
|
|
*/
|
|
classes: _propTypes.default.object.isRequired,
|
|
|
|
/**
|
|
* @ignore
|
|
*/
|
|
className: _propTypes.default.string,
|
|
|
|
/**
|
|
* @ignore
|
|
*/
|
|
defaultValue: _propTypes.default.oneOfType([_propTypes.default.string, _propTypes.default.number]),
|
|
|
|
/**
|
|
* @ignore
|
|
*/
|
|
disabled: _propTypes.default.bool,
|
|
|
|
/**
|
|
* @ignore
|
|
*/
|
|
onChange: _propTypes.default.func,
|
|
|
|
/**
|
|
* Number of rows to display when multiline option is set to true.
|
|
*/
|
|
rows: _propTypes.default.oneOfType([_propTypes.default.string, _propTypes.default.number]),
|
|
|
|
/**
|
|
* Maximum number of rows to display when multiline option is set to true.
|
|
*/
|
|
rowsMax: _propTypes.default.oneOfType([_propTypes.default.string, _propTypes.default.number]),
|
|
|
|
/**
|
|
* Use that property to pass a ref callback to the native textarea element.
|
|
*/
|
|
textareaRef: _propTypes.default.func,
|
|
|
|
/**
|
|
* @ignore
|
|
*/
|
|
value: _propTypes.default.oneOfType([_propTypes.default.string, _propTypes.default.number])
|
|
} : {};
|
|
Textarea.defaultProps = {
|
|
rows: 1
|
|
};
|
|
|
|
var _default = (0, _withStyles.default)(styles)(Textarea);
|
|
|
|
exports.default = _default; |