@pubsweet/ui
Version:
React component library for use in pubsweet apps
160 lines (122 loc) • 7.24 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = void 0;
var _react = _interopRequireDefault(require("react"));
var _styledComponents = _interopRequireDefault(require("styled-components"));
var _propTypes = _interopRequireDefault(require("prop-types"));
var _uiToolkit = require("@pubsweet/ui-toolkit");
var _atoms = require("../atoms");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a 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); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Date.prototype.toString.call(Reflect.construct(Date, [], function () {})); return true; } catch (e) { return false; } }
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
function _templateObject2() {
var data = _taggedTemplateLiteral(["\n padding: calc(", " - ", ") ", ";\n border: ", " ", " transparent;\n border-radius: ", ";\n cursor: pointer;\n\n &:hover {\n border-color: ", ";\n }\n\n ", ";\n"]);
_templateObject2 = function _templateObject2() {
return data;
};
return data;
}
function _templateObject() {
var data = _taggedTemplateLiteral(["\n width: calc(", " * 42);\n margin-bottom: ", ";\n"]);
_templateObject = function _templateObject() {
return data;
};
return data;
}
function _taggedTemplateLiteral(strings, raw) { if (!raw) { raw = strings.slice(0); } return Object.freeze(Object.defineProperties(strings, { raw: { value: Object.freeze(raw) } })); }
var Root = _styledComponents["default"].div(_templateObject(), (0, _uiToolkit.th)('gridUnit'), function (props) {
return props.inline ? '0' : "calc(".concat(props.theme.gridUnit, " * 3) ");
});
var Value = _styledComponents["default"].div(_templateObject2(), (0, _uiToolkit.th)('gridUnit'), (0, _uiToolkit.th)('borderWidth'), (0, _uiToolkit.th)('gridUnit'), (0, _uiToolkit.th)('borderWidth'), (0, _uiToolkit.th)('borderStyle'), (0, _uiToolkit.th)('borderRadius'), (0, _uiToolkit.th)('colorSecondary'), (0, _uiToolkit.override)('ui.EditableValue'));
var ESCAPE_KEY_CODE = 27;
var EditableValue = /*#__PURE__*/function (_React$Component) {
_inherits(EditableValue, _React$Component);
var _super = _createSuper(EditableValue);
function EditableValue(props) {
var _this;
_classCallCheck(this, EditableValue);
_this = _super.call(this, props);
_this._save = _this._save.bind(_assertThisInitialized(_this));
_this._onChange = _this._onChange.bind(_assertThisInitialized(_this));
_this._onClick = _this._onClick.bind(_assertThisInitialized(_this));
_this._onKeyDown = _this._onKeyDown.bind(_assertThisInitialized(_this));
_this._storedValue = _this.props.value;
_this.state = {
value: _this._storedValue,
editing: false
};
return _this;
}
_createClass(EditableValue, [{
key: "_save",
value: function _save() {
if (typeof this.props.onSave === 'function') {
this.props.onSave(this.state.value);
}
this._storedValue = this.state.value;
this.setState({
editing: false
});
}
}, {
key: "_onChange",
value: function _onChange(event) {
this.setState({
value: event.target.value
});
}
}, {
key: "_onClick",
value: function _onClick(event) {
this.setState({
editing: true
});
}
}, {
key: "_onKeyDown",
value: function _onKeyDown(event) {
if (event.keyCode === ESCAPE_KEY_CODE) {
this.setState({
value: this._storedValue,
editing: false
});
}
}
}, {
key: "render",
value: function render() {
if (this.state.editing) {
return /*#__PURE__*/_react["default"].createElement(Root, null, /*#__PURE__*/_react["default"].createElement("form", {
onSubmit: this._save
}, /*#__PURE__*/_react["default"].createElement(_atoms.TextField, {
inline: this.props.inline,
onChange: this._onChange,
onKeyDown: this._onKeyDown,
required: this.props.required,
value: this.state.value
})));
}
return /*#__PURE__*/_react["default"].createElement(Root, null, /*#__PURE__*/_react["default"].createElement(Value, {
onClick: this._onClick
}, this.state.value));
}
}]);
return EditableValue;
}(_react["default"].Component);
exports["default"] = EditableValue;
EditableValue.propTypes = {
required: _propTypes["default"].bool,
onSave: _propTypes["default"].func.isRequired,
value: _propTypes["default"].string
};