From 21208f0c990db36864138a228fb5f065c4fcdb92 Mon Sep 17 00:00:00 2001 From: Julien Poyard <10052221+jpoyard@users.noreply.github.com> Date: Sun, 11 Dec 2022 03:36:09 +0100 Subject: [PATCH] fix(module:tree): tree select search slow in virtual mode (#7385) * fix(module:tree): tree select search slow in virtual mode * fix(module:tree): tree select search slow in virtual mode --- components/tree-select/demo/virtual-scroll.ts | 1 + components/tree/tree.component.ts | 5 +- components/tree/tree.spec.ts | 66 +++++++++++++++++++ 3 files changed, 71 insertions(+), 1 deletion(-) diff --git a/components/tree-select/demo/virtual-scroll.ts b/components/tree-select/demo/virtual-scroll.ts index a2c570be4a..219e205fc1 100644 --- a/components/tree-select/demo/virtual-scroll.ts +++ b/components/tree-select/demo/virtual-scroll.ts @@ -11,6 +11,7 @@ import { NzTreeNodeOptions } from 'ng-zorro-antd/tree'; nzShowSearch nzPlaceHolder="Please select" nzVirtualHeight="300px" + nzHideUnMatched="true" > ` }) diff --git a/components/tree/tree.component.ts b/components/tree/tree.component.ts index 4075a418c4..f22f3456ef 100644 --- a/components/tree/tree.component.ts +++ b/components/tree/tree.component.ts @@ -485,7 +485,10 @@ export class NzTreeComponent ngOnInit(): void { this.nzTreeService.flattenNodes$.pipe(takeUntil(this.destroy$)).subscribe(data => { - this.nzFlattenNodes = data; + this.nzFlattenNodes = + !!this.nzVirtualHeight && this.nzHideUnMatched && this.nzSearchValue?.length > 0 + ? data.filter(d => !d.canHide) + : data; this.cdr.markForCheck(); }); diff --git a/components/tree/tree.spec.ts b/components/tree/tree.spec.ts index a9ffb9c0be..c1d934ac7b 100644 --- a/components/tree/tree.spec.ts +++ b/components/tree/tree.spec.ts @@ -144,6 +144,70 @@ describe('tree', () => { expect(component.treeComponent.getMatchedNodeList().length).toEqual(1); })); + [ + { + title: + "should display 7 nodes when hideUnMatched=false and virtualHeight = undefined and nzSearchValue = '0-1'", + when: { hideUnMatched: false, searchValue: '0-1' }, + then: { matchedNodeList: 3, nzFlattenNodes: 7 } + }, + { + title: + "should display 7 nodes when hideUnMatched=false and virtualHeight = '300px' and nzSearchValue = '0-1'", + when: { hideUnMatched: false, virtualHeight: '300px', searchValue: '0-1' }, + then: { matchedNodeList: 3, nzFlattenNodes: 7 } + }, + { + title: + "'should display 7 nodes when hideUnMatched=true and virtualHeight = undefined and nzSearchValue = '0-1'", + when: { hideUnMatched: true, searchValue: '0-1' }, + then: { matchedNodeList: 3, nzFlattenNodes: 7 } + }, + { + title: + "should display 4 matched nodes based on nzSearchValue when hideUnMatched=true and virtualHeight = '300px' and nzSearchValue = undefined", + when: { hideUnMatched: true, virtualHeight: '300px' }, + then: { matchedNodeList: 0, nzFlattenNodes: 3 } + }, + { + title: + "should display 4 matched nodes based on nzSearchValue when hideUnMatched=true and virtualHeight = '300px' and nzSearchValue = ''", + when: { hideUnMatched: true, virtualHeight: '300px', searchValue: '' }, + then: { matchedNodeList: 0, nzFlattenNodes: 3 } + }, + { + title: + "should display 4 matched nodes based on nzSearchValue when hideUnMatched=true and virtualHeight = '300px' and nzSearchValue = '0-1'", + when: { hideUnMatched: true, virtualHeight: '300px', searchValue: '0-1' }, + then: { matchedNodeList: 3, nzFlattenNodes: 4 } + } + ].forEach(({ title, when, then }) => { + it( + title, + fakeAsync(() => { + // Given + const { component, fixture, nativeElement } = testBed; + component.searchValue = when.searchValue; + component.virtualHeight = when.virtualHeight; + component.hideUnMatched = when.hideUnMatched; + // When + fixture.detectChanges(); + tick(300); + fixture.detectChanges(); + // Then + expect(component.treeComponent.getMatchedNodeList().length) + .withContext('treeComponent.getMatchedNodeList().length') + .toBe(then.matchedNodeList); + expect(component.treeComponent.nzFlattenNodes.length) + .withContext('treeComponent.nzFlattenNodes.length') + .toBe(then.nzFlattenNodes); + expect(nativeElement.querySelectorAll('nz-tree-node').length) + .withContext('number of displayed nz-tree-node elements') + .toBe(then.nzFlattenNodes); + }) + ); + }); + it('should match nodes based on nzSearchFunc', fakeAsync(() => { const { component, fixture, nativeElement } = testBed; component.searchFunc = (data: NzTreeNodeOptions): boolean => data.title === component.searchValue; @@ -588,6 +652,7 @@ describe('tree', () => { [nzMultiple]="multiple" [nzSearchValue]="searchValue" [nzSearchFunc]="searchFunc" + [nzVirtualHeight]="virtualHeight" [nzHideUnMatched]="hideUnMatched" [nzExpandAll]="expandAll" [nzExpandedIcon]="expandedIcon" @@ -619,6 +684,7 @@ export class NzTestTreeBasicControlledComponent { defaultExpandedKeys: string[] = []; expandedIcon?: TemplateRef<{ $implicit: NzTreeNode; origin: NzTreeNodeOptions }>; searchFunc?: (node: NzTreeNodeOptions) => boolean; + virtualHeight?: string | boolean = false; hideUnMatched = false; nodes: NzTreeNodeOptions[] | NzTreeNode[] = [ {