UNPKG

@bos-alpha/data

Version:

数据管理

225 lines (224 loc) 10.3 kB
var __makeTemplateObject = (this && this.__makeTemplateObject) || function (cooked, raw) { if (Object.defineProperty) { Object.defineProperty(cooked, "raw", { value: raw }); } else { cooked.raw = raw; } return cooked; }; var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; var __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) { if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) { if (ar || !(i in from)) { if (!ar) ar = Array.prototype.slice.call(from, 0, i); ar[i] = from[i]; } } return to.concat(ar || Array.prototype.slice.call(from)); }; import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime"; import { useEffect, useState } from 'react'; import { Input, Button, Select, Space as AntSpace, Radio, message, Cascader } from 'antd'; import styled from '@emotion/styled'; import { uuid, prefixCls, Icon } from '@bos-alpha/common'; var RadioGroup = Radio.Group; // 逻辑关系 export var radioOptions = [ { value: 'and', label: 'and' }, { value: 'or', label: 'or' } ]; // 判断条件 var locationsOptions = [ { value: '==', label: '=' }, { value: '!=', label: '≠' }, { value: '<', label: '<' }, { value: '<=', label: '≤' }, { value: '>', label: '>' }, { value: '>=', label: '≥' }, { value: 'like', label: '包含' } ]; var defaultValues = { uuid: uuid(), field: '', logic: 'and', operator: '==', value: '' }; /** * * @param {fieldOptions} 键值下拉选项{ label, value }[] * @param {onQuery} 提交搜索 { field, logic, operator, value }[] * @returns */ export var ConditionCreator = function (_a) { var isReset = _a.isReset, _b = _a.type, type = _b === void 0 ? 'doc' : _b, classDefined = _a.classDefined, fieldOptions = _a.fieldOptions, onQuery = _a.onQuery; var _c = useState([]), formList = _c[0], setFormList = _c[1]; // 添加 function add() { setFormList(__spreadArray(__spreadArray([], formList, true), [ __assign(__assign({}, defaultValues), { field: undefined, uuid: uuid() }) ], false)); } // 删除 function remove(index) { setFormList(formList.filter(function (_, eq) { return eq !== index; })); } // 查询 function handleQuery() { var hasEmpty = formList.filter(function (item) { return item.value === '' || !item.field; }); if (hasEmpty.length > 0) { return message.error('请输入属性值'); } var conditionRows = []; formList.forEach(function (_a) { var uuid = _a.uuid, rest = __rest(_a, ["uuid"]); conditionRows.push(__assign({}, rest)); // 值为数字 且 不是like 增加一个or条件 if (!isNaN(rest.value) && rest.operator !== 'like') { conditionRows.push(__assign(__assign({}, rest), { logic: 'or', number: true })); } }); // 普通对象类高级查询 if (type === 'doc') { onQuery(conditionRows); return; } // 关系类条件 var relationCondition = conditionRows .filter(function (item) { return item.field.length === 1; }) .map(function (item) { return (__assign(__assign({}, item), { field: item.field[0] })); }); // 左对象条件 var leftCondition = conditionRows .filter(function (item) { return item.field.length === 2 && item.field[0] === 'left'; }) .map(function (item) { return (__assign(__assign({}, item), { field: item.field[1] })); }); // 右对象条件 var rightCondition = conditionRows .filter(function (item) { return item.field.length === 2 && item.field[0] === 'right'; }) .map(function (item) { return (__assign(__assign({}, item), { field: item.field[1] })); }); // 关系类高级查询 var conditions = [ __assign({ bosclass: classDefined.leftObjectClass, alias: 'e1' }, (leftCondition.length > 0 ? { subCondition: leftCondition } : {})), __assign({ bosclass: classDefined.rightObjectClass, alias: 'e2' }, (rightCondition.length > 0 ? { subCondition: rightCondition } : {})), __assign({ bosclass: classDefined.className, type: 'relationship', alias: 'e3', from: 'e1', to: 'e2' }, (relationCondition.length > 0 ? { subCondition: relationCondition } : {})) ]; onQuery(conditions); } // 改变 function handleChange(index, key, value) { setFormList(formList.map(function (item, eq) { var _a; if (eq === index) { return __assign(__assign({}, item), (_a = { value: '' }, _a[key] = value, _a)); } return item; })); } /** * 重置 * */ useEffect(function () { setFormList([ __assign(__assign({}, defaultValues), { uuid: uuid() }) ]); if (!isReset) return; if (type === 'doc') { onQuery([]); } else { onQuery([ { bosclass: classDefined.leftObjectClass, alias: 'e1' }, { bosclass: classDefined.rightObjectClass, alias: 'e2' }, { bosclass: classDefined.className, type: 'relationship', alias: 'e3', from: 'e1', to: 'e2' } ]); } }, [isReset]); // eslint-disable-line react-hooks/exhaustive-deps return (_jsxs(Overlay, { children: [formList.map(function (item, index) { return (_jsxs(Space, __assign({ align: "baseline" }, { children: [_jsx(RadioGroup, __assign({ value: item.logic, onChange: function (e) { handleChange(index, 'logic', e.target.value); }, style: { visibility: "".concat(index === 0 ? 'hidden' : 'visible') } }, { children: radioOptions.map(function (item) { return (_jsx(Radio, __assign({ value: item.value }, { children: item.label }), item.value)); }) }), void 0), type === 'edge' ? (_jsx(Cascader, { placeholder: "\u8BF7\u9009\u62E9", style: { width: 160 }, options: fieldOptions, onChange: function (value) { handleChange(index, 'field', value); } }, void 0)) : (_jsx(Select, { placeholder: "\u8BF7\u9009\u62E9", style: { width: 140 }, options: fieldOptions, onChange: function (value) { handleChange(index, 'field', value); } }, void 0)), _jsx(Select, { defaultValue: "==", style: { width: 72 }, options: locationsOptions, onChange: function (value) { handleChange(index, 'operator', value); } }, void 0), _jsx(Input, { value: item.value, placeholder: "\u8BF7\u8F93\u5165\u503C", style: { width: 100 }, onChange: function (e) { handleChange(index, 'value', e.target.value); } }, void 0), _jsxs(Buttons, { children: [_jsx(Icon, { type: "iconicon_facet_add", onClick: function () { return add(); }, className: "add" }, void 0), index !== 0 && (_jsx(Icon, { type: "iconicon_facet_disassociate", onClick: function () { return remove(index); }, className: "remove" }, void 0))] }, void 0)] }), item.uuid)); }), _jsx(StartQueryButton, __assign({ type: "primary", shape: "round", onClick: handleQuery }, { children: "\u5F00\u59CB\u67E5\u8BE2" }), void 0)] }, void 0)); }; var Overlay = styled.div(templateObject_1 || (templateObject_1 = __makeTemplateObject(["\n span.", "-radio + * {\n padding-right: 0px;\n padding-left: 5px;\n }\n"], ["\n span.", "-radio + * {\n padding-right: 0px;\n padding-left: 5px;\n }\n"])), prefixCls); var Buttons = styled.div(templateObject_2 || (templateObject_2 = __makeTemplateObject(["\n padding-left: 2px;\n .add,\n .remove {\n vertical-align: -4px;\n font-size: 18px;\n color: #1890ff;\n }\n .remove {\n margin-left: 10px;\n }\n"], ["\n padding-left: 2px;\n .add,\n .remove {\n vertical-align: -4px;\n font-size: 18px;\n color: #1890ff;\n }\n .remove {\n margin-left: 10px;\n }\n"]))); var Space = styled(AntSpace)(templateObject_3 || (templateObject_3 = __makeTemplateObject(["\n display: flex;\n margin-bottom: 8px;\n"], ["\n display: flex;\n margin-bottom: 8px;\n"]))); var StartQueryButton = styled(Button)(templateObject_4 || (templateObject_4 = __makeTemplateObject(["\n margin: 24px auto 0;\n display: block;\n"], ["\n margin: 24px auto 0;\n display: block;\n"]))); var templateObject_1, templateObject_2, templateObject_3, templateObject_4;