Skip to content

Commit 25de2f2

Browse files
committed
Create Expansion Panel.
1 parent 0e24345 commit 25de2f2

22 files changed

+746
-14
lines changed

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,15 @@ import {AutocompleteDemo} from './autocomplete/autocomplete-demo';
3838
import {InputDemo} from './input/input-demo';
3939
import {StyleDemo} from './style/style-demo';
4040
import {DatepickerDemo} from './datepicker/datepicker-demo';
41+
import {ExpansionDemo} from './expansion/expansion-demo';
4142
import {FullscreenOverlayContainer, OverlayContainer} from '@angular/material';
4243
import {
4344
MdAutocompleteModule, MdButtonModule, MdButtonToggleModule, MdCardModule, MdCheckboxModule,
44-
MdChipsModule, MdCoreModule, MdDatepickerModule, MdDialogModule, MdGridListModule, MdIconModule,
45-
MdInputModule, MdListModule, MdMenuModule, MdNativeDateModule, MdProgressBarModule, MdRadioModule,
46-
MdProgressSpinnerModule, MdRippleModule, MdSelectModule, MdSidenavModule, MdSliderModule,
47-
MdSlideToggleModule, MdSnackBarModule, MdTabsModule, MdToolbarModule, MdTooltipModule
45+
MdChipsModule, MdCoreModule, MdDatepickerModule, MdDialogModule, MdExpansionModule,
46+
MdGridListModule, MdIconModule, MdInputModule, MdListModule, MdMenuModule, MdNativeDateModule,
47+
MdProgressBarModule, MdRadioModule, MdProgressSpinnerModule, MdRippleModule, MdSelectModule,
48+
MdSidenavModule, MdSliderModule, MdSlideToggleModule, MdSnackBarModule, MdTabsModule,
49+
MdToolbarModule, MdTooltipModule
4850
} from '@angular/material';
4951

