UNPKG

@aliretail/react-materials-components

Version:
152 lines (122 loc) 5.55 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); exports.__esModule = true; exports["default"] = void 0; var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends")); var React = _interopRequireWildcard(require("react")); var _SelectLogic = _interopRequireDefault(require("./SelectLogic")); var _SelectCondition = _interopRequireDefault(require("./SelectCondition")); var _classnames = _interopRequireDefault(require("classnames")); var _utils = require("./utils"); function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); } function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; } /* eslint-disable react/prop-types */ var LoginConditionGroup = function LoginConditionGroup(props) { var group = props.group, onRemove = props.onRemove, conditionRemovable = props.conditionRemovable, onUpdate = props.onUpdate, useCompact = props.useCompact; var calcType = group.calcType, id = group.id, _group$conditions = group.conditions, conditions = _group$conditions === void 0 ? [] : _group$conditions; /** 修改 且、或 */ var handleLogicChange = function handleLogicChange(v) { onUpdate(id, (0, _extends2["default"])({}, group, { calcType: v })); }; /** 新增条件 */ var handleConditionAdd = function handleConditionAdd(newCondition, index) { var newConditions = conditions.concat(newCondition); if (index !== undefined) { newConditions.splice(index, 0, newCondition); newConditions.pop(); } onUpdate(id, (0, _extends2["default"])({}, group, { conditions: newConditions })); }; /** 删除条件 */ var handleConditionRemove = function handleConditionRemove(conditionId) { var newConditions = conditions.filter(function (c) { return c.id !== conditionId; }); // 只有一条了,条件要改为且 if (newConditions.length === 1) { onUpdate(id, (0, _extends2["default"])({}, group, { calcType: 'AND', conditions: newConditions })); } else { onUpdate(id, (0, _extends2["default"])({}, group, { conditions: newConditions })); } // 条件删光了,自动删除关系组 if (newConditions.length === 0) { onRemove(id); } }; /** 更新条件 */ var handleConditionUpdate = function handleConditionUpdate(conditionId, condition) { var newConditions = conditions.map(function (c) { if (c.id === conditionId) { return (0, _extends2["default"])({}, c, condition); } return (0, _extends2["default"])({}, c); }); onUpdate(id, (0, _extends2["default"])({}, group, { conditions: newConditions })); }; /** 清除条件 */ var handleConditionClear = function handleConditionClear(conditionId) { var newConditions = conditions.map(function (c) { if (c.id === conditionId) { return { id: c.id }; } return (0, _extends2["default"])({}, c); }); onUpdate(id, (0, _extends2["default"])({}, group, { conditions: newConditions })); }; return /*#__PURE__*/React.createElement("div", { className: "aliretail-logic-condition-group", style: { padding: 20 } }, /*#__PURE__*/React.createElement("div", { className: (0, _classnames["default"])('aliretail-logic-condition-group-select-logic', conditions.length >= 2 && 'has-border'), style: { width: useCompact ? 40 : 44 } }, /*#__PURE__*/React.createElement(_SelectLogic["default"], { disabled: conditions.length < 2, value: calcType, onChange: handleLogicChange })), /*#__PURE__*/React.createElement("div", { className: "aliretail-logic-condition-group-conditions-container" }, conditions === null || conditions === void 0 ? void 0 : conditions.map(function (condition, index) { if (!condition.id) { // eslint-disable-next-line no-param-reassign condition.id = (0, _utils.uniqueConditionKey)(); } return /*#__PURE__*/React.createElement(_SelectCondition["default"], { useCompact: useCompact, key: condition.id, index: index, last: index === conditions.length - 1, removable: conditionRemovable, condition: condition, onAdd: handleConditionAdd, onRemove: handleConditionRemove, onUpdate: handleConditionUpdate, onClear: handleConditionClear }); }))); }; var _default = LoginConditionGroup; exports["default"] = _default;