From 5bb1a998f709c76e373008d891b45a0355b9582f Mon Sep 17 00:00:00 2001 From: vthinkxie Date: Thu, 6 Dec 2018 18:26:21 +0800 Subject: [PATCH] fix(module:collapse): fix collapse (#2597) close #2567 --- .../collapse/nz-collapse-panel.component.ts | 22 ++++++---- components/collapse/nz-collapse.component.ts | 44 +++++-------------- 2 files changed, 25 insertions(+), 41 deletions(-) diff --git a/components/collapse/nz-collapse-panel.component.ts b/components/collapse/nz-collapse-panel.component.ts index 7a494fc6a25..9a6f0a6a634 100644 --- a/components/collapse/nz-collapse-panel.component.ts +++ b/components/collapse/nz-collapse-panel.component.ts @@ -3,17 +3,16 @@ import { ChangeDetectionStrategy, ChangeDetectorRef, Component, - EventEmitter, + EventEmitter, Host, HostBinding, - Input, + Input, OnDestroy, OnInit, Output, TemplateRef, ViewEncapsulation } from '@angular/core'; -import { Subject } from 'rxjs'; - import { InputBoolean } from '../core/util/convert'; +import { NzCollapseComponent } from './nz-collapse.component'; @Component({ selector : 'nz-collapse-panel', @@ -45,17 +44,16 @@ import { InputBoolean } from '../core/util/convert'; } }) -export class NzCollapsePanelComponent { +export class NzCollapsePanelComponent implements OnInit, OnDestroy { @Input() @InputBoolean() @HostBinding('class.ant-collapse-item-active') nzActive = false; @Input() @InputBoolean() @HostBinding('class.ant-collapse-item-disabled') nzDisabled = false; @Input() @InputBoolean() nzShowArrow = true; @Input() nzHeader: string | TemplateRef; @Output() readonly nzActiveChange = new EventEmitter(); - click$ = new Subject(); clickHeader(): void { if (!this.nzDisabled) { - this.click$.next(this); + this.nzCollapseComponent.click(this); } } @@ -63,6 +61,14 @@ export class NzCollapsePanelComponent { this.cdr.markForCheck(); } - constructor(private cdr: ChangeDetectorRef) { + constructor(private cdr: ChangeDetectorRef, @Host() private nzCollapseComponent: NzCollapseComponent) { + } + + ngOnInit(): void { + this.nzCollapseComponent.addPanel(this); + } + + ngOnDestroy(): void { + this.nzCollapseComponent.removePanel(this); } } diff --git a/components/collapse/nz-collapse.component.ts b/components/collapse/nz-collapse.component.ts index 21cc144396a..b11f0920d64 100644 --- a/components/collapse/nz-collapse.component.ts +++ b/components/collapse/nz-collapse.component.ts @@ -1,16 +1,10 @@ import { - AfterContentInit, ChangeDetectionStrategy, Component, - ContentChildren, Input, - OnDestroy, - QueryList, ViewEncapsulation } from '@angular/core'; - -import { merge, Subject, Subscription } from 'rxjs'; -import { startWith, takeUntil } from 'rxjs/operators'; +import { NzCheckboxComponent } from '../checkbox'; import { InputBoolean } from '../core/util/convert'; import { NzCollapsePanelComponent } from './nz-collapse-panel.component'; @@ -25,12 +19,18 @@ import { NzCollapsePanelComponent } from './nz-collapse-panel.component'; }` ] }) -export class NzCollapseComponent implements AfterContentInit, OnDestroy { - @ContentChildren(NzCollapsePanelComponent) listOfNzCollapsePanelComponent: QueryList; +export class NzCollapseComponent { + private listOfNzCollapsePanelComponent: NzCollapsePanelComponent[] = []; @Input() @InputBoolean() nzAccordion = false; @Input() @InputBoolean() nzBordered = true; - private destroy$ = new Subject(); - private clickSubscription: Subscription; + + addPanel(value: NzCollapsePanelComponent): void { + this.listOfNzCollapsePanelComponent.push(value); + } + + removePanel(value: NzCollapsePanelComponent): void { + this.listOfNzCollapsePanelComponent.splice(this.listOfNzCollapsePanelComponent.indexOf(value), 1); + } click(collapse: NzCollapsePanelComponent): void { if (this.nzAccordion && !collapse.nzActive) { @@ -45,26 +45,4 @@ export class NzCollapseComponent implements AfterContentInit, OnDestroy { collapse.nzActive = !collapse.nzActive; collapse.nzActiveChange.emit(collapse.nzActive); } - - ngAfterContentInit(): void { - this.listOfNzCollapsePanelComponent.changes.pipe( - startWith(null), - takeUntil(this.destroy$) - ).subscribe(() => { - if (this.clickSubscription) { - this.clickSubscription.unsubscribe(); - } - this.clickSubscription = merge(...this.listOfNzCollapsePanelComponent.map(item => item.click$)).pipe( - takeUntil(this.destroy$) - ).subscribe((data) => { - this.click(data); - }); - }); - - } - - ngOnDestroy(): void { - this.destroy$.next(); - this.destroy$.complete(); - } }