diff --git a/packages/amis-ui/src/components/Tree.tsx b/packages/amis-ui/src/components/Tree.tsx index 6a12550e4..d4546a799 100644 --- a/packages/amis-ui/src/components/Tree.tsx +++ b/packages/amis-ui/src/components/Tree.tsx @@ -210,6 +210,7 @@ export class TreeSelector extends React.Component< unfolded: WeakMap = new WeakMap(); // key: child option, value: parent option; relations: WeakMap = new WeakMap(); + levels: WeakMap = new WeakMap(); dragNode: Option | null; dropInfo: IDropInfo | null; @@ -287,6 +288,11 @@ export class TreeSelector extends React.Component< } } + componentWillUnmount(): void { + // clear data + this.relations = this.unfolded = this.levels = new WeakMap() as any; + } + /** * 展开懒加载节点的父节点 */ @@ -877,11 +883,9 @@ export class TreeSelector extends React.Component< flattenedOptions.push(item); } else if (this.isUnfolded(parent)) { this.relations.set(item, parent); + this.levels.set(item, level); // 父节点是展开的状态 - flattenedOptions.push({ - ...item, - level - }); + flattenedOptions.push(item); } } ); @@ -1102,7 +1106,7 @@ export class TreeSelector extends React.Component< ? 'folder' : 'file' : false); - const level = item.level ? item.level - 1 : 0; + const level = this.levels.has(item) ? this.levels.get(item)! - 1 : 0; let body = null;