|
@@ -0,0 +1,119 @@
|
|
1
|
+import React, { useEffect, useState } from 'react'
|
|
2
|
+import { Tree, Spin } from 'antd'
|
|
3
|
+import { connect } from 'umi'
|
|
4
|
+
|
|
5
|
+const convert2Tree = (arr) => {
|
|
6
|
+ const rootId = -1
|
|
7
|
+ const tree = []
|
|
8
|
+
|
|
9
|
+ arr.forEach((menu) => {
|
|
10
|
+ const node = {
|
|
11
|
+ title: menu.name,
|
|
12
|
+ key: menu.menuId,
|
|
13
|
+ data: menu,
|
|
14
|
+ }
|
|
15
|
+
|
|
16
|
+ if (menu.children) {
|
|
17
|
+ node.children = menu.children
|
|
18
|
+ } else {
|
|
19
|
+ node.children = []
|
|
20
|
+ // eslint-disable-next-line
|
|
21
|
+ menu.children = node.children
|
|
22
|
+ }
|
|
23
|
+
|
|
24
|
+ // 如果是根节点
|
|
25
|
+ if (menu.menuPId === rootId) {
|
|
26
|
+ tree.push(node)
|
|
27
|
+ } else {
|
|
28
|
+ // 否则查询父节点
|
|
29
|
+ let parent = null
|
|
30
|
+ for(let i = 0; i < arr.length; i += 1) {
|
|
31
|
+ if (arr[i].menuId === menu.menuPId) {
|
|
32
|
+ parent = arr[i]
|
|
33
|
+ break
|
|
34
|
+ }
|
|
35
|
+ }
|
|
36
|
+
|
|
37
|
+ // 找到父节点, 则处理
|
|
38
|
+ // 否则直接丢弃
|
|
39
|
+ if (parent) {
|
|
40
|
+ if (parent.children) {
|
|
41
|
+ parent.children.push(node)
|
|
42
|
+ } else {
|
|
43
|
+ parent.children = [node]
|
|
44
|
+ }
|
|
45
|
+ }
|
|
46
|
+ }
|
|
47
|
+ })
|
|
48
|
+
|
|
49
|
+ return tree
|
|
50
|
+}
|
|
51
|
+
|
|
52
|
+const RoelMenu = (props) => {
|
|
53
|
+ const [treeData, setTreeData] = useState([])
|
|
54
|
+ const [expandedKeys, setExpandedKeys] = useState([])
|
|
55
|
+ const [checkedKeys, setCheckedKeys] = useState({ checked: [], halfChecked: [] })
|
|
56
|
+
|
|
57
|
+ const handleChecked = (keys, e) => {
|
|
58
|
+ setCheckedKeys({ checked: keys, halfChecked: e.halfCheckedKeys })
|
|
59
|
+
|
|
60
|
+ const allChecked = [...keys, ...e.halfCheckedKeys ]
|
|
61
|
+ const allMenus = props.menuList || []
|
|
62
|
+ const checkedMenus = allChecked.map((x) => allMenus.filter((m) => m.menuId === x)[0])
|
|
63
|
+ props.onChange(checkedMenus)
|
|
64
|
+ }
|
|
65
|
+
|
|
66
|
+ useEffect(() => {
|
|
67
|
+ // 因为 convert2Tree 方法有对 Object 的写入操作
|
|
68
|
+ // 此方法会触发 treeData 反复生成异常
|
|
69
|
+ // 所以需要进行一次解构操作
|
|
70
|
+ const list = (props.menuList || []).map((x) => ({ ...x, children: undefined }))
|
|
71
|
+ //
|
|
72
|
+ setTreeData(convert2Tree(list))
|
|
73
|
+
|
|
74
|
+ // 默认展开全部
|
|
75
|
+ setExpandedKeys(list.map((x) => x.menuId))
|
|
76
|
+ }, [props.menuList])
|
|
77
|
+
|
|
78
|
+ useEffect(() => {
|
|
79
|
+ const checked = []
|
|
80
|
+ const halfChecked = []
|
|
81
|
+ const menus = props.roleMenu || []
|
|
82
|
+ const allMenus = props.menuList || []
|
|
83
|
+
|
|
84
|
+ menus.forEach((menu) => {
|
|
85
|
+ // 当前选中的子节点
|
|
86
|
+ const checkedChildren = menus.filter((m) => m.menuPId === menu.menuId)
|
|
87
|
+ // 所有的子节点
|
|
88
|
+ const allChildren = allMenus.filter((m) => m.menuPId === menu.menuId)
|
|
89
|
+ // 如果当前选中的子节点全部都在选择列表中
|
|
90
|
+ if (checkedChildren.length === allChildren.length) {
|
|
91
|
+ checked.push(menu.menuId)
|
|
92
|
+ } else {
|
|
93
|
+ halfChecked.push(menu.menuId)
|
|
94
|
+ }
|
|
95
|
+ })
|
|
96
|
+
|
|
97
|
+ setCheckedKeys(checked, halfChecked)
|
|
98
|
+ // setCheckedKeys((props.roleMenu || []).map((x) => x.menuId))
|
|
99
|
+ }, [props.roleMenu, props.menuList])
|
|
100
|
+
|
|
101
|
+ return (
|
|
102
|
+ <div>
|
|
103
|
+ {
|
|
104
|
+ treeData.length && (
|
|
105
|
+ <Tree
|
|
106
|
+ checkable
|
|
107
|
+ checkedKeys={checkedKeys}
|
|
108
|
+ treeData={treeData}
|
|
109
|
+ defaultExpandedKeys={expandedKeys}
|
|
110
|
+ onCheck={handleChecked}
|
|
111
|
+ />
|
|
112
|
+ )
|
|
113
|
+ }
|
|
114
|
+ { !treeData.length && <Spin />}
|
|
115
|
+ </div>
|
|
116
|
+ )
|
|
117
|
+}
|
|
118
|
+
|
|
119
|
+export default connect((s) => ({ menuList: s.user.menus }))(RoelMenu)
|