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

perf(module:tree): do not run change detection when the tree node is clicked #7128

Merged
merged 1 commit into from Dec 18, 2021
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
21 changes: 12 additions & 9 deletions components/tree/tree-node.component.ts
Expand Up @@ -108,8 +108,7 @@ import { InputBoolean } from 'ng-zorro-antd/core/util';
'[class.ant-tree-treenode-checkbox-indeterminate]': `!nzSelectMode && isHalfChecked`,
'[class.ant-tree-treenode-selected]': `!nzSelectMode && isSelected`,
'[class.ant-tree-treenode-loading]': `!nzSelectMode && isLoading`,
'[style.display]': 'displayStyle',
'(mousedown)': 'onMousedown($event)'
'[style.display]': 'displayStyle'
}
})
export class NzTreeNodeBuiltinComponent implements OnInit, OnChanges, OnDestroy {
Expand Down Expand Up @@ -195,12 +194,6 @@ export class NzTreeNodeBuiltinComponent implements OnInit, OnChanges, OnDestroy
return !this.isExpanded && !this.isLeaf;
}

onMousedown(event: MouseEvent): void {
if (this.nzSelectMode) {
event.preventDefault();
}
}

/**
* collapse node
*
Expand Down Expand Up @@ -404,13 +397,23 @@ export class NzTreeNodeBuiltinComponent implements OnInit, OnChanges, OnDestroy
public nzTreeService: NzTreeBaseService,
private ngZone: NgZone,
private renderer: Renderer2,
private elementRef: ElementRef,
private elementRef: ElementRef<HTMLElement>,
private cdr: ChangeDetectorRef,
@Host() @Optional() public noAnimation?: NzNoAnimationDirective
) {}

ngOnInit(): void {
this.nzTreeNode.component = this;

this.ngZone.runOutsideAngular(() => {
fromEvent(this.elementRef.nativeElement, 'mousedown')
.pipe(takeUntil(this.destroy$))
.subscribe(event => {
if (this.nzSelectMode) {
event.preventDefault();
}
});
});
}

ngOnChanges(changes: { [propertyName: string]: SimpleChange }): void {
Expand Down
26 changes: 24 additions & 2 deletions components/tree/tree.spec.ts
@@ -1,5 +1,5 @@
import { Component, TemplateRef, ViewChild } from '@angular/core';
import { fakeAsync, tick } from '@angular/core/testing';
import { ApplicationRef, Component, TemplateRef, ViewChild } from '@angular/core';
import { fakeAsync, TestBed, tick } from '@angular/core/testing';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { NoopAnimationsModule } from '@angular/platform-browser/animations';
import { Observable, of } from 'rxjs';
Expand Down Expand Up @@ -181,6 +181,26 @@ describe('tree', () => {
const hiddenNodes = nativeElement.querySelectorAll('nz-tree-node[builtin][style*="display: none;"]');
expect(hiddenNodes.length).toEqual(2);
}));

describe('change detection behavior', () => {
it('should not run change detection when the `nz-tree-node` is clicked', () => {
const { component, fixture, nativeElement } = testBed;
component.selectMode = true;
fixture.detectChanges();

const appRef = TestBed.inject(ApplicationRef);
const event = new MouseEvent('mousedown');

spyOn(appRef, 'tick');
spyOn(event, 'preventDefault').and.callThrough();

const treeNode = nativeElement.querySelector('nz-tree-node')!;
treeNode.dispatchEvent(event);

expect(appRef.tick).not.toHaveBeenCalled();
expect(event.preventDefault).toHaveBeenCalled();
});
});
});

describe('basic style of tree', () => {
Expand Down Expand Up @@ -577,6 +597,7 @@ describe('tree', () => {
[nzExpandAll]="expandAll"
[nzExpandedIcon]="expandedIcon"
[nzAsyncData]="asyncData"
[nzSelectMode]="selectMode"
(nzSearchValueChange)="nzEvent($event)"
(nzClick)="nzEvent($event)"
(nzDblClick)="nzEvent($event)"
Expand All @@ -595,6 +616,7 @@ export class NzTestTreeBasicControlledComponent {
multiple = true;
expandAll = false;
asyncData = false;
selectMode = false;
checkStrictly = false;
showLine = false;
defaultCheckedKeys: string[] = [];
Expand Down