@canner/antd-relation-single_select
Version:
relation single selection
184 lines (141 loc) • 7.59 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
require("antd/lib/icon/style");
var _icon = _interopRequireDefault(require("antd/lib/icon"));
require("antd/lib/tag/style");
var _tag = _interopRequireDefault(require("antd/lib/tag"));
var _react = _interopRequireWildcard(require("react"));
var _template = _interopRequireDefault(require("lodash/template"));
var _lodash = _interopRequireDefault(require("lodash.get"));
var _antdShareRelation = _interopRequireDefault(require("@canner/antd-share-relation"));
var _reactIntl = require("react-intl");
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } }
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _typeof(obj) { 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 _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
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 _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
var RelationOneId =
/*#__PURE__*/
function (_PureComponent) {
_inherits(RelationOneId, _PureComponent);
function RelationOneId(props) {
var _this;
_classCallCheck(this, RelationOneId);
_this = _possibleConstructorReturn(this, _getPrototypeOf(RelationOneId).call(this, props));
_defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "isOnComposition", void 0);
_defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "showModal", function () {
_this.setState({
modalVisible: true
});
});
_defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "handleOk", function (queue, originData) {
var _this$props = _this.props,
onChange = _this$props.onChange,
refId = _this$props.refId; // get the first one from picker
onChange(refId, 'connect', originData.find(function (item) {
return item.id === queue[0];
}));
_this.handleCancel();
});
_defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "handleCancel", function () {
_this.setState({
modalVisible: false
});
});
_defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "handleClose", function () {
var _this$props2 = _this.props,
onChange = _this$props2.onChange,
refId = _this$props2.refId,
value = _this$props2.value;
onChange(refId, 'disconnect', value);
});
_this.isOnComposition = false;
_this.state = {
modalVisible: false
};
return _this;
}
_createClass(RelationOneId, [{
key: "render",
value: function render() {
var modalVisible = this.state.modalVisible;
var _this$props3 = this.props,
disabled = _this$props3.disabled,
value = _this$props3.value,
uiParams = _this$props3.uiParams,
refId = _this$props3.refId,
relation = _this$props3.relation,
fetch = _this$props3.fetch,
fetchRelation = _this$props3.fetchRelation,
subscribe = _this$props3.subscribe,
updateQuery = _this$props3.updateQuery,
relationValue = _this$props3.relationValue,
Toolbar = _this$props3.Toolbar,
toolbar = _this$props3.toolbar,
rootValue = _this$props3.rootValue;
return _react.default.createElement("div", null, value && _react.default.createElement(_tag.default, {
key: value.id,
closable: true,
afterClose: this.handleClose,
style: {
fontSize: 16
}
}, getTag(value, uiParams)), _react.default.createElement(_tag.default, {
onClick: this.showModal,
style: {
background: '#fff',
borderStyle: 'dashed'
}
}, _react.default.createElement(_icon.default, {
type: "edit"
}), _react.default.createElement(_reactIntl.FormattedMessage, {
id: "relation.singleSelect.change",
defaultMessage: "Change"
})), !disabled && _react.default.createElement(_antdShareRelation.default, {
rootValue: rootValue,
fetch: fetch,
subscribe: subscribe,
fetchRelation: fetchRelation,
visible: modalVisible,
onOk: this.handleOk,
onCancel: this.handleCancel,
updateQuery: updateQuery,
pickOne: true,
relationValue: relationValue,
Toolbar: Toolbar,
toolbar: toolbar // $FlowFixMe
,
pickedIds: [value && value.id],
columns: uiParams.columns,
refId: refId,
relation: relation
}));
}
}]);
return RelationOneId;
}(_react.PureComponent);
exports.default = RelationOneId;
_defineProperty(RelationOneId, "defaultProps", {
uiParams: {}
});
function getTag(v, uiParams) {
var textCol = uiParams.textCol;
var type = _typeof(textCol);
if (type === 'function') {
return textCol(v);
}
if (type === 'string') {
return (0, _lodash.default)(v, textCol);
}
return textCol;
}