Skip to content

Commit c249a2a

Browse files
committed
fix(TreeNode): update expand logic to handle child nodes
1 parent 1c0b0b7 commit c249a2a

File tree

3 files changed

+26
-4
lines changed

3 files changed

+26
-4
lines changed

js/tree-v1/tree-node.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ export class TreeNode {
237237
if (this.isLeaf()) {
238238
// initExpanded 时,子节点没有完全加载,无法依赖 isLeaf 状态判断
239239
this.expanded = false;
240-
tree.expandedMap.delete(this.value);
240+
this.tree.expandedMap.delete(this.value);
241241
}
242242

243243
// checked 状态依赖于子节点状态
@@ -323,6 +323,9 @@ export class TreeNode {
323323
if (list.length <= 0) {
324324
return;
325325
}
326+
327+
const wasLeaf = this.isLeaf();
328+
326329
if (!Array.isArray(this.children)) {
327330
this.children = [];
328331
}
@@ -337,6 +340,13 @@ export class TreeNode {
337340
children.push(node);
338341
}
339342
});
343+
344+
// 如果之前是叶子节点,现在有了子节点,且 expandAll 为 true,则展开
345+
if (wasLeaf && tree.config.expandAll && !this.isLeaf()) {
346+
tree.expandedMap.set(this.value, true);
347+
this.expanded = true;
348+
}
349+
340350
tree.reflow(this);
341351
this.updateRelated();
342352
}
@@ -867,8 +877,7 @@ export class TreeNode {
867877
// 如果 valueMode 为 all、parentFirst,则视为选中
868878
valueMode !== 'onlyLeaf'
869879
// 如果 valueMode 为 onlyLeaf 并且当前节点是叶子节点,则视为选中
870-
|| this.isLeaf()
871-
)
880+
|| this.isLeaf())
872881
) {
873882
return true;
874883
}

js/tree/tree-node.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ export class TreeNode {
234234
if (this.isLeaf()) {
235235
// initExpanded 时,子节点没有完全加载,无法依赖 isLeaf 状态判断
236236
this.expanded = false;
237-
tree.expandedMap.delete(this.value);
237+
this.tree.expandedMap.delete(this.value);
238238
}
239239

240240
// 节点的选中状态同时依赖于子节点状态与父节点状态
@@ -320,6 +320,9 @@ export class TreeNode {
320320
if (list.length <= 0) {
321321
return;
322322
}
323+
324+
const wasLeaf = this.isLeaf();
325+
323326
if (!Array.isArray(this.children)) {
324327
this.children = [];
325328
}
@@ -334,6 +337,13 @@ export class TreeNode {
334337
children.push(node);
335338
}
336339
});
340+
341+
// 如果之前是叶子节点,现在有了子节点,且 expandAll 为 true,则展开
342+
if (wasLeaf && tree.config.expandAll && !this.isLeaf()) {
343+
tree.expandedMap.set(this.value, true);
344+
this.expanded = true;
345+
}
346+
337347
tree.reflow(this);
338348
this.updateRelated();
339349
}

test/unit/tree/append.test.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -631,6 +631,9 @@ describe('tree:append', () => {
631631
}]
632632
}, {
633633
value: 't2',
634+
children: [{
635+
value: 't2.1',
636+
}]
634637
}]);
635638
await delay(0);
636639
tree.setExpanded(['t1', 't2']);

0 commit comments

Comments
 (0)