Skip to content

Commit 21208f0

Browse files
authored
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
1 parent 3771512 commit 21208f0

3 files changed

Lines changed: 71 additions & 1 deletion

File tree

components/tree-select/demo/virtual-scroll.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { NzTreeNodeOptions } from 'ng-zorro-antd/tree';
1111
nzShowSearch
1212
nzPlaceHolder="Please select"
1313
nzVirtualHeight="300px"
14+
nzHideUnMatched="true"
1415
></nz-tree-select>
1516
`
1617
})

components/tree/tree.component.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,10 @@ export class NzTreeComponent
485485

486486
ngOnInit(): void {
487487
this.nzTreeService.flattenNodes$.pipe(takeUntil(this.destroy$)).subscribe(data => {
488-
this.nzFlattenNodes = data;
488+
this.nzFlattenNodes =
489+
!!this.nzVirtualHeight && this.nzHideUnMatched && this.nzSearchValue?.length > 0
490+
? data.filter(d => !d.canHide)
491+
: data;
489492
this.cdr.markForCheck();
490493
});
491494

components/tree/tree.spec.ts

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,70 @@ describe('tree', () => {
144144
expect(component.treeComponent.getMatchedNodeList().length).toEqual(1);
145145
}));
146146

147+
[
148+
{
149+
title:
150+
"should display 7 nodes when hideUnMatched=false and virtualHeight = undefined and nzSearchValue = '0-1'",
151+
when: { hideUnMatched: false, searchValue: '0-1' },
152+
then: { matchedNodeList: 3, nzFlattenNodes: 7 }
153+
},
154+
{
155+
title:
156+
"should display 7 nodes when hideUnMatched=false and virtualHeight = '300px' and nzSearchValue = '0-1'",
157+
when: { hideUnMatched: false, virtualHeight: '300px', searchValue: '0-1' },
158+
then: { matchedNodeList: 3, nzFlattenNodes: 7 }
159+
},
160+
{
161+
title:
162+
"'should display 7 nodes when hideUnMatched=true and virtualHeight = undefined and nzSearchValue = '0-1'",
163+
when: { hideUnMatched: true, searchValue: '0-1' },
164+
then: { matchedNodeList: 3, nzFlattenNodes: 7 }
165+
},
166+
{
167+
title:
168+
"should display 4 matched nodes based on nzSearchValue when hideUnMatched=true and virtualHeight = '300px' and nzSearchValue = undefined",
169+
when: { hideUnMatched: true, virtualHeight: '300px' },
170+
then: { matchedNodeList: 0, nzFlattenNodes: 3 }
171+
},
172+
{
173+
title:
174+
"should display 4 matched nodes based on nzSearchValue when hideUnMatched=true and virtualHeight = '300px' and nzSearchValue = ''",
175+
when: { hideUnMatched: true, virtualHeight: '300px', searchValue: '' },
176+
then: { matchedNodeList: 0, nzFlattenNodes: 3 }
177+
},
178+
{
179+
title:
180+
"should display 4 matched nodes based on nzSearchValue when hideUnMatched=true and virtualHeight = '300px' and nzSearchValue = '0-1'",
181+
when: { hideUnMatched: true, virtualHeight: '300px', searchValue: '0-1' },
182+
then: { matchedNodeList: 3, nzFlattenNodes: 4 }
183+
}
184+
].forEach(({ title, when, then }) => {
185+
it(
186+
title,
187+
fakeAsync(() => {
188+
// Given
189+
const { component, fixture, nativeElement } = testBed;
190+
component.searchValue = when.searchValue;
191+
component.virtualHeight = when.virtualHeight;
192+
component.hideUnMatched = when.hideUnMatched;
193+
// When
194+
fixture.detectChanges();
195+
tick(300);
196+
fixture.detectChanges();
197+
// Then
198+
expect(component.treeComponent.getMatchedNodeList().length)
199+
.withContext('treeComponent.getMatchedNodeList().length')
200+
.toBe(then.matchedNodeList);
201+
expect(component.treeComponent.nzFlattenNodes.length)
202+
.withContext('treeComponent.nzFlattenNodes.length')
203+
.toBe(then.nzFlattenNodes);
204+
expect(nativeElement.querySelectorAll('nz-tree-node').length)
205+
.withContext('number of displayed nz-tree-node elements')
206+
.toBe(then.nzFlattenNodes);
207+
})
208+
);
209+
});
210+
147211
it('should match nodes based on nzSearchFunc', fakeAsync(() => {
148212
const { component, fixture, nativeElement } = testBed;
149213
component.searchFunc = (data: NzTreeNodeOptions): boolean => data.title === component.searchValue;
@@ -588,6 +652,7 @@ describe('tree', () => {
588652
[nzMultiple]="multiple"
589653
[nzSearchValue]="searchValue"
590654
[nzSearchFunc]="searchFunc"
655+
[nzVirtualHeight]="virtualHeight"
591656
[nzHideUnMatched]="hideUnMatched"
592657
[nzExpandAll]="expandAll"
593658
[nzExpandedIcon]="expandedIcon"
@@ -619,6 +684,7 @@ export class NzTestTreeBasicControlledComponent {
619684
defaultExpandedKeys: string[] = [];
620685
expandedIcon?: TemplateRef<{ $implicit: NzTreeNode; origin: NzTreeNodeOptions }>;
621686
searchFunc?: (node: NzTreeNodeOptions) => boolean;
687+
virtualHeight?: string | boolean = false;
622688
hideUnMatched = false;
623689
nodes: NzTreeNodeOptions[] | NzTreeNode[] = [
624690
{

0 commit comments

Comments
 (0)