|
@@ -0,0 +1,61 @@
|
|
1
|
+import React, { useEffect, useMemo, useRef, useState } from 'react'
|
|
2
|
+import { TreeSelect } from 'antd'
|
|
3
|
+import { connect } from 'dva'
|
|
4
|
+
|
|
5
|
+const getTreeFrom = (arr) => {
|
|
6
|
+ const copy = (arr || []).map((it) => ({ ...it, children: undefined }))
|
|
7
|
+
|
|
8
|
+ const tree = []
|
|
9
|
+ for (let inx in copy) {
|
|
10
|
+ const it = copy[inx]
|
|
11
|
+ const key = it.institutionCode
|
|
12
|
+ const node = {
|
|
13
|
+ key,
|
|
14
|
+ value: key,
|
|
15
|
+ title: it.institutionName,
|
|
16
|
+ children: it.children,
|
|
17
|
+ parent: it.parent,
|
|
18
|
+ }
|
|
19
|
+ const parent = key.length === 2 ? undefined : key.substring(0, key.length - 2)
|
|
20
|
+
|
|
21
|
+ if (parent) {
|
|
22
|
+ for (let cur of copy) {
|
|
23
|
+ if ((cur.institutionCode || cur.key) === parent) {
|
|
24
|
+ node.parent = cur
|
|
25
|
+ cur.children = [
|
|
26
|
+ ...(cur.children || []),
|
|
27
|
+ node,
|
|
28
|
+ ]
|
|
29
|
+ break
|
|
30
|
+ }
|
|
31
|
+ }
|
|
32
|
+ }
|
|
33
|
+
|
|
34
|
+ copy[inx] = node
|
|
35
|
+ tree.push(node)
|
|
36
|
+ }
|
|
37
|
+ return tree.filter(x => !x.parent)
|
|
38
|
+}
|
|
39
|
+
|
|
40
|
+const InstitutionSelect = React.forwardRef((props, ref) => {
|
|
41
|
+
|
|
42
|
+ const { user, ...leftProps } = props
|
|
43
|
+
|
|
44
|
+ const [expandedKeys, setExpandedKeys] = useState([])
|
|
45
|
+
|
|
46
|
+ const treeData = useMemo(() => {
|
|
47
|
+ return getTreeFrom(user.institutionList) || []
|
|
48
|
+ }, [user.institutionList])
|
|
49
|
+
|
|
50
|
+ useEffect(() => {
|
|
51
|
+ setExpandedKeys((user.institutionList || []).map((x) => x.institutionCode))
|
|
52
|
+ }, [user.institutionList])
|
|
53
|
+
|
|
54
|
+ return (
|
|
55
|
+ <TreeSelect allowClear treeExpandedKeys={expandedKeys} onTreeExpand={setExpandedKeys} {...leftProps} treeData={treeData} />
|
|
56
|
+ )
|
|
57
|
+})
|
|
58
|
+
|
|
59
|
+export default connect(({ user }) => ({
|
|
60
|
+ user: user.currentUser,
|
|
61
|
+}))(InstitutionSelect)
|