Skip to content

Commit

Permalink
fix(module: accordion): fix accordion support async load accordionPan…
Browse files Browse the repository at this point in the history
…el (#355)
  • Loading branch information
nuonuoge authored and Guoyuanqiang committed Mar 25, 2019
1 parent aeb3cbe commit 924bcc2
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
11 changes: 9 additions & 2 deletions components/accordion/accordion.component.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Component, ViewChild } from '@angular/core';
import { Component, ViewChild, OnInit } from '@angular/core';
import { By } from '@angular/platform-browser';
import { async, ComponentFixture, fakeAsync, tick, TestBed } from '@angular/core/testing';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
Expand Down Expand Up @@ -112,7 +112,7 @@ describe('AccordionComponent', () => {
</Accordion>
`
})
export class TestAccordionComponent {
export class TestAccordionComponent implements OnInit {
defaultActiveKey = '0';
accordions: Array<any> = [{ title: 'Title 1', child: ['content 1', 'content 1', 'content 1'], inactive: false },
{ title: 'Title 2', child: ['content 2', 'content 2', 'content 2'] }];
Expand All @@ -125,4 +125,11 @@ export class TestAccordionComponent {
onChange(event) {
console.log(event);
}

ngOnInit() {
setTimeout(() => {
this.accordions = [{ title: 'Title 1', child: ['content 1', 'content 1', 'content 1'], inactive: false },
{ title: 'Title 2', child: ['content 2', 'content 2', 'content 2'] }];
}, 0);
}
}
18 changes: 17 additions & 1 deletion components/accordion/accordion.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import { Subscription } from 'rxjs';
export class AccordionComponent implements AfterContentInit, OnDestroy, OnChanges {
private _oldGroups: AccordionGroupComponent[];
private _subscription: Subscription;
private groupsSubscription: Subscription;
private isFirstChange: boolean = true;

@ContentChildren(forwardRef(() => AccordionGroupComponent))
groups: QueryList<AccordionGroupComponent>;
Expand Down Expand Up @@ -95,6 +97,7 @@ export class AccordionComponent implements AfterContentInit, OnDestroy, OnChange
if (index === parseInt(key, 0)) {
setTimeout(() => {
group.isOpened = true;
group.openOnInitialization();
}, 0);
}
});
Expand All @@ -104,6 +107,7 @@ export class AccordionComponent implements AfterContentInit, OnDestroy, OnChange
if (index === parseInt(this.defaultActiveKey, 0)) {
setTimeout(() => {
group.isOpened = true;
group.openOnInitialization();
}, 0);
}
});
Expand All @@ -129,12 +133,24 @@ export class AccordionComponent implements AfterContentInit, OnDestroy, OnChange
}

ngAfterContentInit() {
this.init();
if (this.groups && this.groups.length > 0) {
this.init();
} else {
this.groupsSubscription = this.groups.changes.subscribe(group => {
if (this.isFirstChange) {
this.init();
}
this.isFirstChange = false;
});
}
}

ngOnDestroy() {
if (this._subscription) {
this._subscription.unsubscribe();
}
if (this.groupsSubscription) {
this.groupsSubscription.unsubscribe();
}
}
}

0 comments on commit 924bcc2

Please sign in to comment.