Skip to content

Commit

Permalink
feat(accordion): add panels type option
Browse files Browse the repository at this point in the history
Closes #420
Closes #431
  • Loading branch information
sendilkumarn authored and pkozlowski-opensource committed Jul 13, 2016
1 parent a546197 commit b5ad117
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 5 deletions.
52 changes: 48 additions & 4 deletions src/accordion/accordion.spec.ts
Expand Up @@ -31,8 +31,9 @@ describe('ngb-accordion', () => {

beforeEach(() => {
html = `
<ngb-accordion #acc="ngbAccordion" [closeOthers]="closeOthers" [activeIds]="activeIds" (change)="changeCallback($event)">
<ngb-panel *ngFor="let panel of panels" [id]="panel.id" [disabled]="panel.disabled">
<ngb-accordion #acc="ngbAccordion" [closeOthers]="closeOthers" [activeIds]="activeIds"
(change)="changeCallback($event)" [type]="classType">
<ngb-panel *ngFor="let panel of panels" [id]="panel.id" [disabled]="panel.disabled" [type]="panel.type">
<template ngbPanelTitle>{{panel.title}}</template>
<template ngbPanelContent>{{panel.content}}</template>
</ngb-panel>
Expand Down Expand Up @@ -271,15 +272,58 @@ describe('ngb-accordion', () => {
expectOpenPanels(fixture.nativeElement, [false, false, false]);
});
})));

it('should have specified type of accordion ', async(inject([TestComponentBuilder], (tcb) => {
const testHtml = `
<ngb-accordion #acc="ngbAccordion" [closeOthers]="closeOthers" [type]="type">
<ngb-panel *ngFor="let panel of panels" [id]="panel.id" [disabled]="panel.disabled">
<template ngbPanelTitle>{{panel.title}}</template>
<template ngbPanelContent>{{panel.content}}</template>
</ngb-panel>
</ngb-accordion>
<button *ngFor="let panel of panels" (click)="acc.toggle(panel.id)">Toggle the panel {{ panel.id }}</button>
`;
tcb.overrideTemplate(TestComponent, testHtml).createAsync(TestComponent).then((fixture) => {
fixture.detectChanges();

fixture.componentInstance.type = 'warning';
fixture.detectChanges();

let el = fixture.nativeElement.querySelectorAll('.card-header');
expect(el[0]).toHaveCssClass('card-warning');
expect(el[1]).toHaveCssClass('card-warning');
expect(el[2]).toHaveCssClass('card-warning');

});
})));

it('should override the type in accordion with type in panel', async(inject([TestComponentBuilder], (tcb) => {
tcb.overrideTemplate(TestComponent, html).createAsync(TestComponent).then((fixture) => {
fixture.detectChanges();
fixture.componentInstance.classType = 'warning';

const tc = fixture.componentInstance;
tc.panels[0].type = 'success';
tc.panels[1].type = 'danger';
fixture.detectChanges();

let el = fixture.nativeElement.querySelectorAll('.card-header');
expect(el[0]).toHaveCssClass('card-success');
expect(el[1]).toHaveCssClass('card-danger');
expect(el[2]).toHaveCssClass('card-warning');

});
})));
});

@Component({selector: 'test-cmp', directives: [NGB_ACCORDION_DIRECTIVES], template: ''})
class TestComponent {
activeIds = [];
classType;
closeOthers = false;
panels = [
{id: 'one', disabled: false, title: 'Panel 1', content: 'foo'},
{id: 'two', disabled: false, title: 'Panel 2', content: 'bar'},
{id: 'one', disabled: false, title: 'Panel 1', content: 'foo', type: ''},
{id: 'two', disabled: false, title: 'Panel 2', content: 'bar', type: ''},
{id: 'three', disabled: false, title: 'Panel 3', content: 'baz'}
];
changeCallback = () => {};
Expand Down
13 changes: 12 additions & 1 deletion src/accordion/accordion.ts
Expand Up @@ -53,6 +53,11 @@ export class NgbPanel {
*/
@Input() title: string;

/**
* Panel type (CSS class). Bootstrap 4 recognizes the following types: "success", "info", "warning" and "danger".
*/
@Input() type: string;

@ContentChild(NgbPanelContent) contentTpl: NgbPanelContent;
@ContentChild(NgbPanelTitle) titleTpl: NgbPanelTitle;
}
Expand All @@ -76,7 +81,7 @@ export interface NgbPanelChangeEvent {
template: `
<div class="card">
<template ngFor let-panel [ngForOf]="_panels">
<div class="card-header" [class.active]="_isOpen(panel.id)">
<div [class]="'card-header ' + (panel.type ? 'card-'+panel.type: type ? 'card-'+type : '')" [class.active]="_isOpen(panel.id)">
<a tabindex="0" (click)="toggle(panel.id)" [class.text-muted]="panel.disabled">
{{panel.title}}<template [ngTemplateOutlet]="panel.titleTpl?.templateRef"></template>
</a>
Expand All @@ -101,6 +106,12 @@ export class NgbAccordion implements AfterContentChecked {
*/
@Input('closeOthers') closeOtherPanels: boolean = false;

/**
* Type of accordion's panels. Bootstrap 4 recognizes the following types: "success", "info", "warning" and "danger".
*/
@Input() type: string;


/**
* A panel change event fired right before the panel toggle happens. The event object has three properties:
* 'panelId', the id of panel that id toggled, 'nextState' whether panel will be opened (true) or closed (false),
Expand Down

0 comments on commit b5ad117

Please sign in to comment.