UNPKG

choerodon-ui

Version:

An enterprise-class UI design language and React-based implementation

266 lines (219 loc) 8.57 kB
import _objectSpread from "@babel/runtime/helpers/objectSpread2"; import _defineProperty from "@babel/runtime/helpers/defineProperty"; import _classCallCheck from "@babel/runtime/helpers/classCallCheck"; import _createClass from "@babel/runtime/helpers/createClass"; import _get from "@babel/runtime/helpers/get"; import _inherits from "@babel/runtime/helpers/inherits"; import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn"; import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf"; function _createSuper(Derived) { 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; } } return function () { var Super = _getPrototypeOf(Derived), result; if (isNativeReflectConstruct()) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; } import { __decorate } from "tslib"; import React from 'react'; import { observer } from 'mobx-react'; import { action, runInAction } from 'mobx'; import { TextField } from '../text-field/TextField'; import Icon from '../icon'; import { open } from '../modal-container/ModalContainer'; import SecretFieldView from './SecretFieldView'; import autobind from '../_util/autobind'; import CountDown from './CountDown'; var SecretField = /*#__PURE__*/ function (_TextField) { _inherits(SecretField, _TextField); var _super = _createSuper(SecretField); function SecretField(props, context) { var _this; _classCallCheck(this, SecretField); _this = _super.call(this, props, context); _this.secretEnable = false; runInAction(function () { _this.setSecretEnable(); }); return _this; } _createClass(SecretField, [{ key: "setSecretEnable", value: function setSecretEnable() { var secretFieldEnableConfig = this.getContextConfig('secretFieldEnable'); if (secretFieldEnableConfig) { // 从配置项获取是否开启脱敏组件 this.secretEnable = secretFieldEnableConfig(); } } }, { key: "openModal", value: function openModal() { var label = this.getLabel(); var readOnly = this.readOnly, name = this.name, record = this.record; if (!(record === null || record === void 0 ? void 0 : record.getState("_secretField_countDown_".concat(name)))) { record === null || record === void 0 ? void 0 : record.setState(_defineProperty({}, "_secretField_countDown_".concat(name), new CountDown())); } var pattern = this.getProp('pattern'); var restrict = this.getProp('restrict'); var required = this.getProp('required'); if (!this.modal) { var modalProps = this.props.modalProps; this.modal = open(_objectSpread({ title: label }, modalProps, { children: React.createElement(SecretFieldView, { readOnly: readOnly, name: name || '', record: record, label: label, pattern: pattern, restrict: restrict, required: required, token: record === null || record === void 0 ? void 0 : record.get('_token'), onChange: this.handleSecretChange, countDown: record === null || record === void 0 ? void 0 : record.getState("_secretField_countDown_".concat(name)) }), destroyOnClose: true, closable: true, autoFocus: false, footer: null, onClose: this.handleSecretFieldViewClose })); } } }, { key: "handleSecretFieldViewClose", value: function handleSecretFieldViewClose() { this.modal = undefined; } }, { key: "handleSecretChange", value: function handleSecretChange(data) { var _this$record; (_this$record = this.record) === null || _this$record === void 0 ? void 0 : _this$record.init(this.name || '', data); } }, { key: "handleOpenModal", value: function handleOpenModal() { return this.openModal(); } }, { key: "getSuffix", value: function getSuffix() { var readOnly = this.readOnly, record = this.record, name = this.name, isSecretEnable = this.isSecretEnable, props = this.props, disabled = this.disabled; // 未开启脱敏组件或者脱敏组件值为空时,不显示编辑/查看按钮 if (!isSecretEnable) { var suffix = props.suffix; return suffix ? this.wrapperSuffix(suffix) : null; } // 开启脱敏组件 // 编辑 if (!readOnly) { return this.wrapperSuffix(React.createElement(Icon, { type: 'edit-o' }), { onClick: disabled ? null : this.handleOpenModal }); } // 只读:已读不显示查看按钮 // readFlag: true已查看 undefined未查看 var readFlag = record === null || record === void 0 ? void 0 : record.getState("_secretField_queryFlag_".concat(name)); if (!readFlag && readOnly) { return this.wrapperSuffix(React.createElement(Icon, { type: 'visibility-o' }), { onClick: disabled ? null : this.handleOpenModal }); } return null; } }, { key: "isEditable", value: function isEditable() { return !this.isSecretEnable && _get(_getPrototypeOf(SecretField.prototype), "isEditable", this).call(this); } }, { key: "getWrapperClassNames", value: function getWrapperClassNames() { var _get2; var prefixCls = this.prefixCls; for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } return (_get2 = _get(_getPrototypeOf(SecretField.prototype), "getWrapperClassNames", this)).call.apply(_get2, [this].concat(args, [_defineProperty({}, "".concat(prefixCls, "-secret"), true)])); } }, { key: "getInnerSpanButton", value: function getInnerSpanButton() { var isSecretEnable = this.isSecretEnable; // 显示为脱敏组件时,禁用clearButton if (isSecretEnable) { return null; } // 显示为textField时,正常显示clearButton return _get(_getPrototypeOf(SecretField.prototype), "getInnerSpanButton", this).call(this); } }, { key: "clear", value: function clear() { var isSecretEnable = this.isSecretEnable; // 只有显示为textField时,退格键正常删除内容 if (!isSecretEnable) { _get(_getPrototypeOf(SecretField.prototype), "clear", this).call(this); } } }, { key: "isSecretEnable", get: function get() { var record = this.record, name = this.name; if (!(record === null || record === void 0 ? void 0 : record.get('_token')) || !(record === null || record === void 0 ? void 0 : record.get(name))) { // 新增数据,record没有token或者没有值,显示为textfield return false; } return this.secretEnable; } }, { key: "clearButton", get: function get() { var isSecretEnable = this.isSecretEnable; // 显示为脱敏组件时,clearButton为false if (isSecretEnable) { return false; } // 显示为textField时,正常显示clearButton return _get(_getPrototypeOf(SecretField.prototype), "clearButton", this); } }]); return SecretField; }(TextField); SecretField.displayName = 'SecretField'; SecretField.defaultProps = _objectSpread({}, TextField.defaultProps); SecretField.propTypes = _objectSpread({}, TextField.propTypes); __decorate([action], SecretField.prototype, "setSecretEnable", null); __decorate([action], SecretField.prototype, "openModal", null); __decorate([autobind, action], SecretField.prototype, "handleSecretFieldViewClose", null); __decorate([autobind], SecretField.prototype, "handleSecretChange", null); __decorate([autobind], SecretField.prototype, "handleOpenModal", null); __decorate([action], SecretField.prototype, "clear", null); SecretField = __decorate([observer], SecretField); export default SecretField; //# sourceMappingURL=SecretField.js.map