polestar-ui-kit
Version:
## Install
467 lines (355 loc) • 16.4 kB
JavaScript
;
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); }
require("core-js/modules/es.array.concat");
require("core-js/modules/es.array.for-each");
require("core-js/modules/es.array.reduce");
require("core-js/modules/es.array.some");
require("core-js/modules/es.date.to-string");
require("core-js/modules/es.object.get-prototype-of");
require("core-js/modules/es.object.set-prototype-of");
require("core-js/modules/es.object.to-string");
require("core-js/modules/es.reflect.construct");
require("core-js/modules/es.regexp.to-string");
require("core-js/modules/web.dom-collections.for-each");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = void 0;
var _react = _interopRequireDefault(require("react"));
var _propTypes = _interopRequireDefault(require("prop-types"));
var _polestarIcons = _interopRequireDefault(require("polestar-icons"));
var _Button = _interopRequireDefault(require("../../Button"));
var _Grid = _interopRequireDefault(require("../../Grid"));
var _ = require("../..");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && Symbol.iterator in Object(iter)) return Array.from(iter); }
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
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); }
var GridToGrid = function (_React$Component) {
_inherits(GridToGrid, _React$Component);
var _super = _createSuper(GridToGrid);
function GridToGrid(props) {
var _this;
_classCallCheck(this, GridToGrid);
_this = _super.call(this, props);
_this.onChange = function (_ref) {
var type = _ref.type,
moveItems = _ref.moveItems,
targetItems = _ref.targetItems;
if (_this.props.onChange) {
_this.props.onChange({
type: type,
moveItems: moveItems,
targetItems: targetItems
});
}
};
_this.getSourceItems = function () {
var rowItems = [];
if (_this.sourceRef.current) {
_this.sourceRef.current.api.forEachNode(function (rowNode) {
rowItems.push(rowNode.data);
});
}
return rowItems;
};
_this.getTargetItems = function () {
var rowItems = [];
if (_this.targetRef.current) {
_this.targetRef.current.api.forEachNode(function (rowNode) {
rowItems.push(rowNode.data);
});
}
return rowItems;
};
_this.setAddItems = function (items) {
_this.addItems = items;
};
_this.setAddActive = function (b) {
_this.setState({
addActive: b
});
};
_this.setRemoveActive = function (b) {
_this.setState({
removeActive: b
});
};
_this.add = function (items) {
_this.addItems = items;
_this.handleAdd();
};
_this.remove = function (items) {
_this.removeItems = items;
_this.handleRemove();
};
_this.handleAdd = function () {
if (_this.props.onCustomAdd) {
_this.props.onCustomAdd(_assertThisInitialized(_this));
return;
}
var itemIdKey = _this.props.itemIdKey;
var _assertThisInitialize = _assertThisInitialized(_this),
addItems = _assertThisInitialize.addItems;
var moveItems = [];
var targetItems = _this.getTargetItems();
addItems.forEach(function (addItem) {
var isExist = targetItems.some(function (item) {
return item[itemIdKey] === addItem[itemIdKey];
});
if (!isExist) {
moveItems.push(addItem);
}
});
var resultTargetItems = [].concat(_toConsumableArray(targetItems), moveItems);
_this.targetRef.current.api.setRowData(resultTargetItems);
_this.targetItems = _this.getTargetItems();
_this.sourceRef.current.api.redrawRows();
_this.setState({
addActive: false
});
_this.onChange({
type: 'add',
moveItems: moveItems,
targetItems: _this.targetItems
});
_this.addItems = [];
};
_this.handleRemove = function () {
if (_this.props.onCustomRemove) {
_this.props.onCustomRemove(_assertThisInitialized(_this));
return;
}
var _assertThisInitialize2 = _assertThisInitialized(_this),
removeItems = _assertThisInitialize2.removeItems;
_this.targetRef.current.api.updateRowData({
remove: removeItems
});
_this.targetItems = _this.getTargetItems();
_this.sourceRef.current.api.redrawRows();
_this.setState({
addActive: true,
removeActive: false
});
_this.onChange({
type: 'remove',
moveItems: removeItems,
targetItems: _this.targetItems
});
_this.addItems = _this.sourceRef.current.api.getSelectedRows();
_this.removeItems = [];
};
_this.handleSourceSelectionChanged = function (e, rowItems) {
var itemIdKey = _this.props.itemIdKey;
_this.targetItems = _this.getTargetItems();
_this.addItems = _this.targetItems.reduce(function (accumulator, value) {
var isExist = accumulator.some(function (item) {
return item[itemIdKey] === value[itemIdKey];
});
if (!isExist) {
var targetItem;
_this.sourceRef.current.api.forEachNode(function (node) {
if (value[itemIdKey] === node.data[itemIdKey]) {
node.setSelected(true);
targetItem = node.data;
}
});
if (targetItem) {
accumulator.push(targetItem);
}
}
return accumulator;
}, rowItems);
var addActive = false;
if (rowItems && rowItems.length > 0) {
addActive = true;
}
_this.setState({
addActive: addActive
});
if (_this.props.source && _this.props.source.onSelectionChanged) {
_this.props.source.onSelectionChanged(e, rowItems);
}
};
_this.handleTargetSelectionChanged = function (e, rowItems) {
_this.removeItems = rowItems;
var removeActive = false;
if (rowItems && rowItems.length > 0) {
removeActive = true;
}
_this.setState({
removeActive: removeActive
});
if (_this.props.target && _this.props.target.onSelectionChanged) {
_this.props.target.onSelectionChanged(e, rowItems);
}
};
_this.handleSourceLoadComplete = function () {
_this.isSourceDataComplete = true;
if (!_this.isTargetDataComplete) return;
if (_this.isSourceDataComplete && _this.isTargetDataComplete) {
var targetItems = _this.getTargetItems();
_this.removeSourceItems(targetItems);
if (_this.props.onLoadComplete) {
_this.props.onLoadComplete(_this.getSourceItems(), targetItems);
}
}
};
_this.handleTargetLoadComplete = function () {
_this.isTargetDataComplete = true;
if (_this.isSourceDataComplete) {
_this.handleSourceLoadComplete();
}
};
_this.removeSourceItems = function (items) {
var itemIdKey = _this.props.itemIdKey;
_this.targetItems = items;
_this.sourceRef.current.api.forEachNode(function (node) {
items.some(function (item) {
if (item[itemIdKey] === node.data[itemIdKey]) {
node.setSelected(true);
return true;
}
return false;
});
});
_this.sourceRef.current.api.redrawRows();
};
_this.renderSourceHeader = function () {
var source = _this.props.source;
var iconEl = typeof source.icon === 'string' ? _react["default"].createElement(_polestarIcons["default"], {
name: source.icon
}) : source.icon;
return _react["default"].createElement("div", {
className: "ps-transfer-source-header"
}, iconEl, source.title);
};
_this.renderTargetHeader = function () {
var target = _this.props.target;
var iconEl = typeof target.icon === 'string' ? _react["default"].createElement(_polestarIcons["default"], {
name: target.icon
}) : target.icon;
return _react["default"].createElement("div", {
className: "ps-transfer-target-header"
}, iconEl, target.title);
};
_this.state = {
addActive: false,
removeActive: false
};
_this.isSourceDataComplete = false;
_this.isTargetDataComplete = false;
_this.sourceRef = _react["default"].createRef();
_this.targetRef = _react["default"].createRef();
return _this;
}
_createClass(GridToGrid, [{
key: "render",
value: function render() {
var _this2 = this;
var _this$props = this.props,
source = _this$props.source,
target = _this$props.target,
height = _this$props.height,
itemIdKey = _this$props.itemIdKey,
customRowDisabled = _this$props.customRowDisabled;
return _react["default"].createElement("div", {
className: "ps-transfer-grid"
}, _react["default"].createElement(_.Row, {
type: "flex",
align: "middle"
}, _react["default"].createElement(_.Col, {
className: "ps-transfer-source",
span: 11,
style: {
height: height
}
}, this.renderSourceHeader(), _react["default"].createElement(_Grid["default"], _extends({
ref: this.sourceRef
}, source, {
rowClassRules: {
'ag-row-disabled': customRowDisabled || function (params) {
if (_this2.targetItems) {
return _this2.targetItems.some(function (item) {
return item[itemIdKey] === params.data[itemIdKey];
});
}
return params.node.isSelected();
}
},
onLoadComplete: this.handleSourceLoadComplete,
onSelectionChanged: this.handleSourceSelectionChanged,
checkable: true
}))), _react["default"].createElement(_.Col, {
span: 2,
className: "ps-transfer-button"
}, _react["default"].createElement(_Button["default"], {
type: "circle",
icon: "caret-right",
onClick: this.handleAdd,
disabled: !this.state.addActive
}), _react["default"].createElement(_Button["default"], {
type: "circle",
icon: "caret-left",
onClick: this.handleRemove,
disabled: !this.state.removeActive
})), _react["default"].createElement(_.Col, {
className: "ps-transfer-target",
span: 11,
style: {
height: height
}
}, this.renderTargetHeader(), _react["default"].createElement(_Grid["default"], _extends({
ref: this.targetRef
}, target, {
onLoadComplete: this.handleTargetLoadComplete,
onSelectionChanged: this.handleTargetSelectionChanged,
checkable: true
})))));
}
}]);
return GridToGrid;
}(_react["default"].Component);
GridToGrid.propTypes = {
source: _propTypes["default"].shape({
title: _propTypes["default"].oneOfType([_propTypes["default"].string, _propTypes["default"].element]),
icon: _propTypes["default"].oneOfType([_propTypes["default"].string, _propTypes["default"].func])
}),
target: _propTypes["default"].shape({
title: _propTypes["default"].oneOfType([_propTypes["default"].string, _propTypes["default"].element]),
icon: _propTypes["default"].oneOfType([_propTypes["default"].string, _propTypes["default"].func])
}),
itemIdKey: _propTypes["default"].string,
height: _propTypes["default"].oneOfType([_propTypes["default"].number, _propTypes["default"].string]),
customRowDisabled: _propTypes["default"].func,
onChange: _propTypes["default"].func,
onLoadComplete: _propTypes["default"].func,
onCustomAdd: _propTypes["default"].func,
onCustomRemove: _propTypes["default"].func
};
GridToGrid.defaultProps = {
itemIdKey: 'id'
};
GridToGrid.validator = function (rule, value, callback) {
if (value === undefined || value === null || value.length === 0) {
callback();
} else {
callback();
}
};
var _default = GridToGrid;
exports["default"] = _default;