zp-bee
Version:
zp-bee,是一款基于 Dumi,由 React + TypeScript 开发的组件库 🎉。
117 lines (99 loc) • 3.18 kB
JavaScript
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;
};
import React, { useEffect, useMemo } from 'react';
import classnames from 'classnames';
import TreeNode from './TreeNode';
import { useForceUpdate } from '../_hooks'; //组件
var defaultNode = {
id: 'id',
label: 'label',
expand: 'expand',
children: 'children'
};
var OrgTree = function OrgTree(props) {
var forceUpdate = useForceUpdate();
var _a = useMemo(function () {
return props;
}, [props]),
horizontal = _a.horizontal,
expandAll = _a.expandAll,
activeId = _a.activeId,
node = _a.node,
data = _a.data,
_onClick = _a.onClick,
_onConditionClick = _a.onConditionClick,
_a$renderContent = _a.renderContent,
renderContent = _a$renderContent === void 0 ? function (data) {
return data.label;
} : _a$renderContent,
restProps = __rest(_a, ["horizontal", "expandAll", "activeId", "node", "data", "onClick", "onConditionClick", "renderContent"]);
useEffect(function () {
if (expandAll) toggleExpand(data, true);
}, []);
var handleExpand = function handleExpand(_e, nodeData) {
if ('expand' in nodeData) {
nodeData.expand = !nodeData.expand;
if (!nodeData.expand && nodeData.children) {
collapse(nodeData.children);
}
forceUpdate();
} else {
nodeData.expand = true;
forceUpdate();
}
};
var collapse = function collapse(list) {
list.forEach(function (child) {
if (child.expand) {
child.expand = false;
}
child.children && collapse(child.children);
});
};
var toggleExpand = function toggleExpand(data, val) {
if (Array.isArray(data)) {
data.forEach(function (item) {
item.expand = val;
if (item.children) {
toggleExpand(item.children, val);
}
});
} else {
data.expand = val;
if (data.children) {
toggleExpand(data.children, val);
}
}
forceUpdate();
};
return /*#__PURE__*/React.createElement("div", {
className: "org-tree-container"
}, /*#__PURE__*/React.createElement("div", {
className: classnames('org-tree', {
horizontal: horizontal
})
}, /*#__PURE__*/React.createElement(TreeNode, Object.assign({
data: data,
activeId: activeId,
node: Object.assign(Object.assign({}, defaultNode), node),
onExpand: function onExpand(e, nodeData) {
return handleExpand(e, nodeData);
},
onClick: function onClick(e, nodeData) {
return _onClick && _onClick(e, nodeData);
},
onConditionClick: function onConditionClick(e, nodeData) {
return _onConditionClick && _onConditionClick(e, nodeData);
},
renderContent: renderContent
}, restProps))));
};
export default OrgTree;