/
index.tsx
240 lines (225 loc) · 7 KB
/
index.tsx
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
import React, { useEffect, useState } from 'react';
import { IconProps } from '@uiw/react-icon';
import { IProps, HTMLDivProps, noop } from '@uiw/utils';
import TreeNode from './TreeNode';
import './style/index.less';
export type TreeRenderTitleNode = {
selected?: boolean;
noChild?: boolean;
isHalfChecked?: boolean;
openKeys?: TreeProps['openKeys'];
selectedKeys?: TreeProps['selectedKeys'];
disabled?: boolean;
disabledClass?: string;
disabledStyle?: React.CSSProperties;
};
export interface TreeProps extends IProps, Omit<HTMLDivProps, 'onChange'> {
icon?: IconProps['type'];
data?: TreeData[];
openKeys?: TreeData['key'][];
selectedKeys?: TreeData['key'][];
defaultExpandAll?: boolean;
/**
* 是否自动展开父节点
*/
autoExpandParent?: boolean;
/**
* 是否展示连接线
*/
showLine?: boolean;
iconAnimation?: boolean;
isSelected?: boolean;
/**
* 子节点受父节点控制设置 `true`,需要配合 `multiple` 参数使用。
*/
checkStrictly?: boolean;
/**
* 支持点选多个节点
*/
multiple?: boolean;
renderTitle?: (item: TreeData, node: TreeRenderTitleNode) => React.ReactElement;
onExpand?: (key: TreeData['key'], expanded: boolean, item: TreeData, evn: React.MouseEvent<HTMLElement>) => void;
onSelected?: (
keys: TreeData['key'][],
key: TreeData['key'],
selected: boolean,
item: TreeData,
evn: React.MouseEvent<HTMLElement>,
) => void;
onChange?: (key: string | number | undefined, eys: (string | number | undefined)[]) => void;
value?: TreeData['key'][];
}
export interface TreeData {
label?: React.ReactNode;
children?: TreeData[];
hideNode?: boolean;
key?: string | number;
disabled?: boolean;
[keyName: string]: any;
}
/**
* a contains b
* @param {Array} a
* @param {Array} b
*/
const isContained = (a: any[], b: any[]) => {
if (!(a instanceof Array) || !(b instanceof Array)) return false;
if (a.length < b.length) return false;
const aStr = a.toString();
for (let i = 0, len = b.length; i < len; i += 1) {
if (aStr.indexOf(b[i]) === -1) return false;
}
return true;
};
export const getChildKeys = (
childs: TreeData[] = [],
result: TreeData['key'][] = [],
depth?: number,
): TreeData['key'][] => {
childs.forEach((item) => {
if (!item.hideNode) {
result.push(item.key as string | number);
}
if (typeof depth === 'number' && !(depth - 1)) return;
if (item.children && item.children.length > 0) {
result = result.concat(getChildKeys(item.children, undefined, depth ? depth - 1 : undefined));
}
});
return result;
};
const getParentKeys = (childs: TreeData = {}, result: TreeData['key'][] = []) => {
if (childs.key) {
result.push(childs.key);
}
if (childs.parent) {
result = getParentKeys(childs.parent, result);
}
return result;
};
const getParentSelectKeys = (
childs: TreeData = {},
selectedKeys: TreeData['key'][] = [],
result: TreeData['key'][] = [],
) => {
if (childs.key && childs.children && isContained(selectedKeys, getChildKeys(childs.children, undefined, 1))) {
result.push(childs.key);
if (childs.parent && !childs.parent.parent) {
if (isContained(selectedKeys, getChildKeys(childs.children))) {
selectedKeys = selectedKeys.concat(result);
}
if (isContained(selectedKeys, getChildKeys(childs.parent.children))) {
result.push(childs.parent.key);
}
}
}
if (childs.parent) {
result = getParentSelectKeys(childs.parent, selectedKeys, result);
}
return result;
};
export default function Tree(props: TreeProps) {
const {
prefixCls = 'w-tree',
icon = 'caret-right',
data = [],
openKeys = [],
selectedKeys = [],
defaultExpandAll = false,
showLine = false,
iconAnimation = true,
isSelected = true,
checkStrictly = false,
multiple = false,
onExpand = noop,
onSelected = noop,
className,
autoExpandParent = true,
renderTitle,
onChange,
value,
...elementProps
} = props;
const [curOpenKeys, setCurOpenKeys] = useState(openKeys);
const [curSelectedKeys, setCurSelectedKeys] = useState(value || selectedKeys);
useEffect(() => {
setCurSelectedKeys(props.value || props.selectedKeys || []);
}, [JSON.stringify(props.selectedKeys), JSON.stringify(props.value)]);
useEffect(() => {
let arrOpenKeys: TreeData['key'][] = [...curOpenKeys];
if (defaultExpandAll) {
arrOpenKeys = getChildKeys(data);
} else if (autoExpandParent) {
arrOpenKeys.push(...getChildKeys(data || [], undefined, 1));
}
setCurOpenKeys(arrOpenKeys);
}, []);
const cls = [className, prefixCls, showLine ? `${prefixCls}-line` : null].filter(Boolean).join(' ').trim();
function onItemClick(item: TreeData, evn: React.MouseEvent<HTMLElement>) {
if (!item.children) {
return;
}
// const { onExpand } = this.props;
// const { openKeys } = this.state;
let currentKeys = [...(curOpenKeys as TreeData['key'][])];
const key = currentKeys.find((v) => v === item.key);
const cls = evn.currentTarget.className.replace(/(\s)open/g, '');
let expanded = false;
if (!key && item.key) {
currentKeys.push(item.key);
evn.currentTarget.className = [cls, 'open'].filter(Boolean).join(' ').trim();
expanded = true;
} else {
currentKeys = currentKeys.filter((v) => v !== item.key);
evn.currentTarget.className = cls;
}
setCurOpenKeys(currentKeys);
onExpand && onExpand(item.key, expanded, item, evn);
}
function onItemSelected(item: TreeData, evn: React.MouseEvent<HTMLElement>) {
// const { onSelected, multiple, checkStrictly } = this.props;
let selKeys = [...(curSelectedKeys as TreeData['key'][])];
const findKey = selKeys.find((v) => v === item.key);
let selected = false;
if (!findKey && findKey !== 0) {
selected = true;
selKeys.push(item.key);
} else {
selKeys = selKeys.filter((v) => v !== item.key);
}
if (checkStrictly) {
if (!findKey) {
selKeys = selKeys.concat(getChildKeys(item.children).filter((val) => selKeys.indexOf(val) === -1));
selKeys = selKeys.concat(getParentSelectKeys(item, selKeys));
selKeys = Array.from(new Set(selKeys)); // Remove duplicates.
} else {
selKeys = selKeys.filter((val) => getChildKeys(item.children).indexOf(val) === -1);
selKeys = selKeys.filter((val) => getParentKeys(item.parent).indexOf(val) === -1);
}
}
if (!multiple) {
selKeys = !findKey ? [item.key] : [];
}
setCurSelectedKeys(selKeys);
onSelected && onSelected(selKeys, item.key, selected, item, evn);
onChange?.(item.key, selKeys);
}
return (
<div className={cls} {...elementProps}>
<TreeNode
{...{
icon,
iconAnimation,
isSelected,
openKeys: curOpenKeys,
selectedKeys: curSelectedKeys,
prefixCls,
renderTitle,
}}
onItemClick={onItemClick}
onItemSelected={onItemSelected}
data={data}
level={1}
/>
</div>
);
}