Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(module:tree): tree select search slow in virtual mode #7385

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions components/tree-select/demo/virtual-scroll.ts
Expand Up @@ -11,6 +11,7 @@ import { NzTreeNodeOptions } from 'ng-zorro-antd/tree';
nzShowSearch
nzPlaceHolder="Please select"
nzVirtualHeight="300px"
nzHideUnMatched="true"
></nz-tree-select>
`
})
Expand Down
5 changes: 4 additions & 1 deletion components/tree/tree.component.ts
Expand Up @@ -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();
});

Expand Down
66 changes: 66 additions & 0 deletions components/tree/tree.spec.ts
Expand Up @@ -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;
Expand Down Expand Up @@ -588,6 +652,7 @@ describe('tree', () => {
[nzMultiple]="multiple"
[nzSearchValue]="searchValue"
[nzSearchFunc]="searchFunc"
[nzVirtualHeight]="virtualHeight"
[nzHideUnMatched]="hideUnMatched"
[nzExpandAll]="expandAll"
[nzExpandedIcon]="expandedIcon"
Expand Down Expand Up @@ -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[] = [
{
Expand Down