Skip to content

Commit 3c3eac9

Browse files
authored
perf(module:collapse): do not run change detection if the panel is disabled (#7181)
1 parent 4bd86ca commit 3c3eac9

2 files changed

Lines changed: 31 additions & 22 deletions

File tree

components/collapse/collapse-panel.component.ts

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,26 @@ import {
77
ChangeDetectionStrategy,
88
ChangeDetectorRef,
99
Component,
10+
ElementRef,
1011
EventEmitter,
1112
Host,
1213
Input,
14+
NgZone,
1315
OnDestroy,
1416
OnInit,
1517
Optional,
1618
Output,
1719
TemplateRef,
20+
ViewChild,
1821
ViewEncapsulation
1922
} from '@angular/core';
20-
import { Subject } from 'rxjs';
21-
import { takeUntil } from 'rxjs/operators';
23+
import { fromEvent } from 'rxjs';
24+
import { filter, takeUntil } from 'rxjs/operators';
2225

2326
import { collapseMotion } from 'ng-zorro-antd/core/animation';
2427
import { NzConfigKey, NzConfigService, WithConfig } from 'ng-zorro-antd/core/config';
2528
import { NzNoAnimationDirective } from 'ng-zorro-antd/core/no-animation';
29+
import { NzDestroyService } from 'ng-zorro-antd/core/services';
2630
import { BooleanInput } from 'ng-zorro-antd/core/types';
2731
import { InputBoolean } from 'ng-zorro-antd/core/util';
2832

@@ -37,7 +41,7 @@ const NZ_CONFIG_MODULE_NAME: NzConfigKey = 'collapsePanel';
3741
encapsulation: ViewEncapsulation.None,
3842
animations: [collapseMotion],
3943
template: `
40-
<div role="button" [attr.aria-expanded]="nzActive" class="ant-collapse-header" (click)="clickHeader()">
44+
<div #collapseHeader role="button" [attr.aria-expanded]="nzActive" class="ant-collapse-header">
4145
<div *ngIf="nzShowArrow">
4246
<ng-container *nzStringTemplateOutlet="nzExpandedIcon; let expandedIcon">
4347
<i nz-icon [nzType]="expandedIcon || 'right'" class="ant-collapse-arrow" [nzRotate]="nzActive ? 90 : 0"></i>
@@ -65,7 +69,8 @@ const NZ_CONFIG_MODULE_NAME: NzConfigKey = 'collapsePanel';
6569
'[class.ant-collapse-no-arrow]': '!nzShowArrow',
6670
'[class.ant-collapse-item-active]': 'nzActive',
6771
'[class.ant-collapse-item-disabled]': 'nzDisabled'
68-
}
72+
},
73+
providers: [NzDestroyService]
6974
})
7075
export class NzCollapsePanelComponent implements OnInit, OnDestroy {
7176
readonly _nzModuleName: NzConfigKey = NZ_CONFIG_MODULE_NAME;
@@ -80,20 +85,18 @@ export class NzCollapsePanelComponent implements OnInit, OnDestroy {
8085
@Input() nzHeader?: string | TemplateRef<void>;
8186
@Input() nzExpandedIcon?: string | TemplateRef<void>;
8287
@Output() readonly nzActiveChange = new EventEmitter<boolean>();
83-
private destroy$ = new Subject();
84-
clickHeader(): void {
85-
if (!this.nzDisabled) {
86-
this.nzCollapseComponent.click(this);
87-
}
88-
}
88+
89+
@ViewChild('collapseHeader', { static: true }) collapseHeader!: ElementRef<HTMLElement>;
8990

9091
markForCheck(): void {
9192
this.cdr.markForCheck();
9293
}
9394

9495
constructor(
9596
public nzConfigService: NzConfigService,
97+
private ngZone: NgZone,
9698
private cdr: ChangeDetectorRef,
99+
private destroy$: NzDestroyService,
97100
@Host() private nzCollapseComponent: NzCollapseComponent,
98101
@Optional() public noAnimation?: NzNoAnimationDirective
99102
) {
@@ -107,11 +110,20 @@ export class NzCollapsePanelComponent implements OnInit, OnDestroy {
107110

108111
ngOnInit(): void {
109112
this.nzCollapseComponent.addPanel(this);
113+
114+
this.ngZone.runOutsideAngular(() =>
115+
fromEvent(this.collapseHeader.nativeElement, 'click')
116+
.pipe(
117+
filter(() => !this.nzDisabled),
118+
takeUntil(this.destroy$)
119+
)
120+
.subscribe(() => {
121+
this.ngZone.run(() => this.nzCollapseComponent.click(this));
122+
})
123+
);
110124
}
111125

112126
ngOnDestroy(): void {
113-
this.destroy$.next();
114-
this.destroy$.complete();
115127
this.nzCollapseComponent.removePanel(this);
116128
}
117129
}

components/collapse/collapse.component.ts

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,14 @@ import {
99
ChangeDetectorRef,
1010
Component,
1111
Input,
12-
OnDestroy,
1312
OnInit,
1413
Optional,
1514
ViewEncapsulation
1615
} from '@angular/core';
17-
import { Subject } from 'rxjs';
1816
import { takeUntil } from 'rxjs/operators';
1917

2018
import { NzConfigKey, NzConfigService, WithConfig } from 'ng-zorro-antd/core/config';
19+
import { NzDestroyService } from 'ng-zorro-antd/core/services';
2120
import { BooleanInput } from 'ng-zorro-antd/core/types';
2221
import { InputBoolean } from 'ng-zorro-antd/core/util';
2322

@@ -38,9 +37,10 @@ const NZ_CONFIG_MODULE_NAME: NzConfigKey = 'collapse';
3837
'[class.ant-collapse-ghost]': `nzGhost`,
3938
'[class.ant-collapse-borderless]': '!nzBordered',
4039
'[class.ant-collapse-rtl]': "dir === 'rtl'"
41-
}
40+
},
41+
providers: [NzDestroyService]
4242
})
43-
export class NzCollapseComponent implements OnDestroy, OnInit {
43+
export class NzCollapseComponent implements OnInit {
4444
readonly _nzModuleName: NzConfigKey = NZ_CONFIG_MODULE_NAME;
4545
static ngAcceptInputType_nzAccordion: BooleanInput;
4646
static ngAcceptInputType_nzBordered: BooleanInput;
@@ -54,11 +54,12 @@ export class NzCollapseComponent implements OnDestroy, OnInit {
5454
dir: Direction = 'ltr';
5555

5656
private listOfNzCollapsePanelComponent: NzCollapsePanelComponent[] = [];
57-
private destroy$ = new Subject();
57+
5858
constructor(
5959
public nzConfigService: NzConfigService,
6060
private cdr: ChangeDetectorRef,
61-
@Optional() private directionality: Directionality
61+
@Optional() private directionality: Directionality,
62+
private destroy$: NzDestroyService
6263
) {
6364
this.nzConfigService
6465
.getConfigChangeEventForComponent(NZ_CONFIG_MODULE_NAME)
@@ -100,8 +101,4 @@ export class NzCollapseComponent implements OnDestroy, OnInit {
100101
collapse.nzActive = !collapse.nzActive;
101102
collapse.nzActiveChange.emit(collapse.nzActive);
102103
}
103-
ngOnDestroy(): void {
104-
this.destroy$.next();
105-
this.destroy$.complete();
106-
}
107104
}

0 commit comments

Comments
 (0)