|
@@ -1,7 +1,7 @@
|
1
|
1
|
import React from 'react';
|
2
|
|
-import { Button, Card, Tree, Modal } from 'antd';
|
|
2
|
+import { Button, Card, Tree, message } from 'antd';
|
3
|
3
|
import useBool from '@/utils/hooks/useBool';
|
4
|
|
-import arr2Tree from '@/utils/arr2Tree';
|
|
4
|
+import { arr2Tree, uniq } from '@/utils/array';
|
5
|
5
|
import { getResourceList } from '@/services/role';
|
6
|
6
|
|
7
|
7
|
export default (props) => {
|
|
@@ -11,16 +11,33 @@ export default (props) => {
|
11
|
11
|
const [list, setList] = React.useState([]);
|
12
|
12
|
const [treeData, setTreeData] = React.useState([]);
|
13
|
13
|
const treeDictRef = React.useRef();
|
|
14
|
+ const [checkedKeys, setCheckedKeys] = React.useState([]);
|
|
15
|
+ const checkedNodesRef = React.useState([]);
|
14
|
16
|
|
15
|
17
|
const title = role ? `${role.name} - 授权菜单` : '授权菜单';
|
16
|
18
|
|
17
|
|
- const onSelect = (selectedKeys, info) => {
|
18
|
|
- console.log('selected', selectedKeys, info);
|
19
|
|
- };
|
20
|
|
- const onCheck = (checkedKeys, info) => {
|
21
|
|
- console.log('onCheck', checkedKeys, info);
|
|
19
|
+ const onCheck = (keys, info) => {
|
|
20
|
+ const { checkedNodes } = info;
|
|
21
|
+ checkedNodesRef.current = checkedNodes;
|
|
22
|
+ // const keys = checkedNodes.map(node => getKeysWithParent(node, treeDictRef.current));
|
|
23
|
+ // setCheckedKeys(uniq(keys));
|
|
24
|
+ setCheckedKeys(keys);
|
22
|
25
|
};
|
23
|
26
|
|
|
27
|
+ const onSubmit = () => {
|
|
28
|
+ if (!checkedNodesRef.current) {
|
|
29
|
+ return;
|
|
30
|
+ }
|
|
31
|
+
|
|
32
|
+ if (checkedNodesRef.current.length !== checkedKeys.length) {
|
|
33
|
+ message.warn('未知错误, 请刷新重试');
|
|
34
|
+ return;
|
|
35
|
+ }
|
|
36
|
+
|
|
37
|
+ const keys = checkedNodesRef.current.map(node => getKeysWithParent(node, treeDictRef.current));
|
|
38
|
+
|
|
39
|
+ }
|
|
40
|
+
|
24
|
41
|
React.useEffect(() => {
|
25
|
42
|
startLoading();
|
26
|
43
|
getResourceList({ pageSize: 500, type: 'menu' }).then((res) => {
|
|
@@ -50,14 +67,23 @@ export default (props) => {
|
50
|
67
|
<Card
|
51
|
68
|
loading={loading}
|
52
|
69
|
title={title}
|
53
|
|
- extra={<Button type='primary' ghost>保存</Button>}
|
|
70
|
+ extra={<Button type='primary' ghost onClick={onSubmit}>保存</Button>}
|
54
|
71
|
>
|
55
|
72
|
<Tree
|
56
|
73
|
checkable
|
|
74
|
+ autoExpandParent
|
|
75
|
+ checkStrictly={false}
|
57
|
76
|
selectable={false}
|
|
77
|
+ checkedKeys={checkedKeys}
|
58
|
78
|
onCheck={onCheck}
|
59
|
79
|
treeData={treeData}
|
60
|
80
|
/>
|
61
|
81
|
</Card>
|
62
|
82
|
)
|
63
|
83
|
}
|
|
84
|
+
|
|
85
|
+function getKeysWithParent(node, dict) {
|
|
86
|
+ if (node.parentId === -1) return [node.key];
|
|
87
|
+ const parentKeys = getKeysWithParent(dict[node.parentId], dict);
|
|
88
|
+ return parentKeys ? [node.key].concat(parentKeys) : [node.key];
|
|
89
|
+}
|