@@ -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
2326import { collapseMotion } from 'ng-zorro-antd/core/animation' ;
2427import { NzConfigKey , NzConfigService , WithConfig } from 'ng-zorro-antd/core/config' ;
2528import { NzNoAnimationDirective } from 'ng-zorro-antd/core/no-animation' ;
29+ import { NzDestroyService } from 'ng-zorro-antd/core/services' ;
2630import { BooleanInput } from 'ng-zorro-antd/core/types' ;
2731import { 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} )
7075export 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}
0 commit comments