-
Notifications
You must be signed in to change notification settings - Fork 202
Expand file tree
/
Copy pathlegacyUtil.tsx
More file actions
157 lines (135 loc) · 3.7 KB
/
Copy pathlegacyUtil.tsx
File metadata and controls
157 lines (135 loc) · 3.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import * as React from 'react';
import { toArray, warning } from '@rc-component/util';
import type {
DataNode,
ChangeEventExtra,
SafeKey,
LegacyCheckedNode,
FieldNames,
} from '../interface';
import TreeNode from '../TreeNode';
export function convertChildrenToData(nodes: React.ReactNode): DataNode[] {
return toArray(nodes)
.map((node: React.ReactElement<any>) => {
if (!React.isValidElement(node) || !node.type) {
return null;
}
const {
key,
props: { children, value, ...restProps },
} = node as React.ReactElement<any>;
const data = {
key,
value,
...restProps,
};
const childData = convertChildrenToData(children);
if (childData.length) {
data.children = childData;
}
return data;
})
.filter(data => data);
}
export function fillLegacyProps(dataNode: DataNode) {
if (!dataNode) {
return dataNode;
}
const cloneNode = { ...dataNode };
if (!('props' in cloneNode)) {
Object.defineProperty(cloneNode, 'props', {
get() {
warning(
false,
'New `rc-tree-select` not support return node instance as argument anymore. Please consider to remove `props` access.',
);
return cloneNode;
},
});
}
return cloneNode;
}
export function fillAdditionalInfo(
extra: ChangeEventExtra,
triggerValue: SafeKey,
checkedValues: SafeKey[],
treeData: DataNode[],
showPosition: boolean,
fieldNames: FieldNames,
) {
let triggerNode: React.ReactNode = null;
let nodeList: LegacyCheckedNode[] = null;
function generateMap() {
function dig(list: DataNode[], level = '0', parentIncluded = false) {
return list
.map((option, index) => {
const pos = `${level}-${index}`;
const value = option[fieldNames.value];
const included = checkedValues.includes(value);
const children = dig(option[fieldNames.children] || [], pos, included);
const node = (
<TreeNode {...(option as Required<DataNode>)}>
{children.map(child => child.node)}
</TreeNode>
);
// Link with trigger node
if (triggerValue === value) {
triggerNode = node;
}
if (included) {
const checkedNode: LegacyCheckedNode = {
pos,
node,
children,
};
if (!parentIncluded) {
nodeList.push(checkedNode);
}
return checkedNode;
}
return null;
})
.filter(node => node);
}
if (!nodeList) {
nodeList = [];
dig(treeData);
// Sort to keep the checked node length
nodeList.sort(
(
{
node: {
props: { value: val1 },
},
}: { node: React.ReactElement<any> },
{
node: {
props: { value: val2 },
},
}: { node: React.ReactElement<any> },
) => {
const index1 = checkedValues.indexOf(val1);
const index2 = checkedValues.indexOf(val2);
return index1 - index2;
},
);
}
}
Object.defineProperty(extra, 'triggerNode', {
get() {
warning(false, '`triggerNode` is deprecated. Please consider decoupling data with node.');
generateMap();
return triggerNode;
},
});
Object.defineProperty(extra, 'allCheckedNodes', {
get() {
warning(false, '`allCheckedNodes` is deprecated. Please consider decoupling data with node.');
generateMap();
if (showPosition) {
return nodeList;
}
return nodeList.map(({ node }) => node);
},
});
}