Skip to content

Commit

Permalink
fix(module: accordion): fix accordion not automatic expand bug (#575)
Browse files Browse the repository at this point in the history
  • Loading branch information
BronzeCui authored and fisherspy committed Sep 9, 2019
1 parent 6df7d7c commit 19b90da
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 30 deletions.
38 changes: 12 additions & 26 deletions components/accordion/accordion.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ export class AccordionComponent implements AfterContentInit, OnDestroy, OnChange
@HostBinding('class.am-accordion')
amAccordion: boolean = true;

@HostListener('click', ['$event'])
click(event) {
@HostListener('click')
click() {
let result: any = [];
this.groups.toArray().forEach(group => {
if (group.isOpened) {
Expand All @@ -75,7 +75,7 @@ export class AccordionComponent implements AfterContentInit, OnDestroy, OnChange
init() {
if (this.expandAll && this.groups && this.groups.length > 0) {
this._oldGroups = this.groups.toArray();
this._oldGroups.forEach((group, index) => {
this._oldGroups.forEach((group) => {
group.openOnInitialization();
});
this._subscription = this.groups.changes.subscribe(change => {
Expand All @@ -89,15 +89,16 @@ export class AccordionComponent implements AfterContentInit, OnDestroy, OnChange
});
}

let currentActiveKey = [];
if (
this.activeKey !== undefined &&
this.activeKey.length > 0 &&
!this.accordion &&
this.groups &&
this.groups.length > 0
) {
let currentActiveKey: Array<any> = [];
if (this.activeKey && this.activeKey.length > 0) {
currentActiveKey = this.toArray(this.activeKey);
if (this.accordion) {
currentActiveKey = currentActiveKey.slice(0, 1);
}
} else if (this.defaultActiveKey) {
currentActiveKey = [this.defaultActiveKey];
}
if (this.groups && this.groups.length > 0) {
this.groups.forEach((group, index) => {
currentActiveKey.forEach(key => {
if (index === parseInt(key, 0)) {
Expand All @@ -108,21 +109,6 @@ export class AccordionComponent implements AfterContentInit, OnDestroy, OnChange
}
});
});
} else if (
this.defaultActiveKey !== undefined &&
!this.expandAll &&
!this.accordion &&
this.groups &&
this.groups.length > 0
) {
this.groups.forEach((group, index) => {
if (index === parseInt(this.defaultActiveKey, 0)) {
setTimeout(() => {
group.isOpened = true;
group.openOnInitialization();
}, 0);
}
});
}
}

Expand Down
6 changes: 3 additions & 3 deletions components/accordion/demo/accordion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ import { Component } from '@angular/core';
`
})
export class DemoAccordionAccordionComponent {
activeKey = [0, 1];
activeKey = [1];
accordions: Array<any> = [
{ title: 'Title 1', child: ['content 1', 'content 1', 'content 1'] },
{ title: 'Title 2', child: ['content 2', 'content 2', 'content 2'], inactive: false },
{ title: 'Title 3', child: ['content 3', 'content 3', 'content 3'], inactive: true }
{ title: 'Title 2', child: ['content 2', 'content 2', 'content 2'] },
{ title: 'Title 3', child: ['content 3', 'content 3', 'content 3'] }
];

onChange(event) {
Expand Down
2 changes: 1 addition & 1 deletion components/accordion/demo/basic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { Component, ViewChild, AfterViewInit, OnInit } from '@angular/core';
`
})
export class DemoAccordionBasicComponent implements OnInit {
@ViewChild('title1', {static: false}) title1: ViewChild;
@ViewChild('title1', {static: true}) title1: ViewChild;
accordions: Array<any> = [];

activeKey = [0, 1];
Expand Down

0 comments on commit 19b90da

Please sign in to comment.