-
Notifications
You must be signed in to change notification settings - Fork 358
Expand file tree
/
Copy patheffect.ts
More file actions
250 lines (219 loc) · 6.54 KB
/
effect.ts
File metadata and controls
250 lines (219 loc) · 6.54 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
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
241
242
243
244
245
246
247
248
249
250
import { cloneDeep, isFunction, isNumber } from 'lodash-es';
import { getFullPathLabel, getTreeValue } from './helper';
import type { CascaderContextType, TdCascaderProps, TreeNode, TreeNodeModel, TreeNodeValue } from '../interface';
/**
* 点击item的副作用
* @param propsTrigger
* @param trigger
* @param node
* @param cascaderContext
*/
export function expendClickEffect(
propsTrigger: TdCascaderProps['trigger'],
trigger: TdCascaderProps['trigger'],
node: TreeNode,
cascaderContext: CascaderContextType,
) {
const {
checkStrictly,
multiple,
treeStore,
setVisible,
setValue,
setTreeNodes,
setExpend,
value,
max,
valueType,
isParentFilterable,
} = cascaderContext;
const isDisabled = node.disabled || (multiple && (value as TreeNodeValue[]).length >= max && max !== 0);
if (isDisabled) return;
// 点击展开节点,设置展开状态
if (propsTrigger === trigger && node.children !== null) {
const expanded = node.setExpanded(true);
treeStore.refreshNodes();
treeStore.replaceExpanded(expanded);
const nodes = treeStore.getNodes().filter((node: TreeNode) => node.visible);
setTreeNodes(nodes);
// 多选条件下手动维护expend
if (multiple && !isParentFilterable) {
setExpend(expanded);
}
}
if (!multiple && (node.isLeaf() || checkStrictly) && trigger === 'click') {
if (node.checked) return;
treeStore.resetChecked();
const checked = node.setChecked(true);
const [value] = checked;
// 非受控状态下更新状态
setValue(valueType === 'single' ? value : node.getPath().map((item) => item.value), 'check', node.getModel());
// 当 trigger 为 hover 时 ,点击节点一定是关闭 panel 的操作
if (!checkStrictly || propsTrigger === 'hover') {
setVisible(false, {});
}
}
}
/**
* 多选状态下选中状态数据变化的副作用
* @param node
* @param cascaderContext
* @returns
*/
export function valueChangeEffect(node: TreeNode, cascaderContext: CascaderContextType) {
const { disabled, max, inputVal, multiple, setVisible, setValue, treeNodes, treeStore, valueType } = cascaderContext;
if (!node || disabled || node.disabled) {
return;
}
const checked = node.setChecked(!node.isChecked());
if (isNumber(max) && max < 0) {
console.warn('TDesign Warn:', 'max should > 0');
}
if (checked.length > max && isNumber(max) && max > 0) {
return;
}
if (checked.length === 0) {
const expanded = treeStore.getExpanded();
setTimeout(() => {
treeStore.replaceExpanded(expanded);
treeStore.refreshNodes();
}, 0);
}
if (!multiple) {
setVisible(false, {});
}
const isSelectAll = treeNodes.every((item) => checked.indexOf(item.value) > -1);
if (inputVal && isSelectAll) {
setVisible(false, {});
}
// 处理不同数据类型
const resValue =
valueType === 'single'
? checked
: checked.map((val) =>
treeStore
.getNode(val)
.getPath()
.map((item) => item.value),
);
setValue(resValue, node.checked ? 'uncheck' : 'check', node.getModel());
}
/**
* closeIcon点击副作用
* @param cascaderContext
*/
export function closeIconClickEffect(cascaderContext: CascaderContextType) {
const { setVisible, multiple, setExpend, setValue, treeStore } = cascaderContext;
const expanded = treeStore.getExpanded();
setTimeout(() => {
treeStore.replaceExpanded(expanded);
treeStore.refreshNodes();
}, 0);
setVisible(false, {});
// 手动设置的展开需要去除
if (multiple) {
setExpend([]);
}
setValue(multiple ? [] : '', 'clear');
}
/**
* tag 关闭按钮点击副作用
* @param cascaderContext
*/
export function handleRemoveTagEffect(
cascaderContext: CascaderContextType,
index: number,
onRemove: TdCascaderProps['onRemove'],
) {
const { disabled, setValue, value, valueType, treeStore } = cascaderContext;
if (disabled) return;
const newValue = cloneDeep(value) as [];
const res = newValue.splice(index, 1);
const node = treeStore.getNodes(res[0])[0];
const checked = node.setChecked(!node.isChecked());
if (valueType === 'single') {
setValue(newValue, 'uncheck', node.getModel());
} else {
// 处理不同数据类型
const resValue = checked.map((val) =>
treeStore
.getNode(val)
.getPath()
.map((item) => item.value),
);
setValue(resValue, 'uncheck', node.getModel());
}
if (isFunction(onRemove)) {
onRemove({ value: checked, node: node as any });
}
}
/**
* input和treeStore变化的副作用
* @param inputVal
* @param treeStore
* @param setTreeNodes
* @returns
*/
export const treeNodesEffect = (
inputVal: CascaderContextType['inputVal'],
treeStore: CascaderContextType['treeStore'],
setTreeNodes: CascaderContextType['setTreeNodes'],
filter: CascaderContextType['filter'],
isParentFilterable: boolean,
) => {
if (!treeStore) return;
let nodes = [];
if (inputVal) {
const filterMethods = (node: TreeNode) => {
if (!node.isLeaf() && !isParentFilterable) return;
if (isFunction(filter)) {
return filter(`${inputVal}`, node as TreeNodeModel & TreeNode);
}
const fullPathLabel = getFullPathLabel(node, '');
return fullPathLabel.indexOf(`${inputVal}`) > -1;
};
nodes = treeStore.nodes.filter(filterMethods);
} else {
nodes = treeStore.getNodes().filter((node: TreeNode) => node.visible);
}
setTreeNodes(nodes);
};
/**
* 初始化展开阶段与展开状态副作用
* @param treeStore
* @param treeValue
* @param expend
*/
export const treeStoreExpendEffect = (
treeStore: CascaderContextType['treeStore'],
value: CascaderContextType['value'],
expend: TreeNodeValue[],
) => {
const treeValue = getTreeValue(value);
if (!treeStore) return;
// init expanded, 无expend状态时设置
if (Array.isArray(treeValue) && expend.length === 0) {
const expandedMap = new Map();
const [val] = treeValue;
if (val) {
expandedMap.set(val, true);
const node = treeStore.getNode(val);
if (!node) {
treeStore.refreshNodes();
return;
}
node.getParents().forEach((tn: TreeNode) => {
expandedMap.set(tn.value, true);
});
const expandedArr = Array.from(expandedMap.keys());
treeStore.replaceExpanded(expandedArr);
} else {
treeStore.resetExpanded();
}
}
// 本地维护 expend,更加可控,不需要依赖于 tree 的状态
if (treeStore.getExpanded() && expend.length) {
treeStore.replaceExpanded(expend);
}
treeStore.refreshNodes();
};