choerodon-ui
Version:
An enterprise-class UI design language and React-based implementation
363 lines (311 loc) • 12.7 kB
JavaScript
import _objectSpread from "@babel/runtime/helpers/objectSpread2";
import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator";
import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
import _createClass from "@babel/runtime/helpers/createClass";
import _assertThisInitialized from "@babel/runtime/helpers/assertThisInitialized";
import _get from "@babel/runtime/helpers/get";
import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf";
import _inherits from "@babel/runtime/helpers/inherits";
import _createSuper from "@babel/runtime/helpers/createSuper";
import _regeneratorRuntime from "@babel/runtime/regenerator";
import { __decorate } from "tslib";
import React from 'react';
import { observer } from 'mobx-react';
import { action, observable } from 'mobx';
import { ProgressType } from '../../../es/progress/enum';
import KeyCode from '../../../es/_util/KeyCode';
import TextArea from '../text-area/TextArea';
import { TextField } from '../text-field/TextField';
import { ResizeType } from '../text-area/enum';
import Icon from '../icon';
import { open } from '../modal-container/ModalContainer';
import IntlList from './IntlList';
import { IntlType } from './enum';
import localeContext, { $l } from '../locale-context';
import Progress from '../progress';
import { Size } from '../core/enum';
import message from '../message';
import exception from '../_util/exception';
import autobind from '../_util/autobind';
import { stopEvent } from '../_util/EventManager';
import isSame from '../_util/isSame';
var IntlField = /*#__PURE__*/function (_TextArea) {
_inherits(IntlField, _TextArea);
var _super = _createSuper(IntlField);
function IntlField(props, context) {
var _this;
_classCallCheck(this, IntlField);
_this = _super.call(this, props, context);
_this.openModal = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
var _this$props, modalProps, maxLengths, type, rows, cols, resize, _assertThisInitialize, record, lang, name, element, supports, maxLengthList;
return _regeneratorRuntime.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
if (_this.modal) {
_context.next = 23;
break;
}
_this$props = _this.props, modalProps = _this$props.modalProps, maxLengths = _this$props.maxLengths, type = _this$props.type, rows = _this$props.rows, cols = _this$props.cols, resize = _this$props.resize;
_assertThisInitialize = _assertThisInitialized(_this), record = _assertThisInitialize.record, lang = _assertThisInitialize.lang, name = _assertThisInitialize.name, element = _assertThisInitialize.element;
supports = localeContext.supports;
maxLengthList = {};
Object.keys(supports).map(function (key) {
maxLengthList[key] = maxLengths && key !== lang ? maxLengths[key] || element.maxLength : element.maxLength;
return null;
});
if (!record) {
_context.next = 21;
break;
}
_this.setLoading(true);
_context.prev = 8;
if (element && !isSame(_this.getValue(), element.value)) {
_this.syncValueOnBlur(element.value);
}
_context.next = 12;
return record.tls(name);
case 12:
_context.next = 18;
break;
case 14:
_context.prev = 14;
_context.t0 = _context["catch"](8);
message.error(exception(_context.t0));
return _context.abrupt("return");
case 18:
_context.prev = 18;
_this.setLoading(false);
return _context.finish(18);
case 21:
_this.storeLocales();
_this.modal = open(_objectSpread({
title: $l('IntlField', 'modal_title'),
children: /*#__PURE__*/React.createElement(IntlList, {
readOnly: _this.readOnly,
disabled: _this.disabled,
record: record,
name: name,
lang: lang,
maxLengths: maxLengthList,
type: type,
rows: rows,
cols: cols,
resize: resize,
getConfig: _this.getContextConfig
}),
onClose: _this.handleIntlListClose,
onOk: _this.handleIntlListOk,
onCancel: _this.handleIntlListCancel,
okButton: !_this.disabled && !_this.readOnly,
destroyOnClose: true
}, modalProps));
case 23:
case "end":
return _context.stop();
}
}
}, _callee, null, [[8, 14, 18, 21]]);
}));
_this.handleIntlListClose = /*#__PURE__*/_asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2() {
return _regeneratorRuntime.wrap(function _callee2$(_context2) {
while (1) {
switch (_context2.prev = _context2.next) {
case 0:
delete _this.modal;
_this.focus();
case 2:
case "end":
return _context2.stop();
}
}
}, _callee2);
}));
var suffixCls = _this.props.type !== IntlType.multipleLine ? 'input' : 'textarea';
_this.prefixCls = _this.getContextProPrefixCls(suffixCls, props.prefixCls);
return _this;
}
_createClass(IntlField, [{
key: "setLoading",
value: function setLoading(loading) {
this.loading = loading;
}
}, {
key: "handleIntlListOk",
value: function () {
var _handleIntlListOk = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee3() {
var supports, languages, record, name, field, tlsKey;
return _regeneratorRuntime.wrap(function _callee3$(_context3) {
while (1) {
switch (_context3.prev = _context3.next) {
case 0:
supports = localeContext.supports;
languages = Object.keys(supports);
record = this.record, name = this.name, field = this.field;
if (!(record && field)) {
_context3.next = 8;
break;
}
tlsKey = this.getContextConfig('tlsKey');
_context3.next = 7;
return Promise.all(languages.map(function (language) {
var intlField = record.dataSet.getField("".concat(tlsKey, ".").concat(name, ".").concat(language));
return intlField ? intlField.checkValidity(record) : true;
}));
case 7:
return _context3.abrupt("return", _context3.sent.every(Boolean));
case 8:
case "end":
return _context3.stop();
}
}
}, _callee3, this);
}));
function handleIntlListOk() {
return _handleIntlListOk.apply(this, arguments);
}
return handleIntlListOk;
}()
}, {
key: "handleIntlListCancel",
value: function () {
var _handleIntlListCancel = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee4() {
var name, record, tlsKey;
return _regeneratorRuntime.wrap(function _callee4$(_context4) {
while (1) {
switch (_context4.prev = _context4.next) {
case 0:
name = this.name, record = this.record;
if (record) {
tlsKey = this.getContextConfig('tlsKey');
record.set("".concat(tlsKey, ".").concat(name), this.locales);
}
case 2:
case "end":
return _context4.stop();
}
}
}, _callee4, this);
}));
function handleIntlListCancel() {
return _handleIntlListCancel.apply(this, arguments);
}
return handleIntlListCancel;
}()
}, {
key: "handleKeyDown",
value: function handleKeyDown(e) {
if (e.keyCode === KeyCode.DOWN && this.props.type !== IntlType.multipleLine) {
stopEvent(e);
this.openModal();
}
_get(_getPrototypeOf(IntlField.prototype), "handleKeyDown", this).call(this, e);
}
}, {
key: "handleBlur",
value: function handleBlur(e) {
if (this.modal) {
e.preventDefault();
}
_get(_getPrototypeOf(IntlField.prototype), "handleBlur", this).call(this, e);
}
}, {
key: "storeLocales",
value: function storeLocales() {
var name = this.name,
record = this.record;
if (record) {
var tlsKey = this.getContextConfig('tlsKey');
this.locales = _objectSpread({}, record.get("".concat(tlsKey, ".").concat(name)));
}
}
}, {
key: "getOmitPropsKeys",
value: function getOmitPropsKeys() {
if (this.props.type === IntlType.multipleLine) {
return _get(_getPrototypeOf(IntlField.prototype), "getOmitPropsKeys", this).call(this).concat(['type']);
}
return _get(_getPrototypeOf(IntlField.prototype), "getOmitPropsKeys", this).call(this).concat(['cols', 'rows', 'wrap', 'resize', 'autoSize', 'onResize', 'type']);
}
}, {
key: "getOtherProps",
value: function getOtherProps() {
if (this.props.type === IntlType.multipleLine) {
return _get(_getPrototypeOf(IntlField.prototype), "getOtherProps", this).call(this);
}
return TextField.prototype.getOtherProps.call(this);
}
}, {
key: "getWrapperClassNames",
value: function getWrapperClassNames() {
var _get2;
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
return (_get2 = _get(_getPrototypeOf(IntlField.prototype), "getWrapperClassNames", this)).call.apply(_get2, [this, "".concat(this.prefixCls, "-intl")].concat(args));
}
}, {
key: "getSuffix",
value: function getSuffix() {
var suffix = this.props.suffix;
return this.wrapperSuffix(this.loading ? /*#__PURE__*/React.createElement(Progress, {
size: Size.small,
type: ProgressType.loading
}) : suffix || /*#__PURE__*/React.createElement(Icon, {
className: "".concat(this.prefixCls, "-intl"),
type: "language"
}), {
onClick: this.openModal
});
}
}, {
key: "handleEnterDown",
value: function handleEnterDown(e) {
if (this.props.type === IntlType.multipleLine) {
_get(_getPrototypeOf(IntlField.prototype), "handleEnterDown", this).call(this, e);
} else {
TextField.prototype.handleEnterDown.call(this, e);
}
}
}, {
key: "componentWillReceiveProps",
value: function componentWillReceiveProps(nextProps, nextContext) {
if (nextProps.type !== this.props.type) {
var suffixCls = nextProps.type !== IntlType.multipleLine ? 'input' : 'textarea';
this.prefixCls = this.getContextProPrefixCls(suffixCls, nextProps.prefixCls);
}
_get(_getPrototypeOf(IntlField.prototype), "componentWillReceiveProps", this).call(this, nextProps, nextContext);
}
}, {
key: "componentWillUnmount",
value: function componentWillUnmount() {
if (this.modal) {
this.modal.close();
}
}
}, {
key: "renderWrapper",
value: function renderWrapper() {
if (this.props.type === IntlType.multipleLine) {
return _get(_getPrototypeOf(IntlField.prototype), "renderWrapper", this).call(this);
}
return this.renderGroup();
}
}]);
return IntlField;
}(TextArea);
IntlField.displayName = 'IntlField';
IntlField.defaultProps = _objectSpread(_objectSpread({}, TextArea.defaultProps), {}, {
rows: 3,
resize: ResizeType.vertical,
type: IntlType.singleLine
});
__decorate([observable], IntlField.prototype, "loading", void 0);
__decorate([action], IntlField.prototype, "setLoading", null);
__decorate([autobind], IntlField.prototype, "handleIntlListOk", null);
__decorate([autobind], IntlField.prototype, "handleIntlListCancel", null);
__decorate([autobind], IntlField.prototype, "handleKeyDown", null);
__decorate([autobind], IntlField.prototype, "handleBlur", null);
IntlField = __decorate([observer], IntlField);
export default IntlField;
//# sourceMappingURL=IntlField.js.map