@wufengteam/wform
Version:
@wufengteam/wform
187 lines • 9.95 kB
JavaScript
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 _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure 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 _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 _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
/* eslint-disable no-console */
import React, { useState, useEffect, useRef } from 'react';
import { Tag } from 'antd-mobile-5';
import { CloseOutline } from 'antd-mobile-icons-5';
import { objSortBy } from '@wufengteam/utils';
import AddDeptModal from './AddDeptModal';
import FieldItemH5 from '../../components/FieldItemH5';
import withTransformProps from '../../utils/withTransformProps';
import { prefix } from '../../utils';
import "./index.css";
var cssPrefix = "".concat(prefix, "-add-department-h5");
var InnerAddDepartmentH5 = function InnerAddDepartmentH5(props) {
var _a;
// console.log('InnerAddDepartmentH5', props);
var value = props.value,
onChange = props.onChange,
_props$selectType = props.selectType,
selectType = _props$selectType === void 0 ? 'single' : _props$selectType,
_props$fieldAttr = props.fieldAttr,
fieldAttr = _props$fieldAttr === void 0 ? [] : _props$fieldAttr,
name = props.name,
disabled = props.disabled,
defaultDepart = props.defaultDepart,
settingDepart = props.settingDepart,
selectRank = props.selectRank,
selectRange = props.selectRange;
var _useState = useState(false),
_useState2 = _slicedToArray(_useState, 2),
visible = _useState2[0],
setVisible = _useState2[1];
var _useState3 = useState(value || []),
_useState4 = _slicedToArray(_useState3, 2),
selectedDepts = _useState4[0],
setSelectedDepts = _useState4[1];
var _ref = ((_a = props === null || props === void 0 ? void 0 : props.getEngineApis) === null || _a === void 0 ? void 0 : _a.call(props)) || {},
_ref$customEngineApi = _ref.customEngineApi,
customEngineApi = _ref$customEngineApi === void 0 ? {} : _ref$customEngineApi;
var enableOrgIds = useRef([]);
useEffect(function () {
setSelectedDepts(Array.isArray(value) ? value : []);
}, [JSON.stringify(value)]);
var onChangeValue = function onChangeValue(list) {
if (onChange) {
var newData = objSortBy(list, 'orgId');
var result = newData.map(function (i) {
return Object.assign(Object.assign({}, i), {
partyId: i === null || i === void 0 ? void 0 : i.orgId,
partyType: 'ORG',
partyName: i === null || i === void 0 ? void 0 : i.orgName
});
});
if (result && Array.isArray(result) && result.length > 0) {
onChange(result);
setSelectedDepts(result);
} else {
onChange(undefined);
setSelectedDepts(undefined);
}
}
};
var onAddDept = function onAddDept() {
if (!disabled && !fieldAttr.includes('read')) {
setVisible(true);
}
};
useEffect(function () {
var _a, _b;
if (selectRange === 'all') {
if ((defaultDepart === null || defaultDepart === void 0 ? void 0 : defaultDepart.containCurrentUserOrg) === 'T') {
// 默认值为当前用户所在部门
(_a = customEngineApi === null || customEngineApi === void 0 ? void 0 : customEngineApi.getCurrentOrg) === null || _a === void 0 ? void 0 : _a.call(customEngineApi).then(function (res) {
if (res && JSON.stringify(res) !== '{}') {
onChangeValue([res]);
}
});
} else {
onChangeValue(((_b = defaultDepart === null || defaultDepart === void 0 ? void 0 : defaultDepart.checkValues) === null || _b === void 0 ? void 0 : _b.cfgObjs) || []);
}
}
}, [defaultDepart]);
// 获取所有的orgId
function getAllOrgIds(list) {
var orgIds = [];
// 递归函数,用于遍历数组和子数组
function traverse(items) {
items.forEach(function (item) {
// 收集当前项的orgId
if (item.orgId) {
orgIds.push(item === null || item === void 0 ? void 0 : item.orgId);
}
// 如果当前项有children属性且是一个数组,则递归遍历
if (Array.isArray(item.children)) {
traverse(item.children);
}
});
}
// 调用递归函数开始遍历
traverse(list);
// 返回所有收集到的orgId
return orgIds;
}
useEffect(function () {
var _a, _b, _c, _d, _e;
if (selectRange === 'custom') {
if ((settingDepart === null || settingDepart === void 0 ? void 0 : settingDepart.containCurrentUserOrg) === 'T') {
// 默认值为当前用户所在部门
(_a = customEngineApi === null || customEngineApi === void 0 ? void 0 : customEngineApi.getCurrentOrg) === null || _a === void 0 ? void 0 : _a.call(customEngineApi).then(function (res) {
if (res && JSON.stringify(res) !== '{}') {
enableOrgIds.current = [].concat(_toConsumableArray(enableOrgIds.current), [res === null || res === void 0 ? void 0 : res.orgId]);
}
});
}
if (Array.isArray((_b = settingDepart === null || settingDepart === void 0 ? void 0 : settingDepart.checkValues) === null || _b === void 0 ? void 0 : _b.cfgObjs) && ((_d = (_c = settingDepart === null || settingDepart === void 0 ? void 0 : settingDepart.checkValues) === null || _c === void 0 ? void 0 : _c.cfgObjs) === null || _d === void 0 ? void 0 : _d.length) > 0) {
var allOrgIds = getAllOrgIds((_e = settingDepart === null || settingDepart === void 0 ? void 0 : settingDepart.checkValues) === null || _e === void 0 ? void 0 : _e.cfgObjs);
enableOrgIds.current = [].concat(_toConsumableArray(enableOrgIds.current), _toConsumableArray(allOrgIds)) || [];
}
}
}, [settingDepart]);
return /*#__PURE__*/React.createElement("div", {
style: {
width: '100%'
}
}, /*#__PURE__*/React.createElement("div", {
className: "".concat(cssPrefix, "-btn")
}, /*#__PURE__*/React.createElement("span", {
onClick: onAddDept
}, (selectedDepts === null || selectedDepts === void 0 ? void 0 : selectedDepts.length) !== 0 ? '继续添加' : '前往添加'), /*#__PURE__*/React.createElement("div", {
className: "lingxiteam-dform-arrow-horizontal"
})), /*#__PURE__*/React.createElement("div", {
className: "".concat(cssPrefix, "-tag-wrap")
}, Array.isArray(selectedDepts) && selectedDepts.map(function (item) {
var staffName = item.staffName,
orgId = item.orgId,
orgName = item.orgName,
isSonOrg = item.isSonOrg;
var title = staffName || orgName;
return /*#__PURE__*/React.createElement(Tag, {
className: "".concat(cssPrefix, "-tag"),
key: orgId,
color: "primary",
fill: "outline"
}, /*#__PURE__*/React.createElement("div", {
className: "".concat(cssPrefix, "-tag-title")
}, "".concat(isSonOrg) === '1' ? "".concat(title, "(\u542B\u5B50\u90E8\u95E8)") : title), /*#__PURE__*/React.createElement(CloseOutline, {
fontSize: 12,
onClick: function onClick() {
if (!disabled && !fieldAttr.includes('read')) {
onChangeValue(selectedDepts.filter(function (dItem) {
return dItem.orgId !== orgId;
}));
}
}
}));
})), visible && ( /*#__PURE__*/React.createElement(AddDeptModal, {
initialValue: value || [],
visible: visible,
onClose: function onClose() {
return setVisible(false);
},
name: name,
onOk: function onOk(data) {
onChangeValue(data);
setVisible(false);
},
defaultDepart: defaultDepart,
selectRank: selectRank,
settingDepart: settingDepart,
selectType: selectType,
getEngineApis: props === null || props === void 0 ? void 0 : props.getEngineApis,
enableOrgIds: enableOrgIds.current
})));
};
var AddDepartmentH5 = function AddDepartmentH5(props) {
// console.log('AddDepartmentH5', props);
return /*#__PURE__*/React.createElement(FieldItemH5, Object.assign({}, props), /*#__PURE__*/React.createElement(InnerAddDepartmentH5, Object.assign({}, props)));
};
export default withTransformProps(AddDepartmentH5);