5052
/**
@@ -60,6 +62,7 @@ import {
6062
MdChipsModule,
6163
MdDatepickerModule,
6264
MdDialogModule,
65+
MdExpansionModule,
6366
MdGridListModule,
6467
MdIconModule,
6568
MdInputModule,
@@ -140,6 +143,7 @@ export class DemoMaterialModule {}
140143
RainyTabContent,
141144
FoggyTabContent,
142145
PlatformDemo,
146+
ExpansionDemo,
143147
],
144148
providers: [
145149
{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
@@ -42,6 +42,7 @@ export class DemoApp {
4242
{name: 'Checkbox', route: 'checkbox'},
4343
{name: 'Datepicker', route: 'datepicker'},
4444
{name: 'Dialog', route: 'dialog'},
45+
{name: 'Expansion Panel', route: 'expansion'},
4546
{name: 'Gestures', route: 'gestures'},
4647
{name: 'Grid List', route: 'grid-list'},
4748
{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
@@ -33,6 +33,7 @@ import {AutocompleteDemo} from '../autocomplete/autocomplete-demo';
3333
import {InputDemo} from '../input/input-demo';
3434
import {StyleDemo} from '../style/style-demo';
3535
import {DatepickerDemo} from '../datepicker/datepicker-demo';
36+
import {ExpansionDemo} from '../expansion/expansion-demo';
3637

3738
export const DEMO_APP_ROUTES: Routes = [
3839
{path: '', component: Home},
@@ -68,4 +69,5 @@ export const DEMO_APP_ROUTES: Routes = [
6869
{path: 'snack-bar', component: SnackBarDemo},
6970
{path: 'platform', component: PlatformDemo},
7071
{path: 'style', component: StyleDemo},
72+
{path: 'expansion', component: ExpansionDemo},
7173
];
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<h1>Single Expansion Panel</h1>
2+
<md-expansion-panel class="md-expansion-demo-width" #myPanel>
3+
<md-expansion-panel-header>
4+
<mat-panel-description>This is a panel description.</mat-panel-description>
5+
<mat-panel-title>Panel Title</mat-panel-title>
6+
</md-expansion-panel-header>
7+
This is the content text that makes sense here.
8+
<div md-action-row>
9+
<button md-button (click)="myPanel.expanded = false">CANCEL</button>
10+
<button md-button>SAVE</button>
11+
</div>
12+
</md-expansion-panel>
13+
<br>
14+
<h1>Accordion</h1>
15+
<div>
16+
<p>Accordion Options</p>
17+
<div>
18+
<md-slide-toggle [(ngModel)]="multi">Allow Multi Expansion</md-slide-toggle>
19+
<md-slide-toggle [(ngModel)]="hideToggle">Hide Indicators</md-slide-toggle>
20+
<md-slide-toggle [(ngModel)]="showPanel3">Show Panel 3</md-slide-toggle>
21+
</div>
22+
<p>Accordion Style</p>
23+
<md-radio-group [(ngModel)]="displayMode">
24+
<md-radio-button value="default">Default</md-radio-button>
25+
<md-radio-button value="flat">Flat</md-radio-button>
26+
</md-radio-group>
27+
<p>Accordion Panel(s)</p>
28+
<div>
29+
<md-checkbox [(ngModel)]="panel1.expanded">Panel 1</md-checkbox>
30+
<md-checkbox [(ngModel)]="panel2.expanded">Panel 2</md-checkbox>
31+
</div>
32+
</div>
33+
<br>
34+
<div cdk-accordion [displayMode]="displayMode" [multi]="multi"
35+
class="md-expansion-demo-width">
36+
<md-expansion-panel #panel1 [hideToggle]="hideToggle">
37+
<md-expansion-panel-header>Section 1</md-expansion-panel-header>
38+
<p>This is the content text that makes sense here.</p>
39+
</md-expansion-panel>
40+
<md-expansion-panel #panel2 [hideToggle]="hideToggle">
41+
<md-expansion-panel-header>Section 2</md-expansion-panel-header>
42+
<p>This is the content text that makes sense here.</p>
43+
</md-expansion-panel>
44+
<md-expansion-panel #panel3 *ngIf="showPanel3" [hideToggle]="hideToggle">
45+
<md-expansion-panel-header>Section 3</md-expansion-panel-header>
46+
<md-checkbox #showButtons>Reveal Buttons Below</md-checkbox>
47+
<div md-action-row *ngIf="showButtons.checked">
48+
<button md-button (click)="panel2.expanded = true">OPEN SECTION 2</button>
49+
<button md-button (click)="panel3.expanded = false">CLOSE</button>
50+
</div>
51+
</md-expansion-panel>
52+
</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+
displayMode: string = 'default';
12+
multi: boolean = false;
13+
hideToggle: boolean = false;
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
@@ -8,6 +8,7 @@
88
@import '../../chips/chips-theme';
99
@import '../../datepicker/datepicker-theme';
1010
@import '../../dialog/dialog-theme';
11+
@import '../../expansion/expansion-theme';
1112
@import '../../grid-list/grid-list-theme';
1213
@import '../../icon/icon-theme';
1314
@import '../../input/input-theme';
@@ -36,6 +37,7 @@
3637
@include mat-chips-theme($theme);
3738
@include mat-datepicker-theme($theme);
3839
@include mat-dialog-theme($theme);
40+
@include mat-expansion-panel-theme($theme);
3941
@include mat-grid-list-theme($theme);
4042
@include mat-icon-theme($theme);
4143
@include mat-input-theme($theme);
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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+
13+
[mat-action-row],
14+
[md-action-row] {
15+
border-top-color: mat-color($foreground, divider);
16+
}
17+
18+
.mat-expansion-panel-header {
19+
&:focus,
20+
&:hover {
21+
background: mat-color($background, hover);
22+
}
23+
24+
.mat-expansion-panel-header-title {
25+
color: mat-color($foreground, text);
26+
}
27+
28+
.mat-expansion-panel-header-description {
29+
color: mat-color($foreground, secondary-text);
30+
}
31+
32+
.mat-expansion-indicator::after {
33+
color: mat-color($foreground, secondary-text);
34+
}
35+
}
36+
}

src/lib/expansion/accordion-item.ts

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import {Output, EventEmitter, Input, OnDestroy, Optional, Host} from '@angular/core';
2+
import {UniqueSelectionDispatcher} from '../core';
3+
import {CdkAccordion} from './accordion';
4+
5+
/** Used to generate unique ID for each expansion panel. */
6+
let nextId = 0;
7+
8+
/**
9+
* An abstract class to be extended and decorated as a component. Sets up all
10+
* events and attributes needed to be managed by a CdkAccordion parent.
11+
*/
12+
export abstract class CdkAccordionItem implements OnDestroy {
13+
/** Event emitted every time the MdAccordianChild is closed. */
14+
@Output() closed = new EventEmitter<void>();
15+
/** Event emitted every time the MdAccordianChild is opened. */
16+
@Output() opened = new EventEmitter<void>();
17+
/** Event emitted when the MdAccordianChild is destroyed. */
18+
@Output() destroyed = new EventEmitter<void>();
19+
/** The unique MdAccordianChild id. */
20+
readonly id = `cdk-accordion-child-${nextId++}`;
21+
/** Whether the MdAccordianChild is expanded. */
22+
@Input() get expanded(): boolean { return this._expanded; }
23+
set expanded(expanded: boolean) {
24+
// Only emit events and update the internal value if the value changes.
25+
if (this._expanded !== expanded) {
26+
this._expanded = expanded;
27+
if (expanded) {
28+
this.opened.emit();
29+
/**
30+
* In the unique selection dispatcher, the id parameter is the id of the CdkAccordonItem,
31+
* the name value is the id of the accordion.
32+
*/
33+
let accordionId = this.accordion ? this.accordion.id : this.id;
34+
this._uniqueSelectionDispatcher.notify(this.id, accordionId);
35+
} else {
36+
this.closed.emit();
37+
}
38+
}
39+
}
40+
private _expanded: boolean;
41+
42+
constructor(@Optional() @Host() public accordion: CdkAccordion,
43+
protected _uniqueSelectionDispatcher: UniqueSelectionDispatcher) {
44+
_uniqueSelectionDispatcher.listen((id: string, accordionId: string) => {
45+
if (this.accordion && !this.accordion.multi &&
46+
this.accordion.id === accordionId && this.id !== id) {
47+
this.expanded = false;
48+
}
49+
});
50+
}
51+
52+
/** Emits an event for the accordion child being destroyed. */
53+
ngOnDestroy() {
54+
this.destroyed.emit();
55+
}
56+
}

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('CdkAccordion', () => {
8+
beforeEach(async(() => {
9+
TestBed.configureTestingModule({
10+
imports: [MdExpansionModule],
11+
declarations: [
12+
SetOfItems
13+
],
14+
});
15+
TestBed.compileComponents();
16+
}));
17+
18+
it('should ensure only one item is expanded at a time', () => {
19+
let fixture = TestBed.createComponent(SetOfItems);
20+
let items = fixture.debugElement.queryAll(By.css('.mat-expansion-panel'));
21+
22+
fixture.componentInstance.firstPanelExpanded = true;
23+
fixture.detectChanges();
24+
expect(items[0].classes['mat-expanded']).toBeTruthy();
25+
expect(items[1].classes['mat-expanded']).toBeFalsy();
26+
27+
fixture.componentInstance.secondPanelExpanded = true;
28+
fixture.detectChanges();
29+
expect(items[0].classes['mat-expanded']).toBeFalsy();
30+
expect(items[1].classes['mat-expanded']).toBeTruthy();
31+
});
32+
33+
it('should allow multiple items to be expanded simultaneously', () => {
34+
let fixture = TestBed.createComponent(SetOfItems);
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 cdk-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 SetOfItems {
59+
multi: boolean = false;
60+
firstPanelExpanded: boolean = false;
61+
secondPanelExpanded: boolean = false;
62+
}

src/lib/expansion/accordion.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import {Directive, Input} from '@angular/core';
2+
import {
3+
trigger,
4+
state,
5+
style,
6+
transition,
7+
animate,
8+
} from '@angular/animations';
9+
import {coerceBooleanProperty} from '../core/coercion/boolean-property';
10+
import 'rxjs/add/operator/takeUntil';
11+
12+
export type CdkAccordionDisplayMode = 'default' | 'flat';
13+
14+
/** Unique ID counter */
15+
let nextId = 0;
16+
17+
/**
18+
* Directive whose purpose is to manage the expanded state of CdkAccordionItem children.
19+
*/
20+
@Directive({
21+
selector: '[cdk-accordion]',
22+
})
23+
export class CdkAccordion {
24+
/** A readonly id value to use for unique selection coordination. */
25+
readonly id = `cdk-accordion-${nextId++}`;
26+
27+
/** Whether the expansion indicator should be hidden. */
28+
@Input() get hideToggle(): boolean { return this._hideToggle; }
29+
set hideToggle(show: boolean) { this._hideToggle = coerceBooleanProperty(show); }
30+
private _hideToggle: boolean = false;
31+
32+
/** Whether the panel set should use flat styling. */
33+
@Input() displayMode: CdkAccordionDisplayMode = 'default';
34+
35+
/** Whether the panel set should allow multiple open panels. */
36+
@Input() get multi(): boolean { return this._multi; }
37+
set multi(multi: boolean) { this._multi = coerceBooleanProperty(multi); }
38+
private _multi: boolean = false;
39+
}
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="md-panel-title, mat-panel-title"></ng-content>
3+
<ng-content select="md-panel-description, mat-panel-description"></ng-content>
4+
<ng-content></ng-content>
5+
</span>
6+
<span [@indicatorRotate]="_getExpandedState()" *ngIf="!_getHideToggle()"
7+
class="mat-expansion-indicator"></span>

0 commit comments

Comments
 (0)