Skip to content

Commit 50ff89e

Browse files
committed
Create Expansion Panel.
1 parent 525ce1e commit 50ff89e

21 files changed

+657
-0
lines changed

src/demo-app/demo-app-module.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ import {PlatformDemo} from './platform/platform-demo';
4343
import {AutocompleteDemo} from './autocomplete/autocomplete-demo';
4444
import {InputDemo} from './input/input-demo';
4545
import {StyleDemo} from './style/style-demo';
46+
import {ExpansionDemo} from './expansion/expansion-demo';
4647

4748

4849
@NgModule({
@@ -100,6 +101,7 @@ import {StyleDemo} from './style/style-demo';
100101
RainyTabContent,
101102
FoggyTabContent,
102103
PlatformDemo,
104+
ExpansionDemo,
103105
],
104106
providers: [
105107
{provide: OverlayContainer, useClass: FullscreenOverlayContainer}

src/demo-app/demo-app/demo-app.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ export class DemoApp {
2626
{name: 'Chips', route: 'chips'},
2727
{name: 'Checkbox', route: 'checkbox'},
2828
{name: 'Dialog', route: 'dialog'},
29+
{name: 'Expansion Panel', route: 'expansion'},
2930
{name: 'Gestures', route: 'gestures'},
3031
{name: 'Grid List', route: 'grid-list'},
3132
{name: 'Icon', route: 'icon'},

src/demo-app/demo-app/routes.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import {PlatformDemo} from '../platform/platform-demo';
3232
import {AutocompleteDemo} from '../autocomplete/autocomplete-demo';
3333
import {InputDemo} from '../input/input-demo';
3434
import {StyleDemo} from '../style/style-demo';
35+
import {ExpansionDemo} from '../expansion/expansion-demo';
3536

3637
export const DEMO_APP_ROUTES: Routes = [
3738
{path: '', component: Home},
@@ -66,4 +67,5 @@ export const DEMO_APP_ROUTES: Routes = [
6667
{path: 'snack-bar', component: SnackBarDemo},
6768
{path: 'platform', component: PlatformDemo},
6869
{path: 'style', component: StyleDemo},
70+
{path: 'expansion', component: ExpansionDemo},
6971
];
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<h1>Single Expansion Panel</h1>
2+
<md-expansion-panel class="md-expansion-demo-width" #myPanel>
3+
<md-expansion-panel-header>
4+
<span title>{{ myPanel.expanded ? 'My Best Work' : 'Not My Best Work' }}</span>
5+
<span description *ngIf="myPanel.expanded">
6+
This is a description only shows while expanded.
7+
</span>
8+
</md-expansion-panel-header>
9+
This is the content text that makes sense here.
10+
<div md-action-row>
11+
<button md-button (click)="myPanel.expanded = false">CANCEL</button>
12+
<button md-button>SAVE</button>
13+
</div>
14+
</md-expansion-panel>
15+
<br>
16+
<h1>Accordion</h1>
17+
<div>
18+
<p>Accordion Options</p>
19+
<div>
20+
<md-slide-toggle [(ngModel)]="multi">Allow Multi Expansion</md-slide-toggle>
21+
<md-slide-toggle [(ngModel)]="showExpandIndicators">Show Indicators</md-slide-toggle>
22+
<md-slide-toggle [(ngModel)]="showPanel3">Show Panel 3</md-slide-toggle>
23+
</div>
24+
<p>Accordion Style</p>
25+
<md-radio-group [(ngModel)]="panelStyle">
26+
<md-radio-button value="default">Default</md-radio-button>
27+
<md-radio-button value="flat">Flat</md-radio-button>
28+
</md-radio-group>
29+
<p>Accordion Panel(s)</p>
30+
<div>
31+
<md-checkbox [(ngModel)]="panel1.expanded">Panel 1</md-checkbox>
32+
<md-checkbox [(ngModel)]="panel2.expanded">Panel 2</md-checkbox>
33+
</div>
34+
</div>
35+
<br>
36+
<div md-accordion [accordionStyle]="panelStyle" [multi]="multi"
37+
class="md-expansion-demo-width">
38+
<md-expansion-panel #panel1 [showExpandIndicator]="showExpandIndicators">
39+
<md-expansion-panel-header>
40+
Section 1
41+
</md-expansion-panel-header>
42+
<p>This is the content text that makes sense here.</p>
43+
</md-expansion-panel>
44+
<md-expansion-panel #panel2 [showExpandIndicator]="showExpandIndicators">
45+
<md-expansion-panel-header>
46+
Section 2
47+
</md-expansion-panel-header>
48+
<p>This is the content text that makes sense here.</p>
49+
</md-expansion-panel>
50+
<md-expansion-panel #panel3 *ngIf="showPanel3" [showExpandIndicator]="showExpandIndicators">
51+
<md-expansion-panel-header>
52+
Section 3
53+
</md-expansion-panel-header>
54+
<md-checkbox #showButtons>Reveal Buttons Below</md-checkbox>
55+
<div md-action-row *ngIf="showButtons.checked">
56+
<button md-button (click)="panel2.expanded = true">OPEN SECTION 2</button>
57+
<button md-button (click)="panel3.expanded = false">CLOSE</button>
58+
</div>
59+
</md-expansion-panel>
60+
</div>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.md-expansion-demo-width {
2+
width: 600px;
3+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import {Component, ViewEncapsulation} from '@angular/core';
2+
3+
@Component({
4+
moduleId: module.id,
5+
selector: 'expansion-demo',
6+
styleUrls: ['expansion-demo.css'],
7+
templateUrl: 'expansion-demo.html',
8+
encapsulation: ViewEncapsulation.None,
9+
})
10+
export class ExpansionDemo {
11+
panelStyle: string = 'default';
12+
multi: boolean = false;
13+
showExpandIndicators: boolean = true;
14+
showPanel3 = true;
15+
}

src/lib/core/theming/_all-theme.scss

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
@import '../../checkbox/checkbox-theme';
88
@import '../../chips/chips-theme';
99
@import '../../dialog/dialog-theme';
10+
@import '../../expansion/expansion-theme';
1011
@import '../../grid-list/grid-list-theme';
1112
@import '../../icon/icon-theme';
1213
@import '../../input/input-theme';
@@ -34,6 +35,7 @@
3435
@include mat-checkbox-theme($theme);
3536
@include mat-chips-theme($theme);
3637
@include mat-dialog-theme($theme);
38+
@include mat-expansion-panel-theme($theme);
3739
@include mat-grid-list-theme($theme);
3840
@include mat-icon-theme($theme);
3941
@include mat-input-theme($theme);
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
@import '../core/theming/palette';
2+
@import '../core/theming/theming';
3+
4+
@mixin mat-expansion-panel-theme($theme) {
5+
$background: map-get($theme, background);
6+
$foreground: map-get($theme, foreground);
7+
8+
.mat-expansion-panel {
9+
background: mat-color($background, card);
10+
color: mat-color($foreground, base);
11+
}
12+
}

src/lib/expansion/accordion.spec.ts

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import {async, TestBed} from '@angular/core/testing';
2+
import {Component} from '@angular/core';
3+
import {By} from '@angular/platform-browser';
4+
import {MdExpansionModule} from './index';
5+
6+
7+
describe('MdAccordion', () => {
8+
beforeEach(async(() => {
9+
TestBed.configureTestingModule({
10+
imports: [MdExpansionModule],
11+
declarations: [
12+
SetOfPanels
13+
],
14+
});
15+
TestBed.compileComponents();
16+
}));
17+
18+
it('should close all other panels upon a panel being opened', () => {
19+
let fixture = TestBed.createComponent(SetOfPanels);
20+
let panels = fixture.debugElement.queryAll(By.css('.mat-expansion-panel'));
21+
22+
fixture.componentInstance.firstPanelExpanded = true;
23+
fixture.detectChanges();
24+
expect(panels[0].classes['mat-expanded']).toBeTruthy();
25+
expect(panels[1].classes['mat-expanded']).toBeFalsy();
26+
27+
fixture.componentInstance.secondPanelExpanded = true;
28+
fixture.detectChanges();
29+
expect(panels[0].classes['mat-expanded']).toBeFalsy();
30+
expect(panels[1].classes['mat-expanded']).toBeTruthy();
31+
});
32+
33+
it('should allow multiple panels to be open simultaneously', () => {
34+
let fixture = TestBed.createComponent(SetOfPanels);
35+
let panels = fixture.debugElement.queryAll(By.css('.mat-expansion-panel'));
36+
37+
fixture.componentInstance.multi = true;
38+
fixture.componentInstance.firstPanelExpanded = true;
39+
fixture.componentInstance.secondPanelExpanded = true;
40+
fixture.detectChanges();
41+
expect(panels[0].classes['mat-expanded']).toBeTruthy();
42+
expect(panels[1].classes['mat-expanded']).toBeTruthy();
43+
});
44+
});
45+
46+
47+
@Component({template: `
48+
<div md-accordion [multi]="multi">
49+
<md-expansion-panel [expanded]="firstPanelExpanded">
50+
<md-expansion-panel-header>Summary</md-expansion-panel-header>
51+
<p>Content</p>
52+
</md-expansion-panel>
53+
<md-expansion-panel [expanded]="secondPanelExpanded">
54+
<md-expansion-panel-header>Summary</md-expansion-panel-header>
55+
<p>Content</p>
56+
</md-expansion-panel>
57+
</div>`})
58+
class SetOfPanels {
59+
multi: boolean = false;
60+
firstPanelExpanded: boolean = false;
61+
secondPanelExpanded: boolean = false;
62+
}

src/lib/expansion/accordion.ts

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
import {
2+
Directive,
3+
ElementRef,
4+
EventEmitter,
5+
Host,
6+
Input,
7+
Output,
8+
ViewEncapsulation,
9+
AfterContentInit,
10+
OnDestroy,
11+
ContentChildren,
12+
QueryList,
13+
Optional,
14+
forwardRef,
15+
} from '@angular/core';
16+
import {
17+
trigger,
18+
state,
19+
style,
20+
transition,
21+
animate,
22+
} from '@angular/animations';
23+
import {coerceBooleanProperty} from '../core/coercion/boolean-property';
24+
import {Subscription} from 'rxjs/Subscription';
25+
import {Subject} from 'rxjs/Subject';
26+
import 'rxjs/add/operator/takeUntil';
27+
28+
/** Used to generate unique ID for each expansion panel. */
29+
let nextId = 0;
30+
31+
export class MdAccordionChild implements OnDestroy {
32+
/** Event emitted every time the MdAccordianChild is closed. */
33+
@Output() close = new EventEmitter<null>();
34+
/** Event emitted every time the MdAccordianChild is opened. */
35+
@Output() open = new EventEmitter<null>();
36+
/** Event emitted when the MdAccordianChild is destroyed. */
37+
@Output() destroy = new EventEmitter<null>();
38+
/** Whether the MdAccordianChild is expanded. */
39+
expanded: boolean;
40+
41+
/** The unique MdAccordianChild id. */
42+
private _id: number = nextId++;
43+
get id(): string { return `md-accordion-child-${this._id}`; }
44+
/** Emits an event for the accordion child being destroyed. */
45+
ngOnDestroy() {
46+
this.destroy.emit();
47+
}
48+
}
49+
50+
@Directive({
51+
selector: '[md-accordion], [mat-accordion]',
52+
})
53+
export class MdAccordion implements AfterContentInit, OnDestroy {
54+
/** Whether the expansion indicator should be shown. */
55+
private _showIndicator: boolean = true;
56+
@Input()
57+
get showExpandIndicator(): boolean { return this._showIndicator; }
58+
set showExpandIndicator(show: boolean) { this._showIndicator = coerceBooleanProperty(show); }
59+
/** Whether the panel set should use flat styling. */
60+
@Input() accordionStyle: string = 'default';
61+
/** Whether the panel set should allow multiple open panels. */
62+
private _multi: boolean = false;
63+
@Input()
64+
get multi(): boolean { return this._multi; }
65+
set multi(multi: boolean) { this._multi = coerceBooleanProperty(multi); }
66+
67+
/** A subject to be completed on destroy to clean up subscriptions. */
68+
private _destroySubject = new Subject();
69+
/** A list of subscriptions for panel open events. */
70+
private _childrenSet: Set<MdAccordionChild> = new Set();
71+
/** QueryList of all expansion panels in the expansion panel set. */
72+
@ContentChildren(MdAccordionChild) private _children: QueryList<MdAccordionChild>;
73+
74+
/** Set up event subscriptions for all panels for when they open, closing the other panels. */
75+
ngAfterContentInit() {
76+
this.registerChildren(this._children);
77+
this._children.changes
78+
.takeUntil(this._destroySubject)
79+
.subscribe((children: QueryList<MdAccordionChild>) => {
80+
this.registerChildren(children);
81+
});
82+
}
83+
84+
/** Clean up panel subscriptions. */
85+
ngOnDestroy() {
86+
this._destroySubject.next();
87+
this._destroySubject.complete();
88+
}
89+
90+
/** Registers a QueryList of panels. */
91+
private registerChildren(panels: QueryList<MdAccordionChild>) {
92+
this._children.forEach(panel => this.registerChild(panel));
93+
}
94+
95+
/**
96+
* Registers an MdExpansion panel if has not already been registered. Creates subscriptions
97+
* for the open and destroy events for the panel.
98+
*/
99+
private registerChild(child: MdAccordionChild) {
100+
// If the accordian child is already in the set, exit method.
101+
if (this._childrenSet.has(child)) {
102+
return;
103+
}
104+
// Subscribes to an accordion child's open event emitter to coordinate the expanded states.
105+
child.open
106+
.takeUntil(this._destroySubject)
107+
.subscribe(() => {
108+
if (!this.multi) {
109+
this._childrenSet.forEach(c => c.expanded = child.id === c.id);
110+
}
111+
});
112+
// Subscribes to an accordian child's destory event emitter to remove the accord child from the
113+
// set when appropriate.
114+
child.destroy
115+
.takeUntil(this._destroySubject)
116+
.subscribe(() => this._childrenSet.delete(child));
117+
// Adds the accordian child to the set.
118+
this._childrenSet.add(child);
119+
}
120+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<span class="mat-content">
2+
<ng-content select="[title]"></ng-content>
3+
<ng-content select="[description]"></ng-content>
4+
<ng-content></ng-content>
5+
</span>
6+
<span [@indicatorRotate]="getExpandedState()" *ngIf="getShowExpandIndicator()"
7+
class="mat-expansion-indicator"></span>

0 commit comments

Comments
 (0)