UNPKG

ze-react-component-library

Version:
208 lines (179 loc) 7.04 kB
import "antd/es/button/style"; import _Button from "antd/es/button"; import "antd/es/select/style"; import _Select from "antd/es/select"; import "antd/es/auto-complete/style"; import _AutoComplete from "antd/es/auto-complete"; 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); } 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 __spreadArray = this && this.__spreadArray || function (to, from) { for (var i = 0, il = from.length, j = to.length; i < il; i++, j++) { to[j] = from[i]; } return to; }; import React, { useState, memo } from "react"; import { getNameProperty } from "zeroetp-api-sdk"; import { useRequest } from "@umijs/hooks"; import { requestLogicform } from "../../request"; import "./index.less"; // 不带Hierarchy的,渲染object类型的 export var ObjectFormRenderItem = /*#__PURE__*/memo(function (_a) { var _b; var schema = _a.schema, props = _a.props, queryProperty = _a.queryProperty, groupby = _a.groupby, _c = _a.type, type = _c === void 0 ? "select" : _c, hideIdInLabel = _a.hideIdInLabel, showTotal = _a.showTotal; var _d = useState(), search = _d[0], setSearch = _d[1]; var data = useRequest(function () { var _a; // console.log(props); var limit = 20; var query = __assign({}, (_a = props === null || props === void 0 ? void 0 : props.fieldProps) === null || _a === void 0 ? void 0 : _a.query); if (search) { query[queryProperty.name] = { $contains: search }; limit = 100; } return requestLogicform({ schema: schema._id, groupby: groupby, query: query, limit: limit }); }, { formatResult: function formatResult(res) { return res ? res.result : []; }, initialData: [], refreshDeps: [search] }).data; var optionsData = data.map(function (i) { return __assign(__assign({}, i), { value: i._id, label: /*#__PURE__*/React.createElement(React.Fragment, null, i[queryProperty.name], !hideIdInLabel && "\uFF08" + i._id + "\uFF09") }); }); if (showTotal) { optionsData = __spreadArray([{ value: "全部", label: "全部" }], optionsData); } if (type === "autocomplete") { return /*#__PURE__*/React.createElement(_AutoComplete, __assign({ showSearch: true, defaultActiveFirstOption: false, showArrow: false, filterOption: false, onSearch: setSearch, placeholder: "\u8BF7\u8F93\u5165", allowClear: true, dropdownMatchSelectWidth: false }, props === null || props === void 0 ? void 0 : props.fieldProps, { options: optionsData })); } return /*#__PURE__*/React.createElement(_Select, __assign({ showSearch: true, defaultActiveFirstOption: false, showArrow: false, filterOption: false, onSearch: setSearch, placeholder: "\u8BF7\u9009\u62E9", allowClear: true, dropdownMatchSelectWidth: false }, props === null || props === void 0 ? void 0 : props.fieldProps, { labelInValue: true, options: optionsData, value: ((_b = props === null || props === void 0 ? void 0 : props.fieldProps) === null || _b === void 0 ? void 0 : _b.value) ? optionsData.find(function (d) { var _a, _b, _c; return _typeof((_a = props === null || props === void 0 ? void 0 : props.fieldProps) === null || _a === void 0 ? void 0 : _a.value) === "object" ? ((_b = props === null || props === void 0 ? void 0 : props.fieldProps) === null || _b === void 0 ? void 0 : _b.value._id) === d.value : ((_c = props === null || props === void 0 ? void 0 : props.fieldProps) === null || _c === void 0 ? void 0 : _c.value) === d.value; }) : undefined, onChange: function onChange(v) { var _a, _b; var value = v === null || v === void 0 ? void 0 : v.value; var isTotal = (v === null || v === void 0 ? void 0 : v.value) === "全部"; // 为了便于前端显示,这里需要返回整个object。 var entity = data.find(function (i) { return i._id === value; }); (_b = (_a = props === null || props === void 0 ? void 0 : props.fieldProps) === null || _a === void 0 ? void 0 : _a.onChange) === null || _b === void 0 ? void 0 : _b.call(_a, isTotal ? v : entity); } })); }); export var ObjectArrayFormRenderItem = /*#__PURE__*/memo(function (_a) { var schema = _a.schema, props = _a.props; var _b = (props === null || props === void 0 ? void 0 : props.fieldProps) || {}, _c = _b.value, value = _c === void 0 ? [] : _c, onChange = _b.onChange; var onItemChange = function onItemChange(v, index) { var newValue = __spreadArray([], value); newValue[index] = v; onChange === null || onChange === void 0 ? void 0 : onChange(newValue); }; var onItemDelete = function onItemDelete(index) { var newValue = value === null || value === void 0 ? void 0 : value.filter(function (d, i) { return index !== i; }); onChange === null || onChange === void 0 ? void 0 : onChange(newValue); }; var onItemCreate = function onItemCreate() { onChange === null || onChange === void 0 ? void 0 : onChange(__spreadArray(__spreadArray([], value), [undefined])); }; return /*#__PURE__*/React.createElement("div", { className: "ze-object-field-item" }, value === null || value === void 0 ? void 0 : value.map(function (d, i) { return /*#__PURE__*/React.createElement("div", { key: i, style: { display: "flex", alignItems: "center", marginBottom: 12 } }, /*#__PURE__*/React.createElement(ObjectFormRenderItem, { schema: schema, queryProperty: getNameProperty(schema), props: __assign(__assign({}, props), { fieldProps: __assign(__assign({}, props === null || props === void 0 ? void 0 : props.fieldProps), { value: d, onChange: function onChange(v) { // console.log(v); onItemChange(v, i); } }) }) }), /*#__PURE__*/React.createElement(_Button, { size: "small", type: "link", danger: true, onClick: function onClick() { onItemDelete(i); } }, "\u5220\u9664")); }), /*#__PURE__*/React.createElement(_Button, { type: "dashed", style: { width: "100%" }, onClick: onItemCreate }, "\u65B0\u589E")); }); export default ObjectFormRenderItem;