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-view): do not run change detection when the nz-tree-node-checkbox is clicked #7307

Merged
merged 1 commit into from Apr 21, 2022
Merged
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
47 changes: 38 additions & 9 deletions components/tree-view/checkbox.ts
Expand Up @@ -3,8 +3,21 @@
* found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE
*/

import { ChangeDetectionStrategy, Component, EventEmitter, Input, Output } from '@angular/core';
import {
ChangeDetectionStrategy,
ChangeDetectorRef,
Component,
ElementRef,
EventEmitter,
Input,
NgZone,
OnInit,
Output
} from '@angular/core';
import { fromEvent } from 'rxjs';
import { takeUntil } from 'rxjs/operators';

import { NzDestroyService } from 'ng-zorro-antd/core/services';
import { BooleanInput } from 'ng-zorro-antd/core/types';
import { InputBoolean } from 'ng-zorro-antd/core/util';

Expand All @@ -17,21 +30,37 @@ import { InputBoolean } from 'ng-zorro-antd/core/util';
class: 'ant-tree-checkbox',
'[class.ant-tree-checkbox-checked]': `nzChecked`,
'[class.ant-tree-checkbox-indeterminate]': `nzIndeterminate`,
'[class.ant-tree-checkbox-disabled]': `nzDisabled`,
'(click)': 'onClick($event)'
}
'[class.ant-tree-checkbox-disabled]': `nzDisabled`
},
providers: [NzDestroyService]
})
export class NzTreeNodeCheckboxComponent {
export class NzTreeNodeCheckboxComponent implements OnInit {
static ngAcceptInputType_nzDisabled: BooleanInput;

@Input() nzChecked?: boolean;
@Input() nzIndeterminate?: boolean;
@Input() @InputBoolean() nzDisabled?: boolean;
@Output() readonly nzClick = new EventEmitter<MouseEvent>();

onClick(e: MouseEvent): void {
if (!this.nzDisabled) {
this.nzClick.emit(e);
}
constructor(
private ngZone: NgZone,
private ref: ChangeDetectorRef,
private host: ElementRef<HTMLElement>,
private destroy$: NzDestroyService
) {}

ngOnInit(): void {
this.ngZone.runOutsideAngular(() =>
fromEvent<MouseEvent>(this.host.nativeElement, 'click')
.pipe(takeUntil(this.destroy$))
.subscribe((event: MouseEvent) => {
if (!this.nzDisabled && this.nzClick.observers.length) {
this.ngZone.run(() => {
this.nzClick.emit(event);
this.ref.markForCheck();
});
}
})
);
}
}