Skip to content

chore(accordion): move CdkAccordion to @angular/cdk #7530

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 12, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
155 changes: 155 additions & 0 deletions src/cdk/accordion/accordion-item.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
import {async, TestBed, ComponentFixture} from '@angular/core/testing';
import {Component} from '@angular/core';
import {By} from '@angular/platform-browser';
import {BrowserAnimationsModule} from '@angular/platform-browser/animations';
import {CdkAccordionModule, CdkAccordionItem} from './public-api';

describe('CdkAccordionItem', () => {
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [
BrowserAnimationsModule,
CdkAccordionModule
],
declarations: [
SingleItem,
ItemGroupWithoutAccordion,
ItemGroupWithAccordion
],
});
TestBed.compileComponents();
}));

describe('single item', () => {
let fixture: ComponentFixture<SingleItem>;
let item: CdkAccordionItem;

beforeEach(() => {
fixture = TestBed.createComponent(SingleItem);
item = fixture.debugElement
.query(By.directive(CdkAccordionItem))
.injector.get(CdkAccordionItem);
});

it('should toggle its expanded state', () => {
expect(item.expanded).toBeFalsy();
item.toggle();
expect(item.expanded).toBeTruthy();
item.toggle();
expect(item.expanded).toBeFalsy();
});

it('should set its expanded state to expanded', () => {
item.expanded = false;
item.open();
expect(item.expanded).toBeTruthy();
});

it('should set its expanded state to closed', () => {
item.expanded = true;
item.close();
expect(item.expanded).toBeFalsy();
});

it('should emit a closed event', () => {
spyOn(item.closed, 'emit');
item.close();
fixture.detectChanges();
expect(item.closed.emit).toHaveBeenCalledWith();
});

it('should emit an opened event', () => {
spyOn(item.opened, 'emit');
item.open();
fixture.detectChanges();
expect(item.opened.emit).toHaveBeenCalledWith();
});

it('should emit an destroyed event', () => {
spyOn(item.destroyed, 'emit');
item.ngOnDestroy();
fixture.detectChanges();
expect(item.destroyed.emit).toHaveBeenCalledWith();
});
});

describe('items without accordion', () => {
let fixture: ComponentFixture<SingleItem>;
let firstItem: CdkAccordionItem;
let secondItem: CdkAccordionItem;

beforeEach(() => {
fixture = TestBed.createComponent(ItemGroupWithoutAccordion);
[firstItem, secondItem] = fixture.debugElement
.queryAll(By.directive(CdkAccordionItem))
.map(el => {
return el.injector.get(CdkAccordionItem) as CdkAccordionItem;
});
});

it('should not change expanded state based on unrelated items', () => {
expect(firstItem.expanded).toBeFalsy();
expect(secondItem.expanded).toBeFalsy();
firstItem.open();
fixture.detectChanges();
expect(firstItem.expanded).toBeTruthy();
expect(secondItem.expanded).toBeFalsy();
secondItem.open();
fixture.detectChanges();
expect(firstItem.expanded).toBeTruthy();
expect(secondItem.expanded).toBeTruthy();
});
});


describe('items in accordion', () => {
let fixture: ComponentFixture<SingleItem>;
let firstItem: CdkAccordionItem;
let secondItem: CdkAccordionItem;

beforeEach(() => {
fixture = TestBed.createComponent(ItemGroupWithAccordion);
[firstItem, secondItem] = fixture.debugElement
.queryAll(By.directive(CdkAccordionItem))
.map(el => {
return el.injector.get(CdkAccordionItem) as CdkAccordionItem;
});
});

it('should change expanded state based on related items', () => {
expect(firstItem.expanded).toBeFalsy();
expect(secondItem.expanded).toBeFalsy();
firstItem.open();
fixture.detectChanges();
expect(firstItem.expanded).toBeTruthy();
expect(secondItem.expanded).toBeFalsy();
secondItem.open();
fixture.detectChanges();
expect(firstItem.expanded).toBeFalsy();
expect(secondItem.expanded).toBeTruthy();
});
});
});

@Component({
template: `<cdk-accordion-item #item1></cdk-accordion-item>`
})
class SingleItem {}

@Component({
template: `
<cdk-accordion-item #item1></cdk-accordion-item>
<cdk-accordion-item #item2></cdk-accordion-item>
`
})
class ItemGroupWithoutAccordion {}

@Component({
template: `
<cdk-accordion>
<cdk-accordion-item #item1></cdk-accordion-item>
<cdk-accordion-item #item2></cdk-accordion-item>
</cdk-accordion>
`
})
class ItemGroupWithAccordion {}
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,28 @@

import {
Output,
Directive,
EventEmitter,
Input,
Injectable,
OnDestroy,
Optional,
ChangeDetectorRef,
} from '@angular/core';
import {UniqueSelectionDispatcher} from '@angular/cdk/collections';
import {CdkAccordion} from './accordion';

/** Used to generate unique ID for each expansion panel. */
/** Used to generate unique ID for each accordion item. */
let nextId = 0;

/**
* An abstract class to be extended and decorated as a component. Sets up all
* An basic directive expected to be extended and decorated as a component. Sets up all
* events and attributes needed to be managed by a CdkAccordion parent.
*/
@Injectable()
export class AccordionItem implements OnDestroy {
@Directive({
selector: 'cdk-accordion-item',
exportAs: 'cdkAccordionItem',
})
export class CdkAccordionItem implements OnDestroy {
/** Event emitted every time the AccordionItem is closed. */
@Output() closed = new EventEmitter<void>();
/** Event emitted every time the AccordionItem is opened. */
Expand Down Expand Up @@ -62,7 +65,7 @@ export class AccordionItem implements OnDestroy {
}
private _expanded: boolean;

/** Unregister function for _expansionDispatcher **/
/** Unregister function for _expansionDispatcher. */
private _removeUniqueSelectionListener: () => void = () => {};

constructor(@Optional() public accordion: CdkAccordion,
Expand Down
19 changes: 19 additions & 0 deletions src/cdk/accordion/accordion-module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/

import {NgModule} from '@angular/core';
import {UNIQUE_SELECTION_DISPATCHER_PROVIDER} from '@angular/cdk/collections';
import {CdkAccordion} from './accordion';
import {CdkAccordionItem} from './accordion-item';

@NgModule({
exports: [CdkAccordion, CdkAccordionItem],
declarations: [CdkAccordion, CdkAccordionItem],
providers: [UNIQUE_SELECTION_DISPATCHER_PROVIDER],
})
export class CdkAccordionModule {}
67 changes: 67 additions & 0 deletions src/cdk/accordion/accordion.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import {async, TestBed} from '@angular/core/testing';
import {Component, ViewChild} from '@angular/core';
import {By} from '@angular/platform-browser';
import {BrowserAnimationsModule} from '@angular/platform-browser/animations';
import {CdkAccordionModule, CdkAccordionItem} from './public-api';

describe('CdkAccordion', () => {
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [
BrowserAnimationsModule,
CdkAccordionModule
],
declarations: [
SetOfItems
],
});
TestBed.compileComponents();
}));

it('should ensure only one item is expanded at a time', () => {
const fixture = TestBed.createComponent(SetOfItems);
const [firstPanel, secondPanel] = fixture.debugElement
.queryAll(By.directive(CdkAccordionItem))
.map(el => {
return el.injector.get(CdkAccordionItem) as CdkAccordionItem;
});

firstPanel.open();
fixture.detectChanges();
expect(firstPanel.expanded).toBeTruthy();
expect(secondPanel.expanded).toBeFalsy();

secondPanel.open();
fixture.detectChanges();
expect(firstPanel.expanded).toBeFalsy();
expect(secondPanel.expanded).toBeTruthy();
});

it('should allow multiple items to be expanded simultaneously', () => {
const fixture = TestBed.createComponent(SetOfItems);
const [firstPanel, secondPanel] = fixture.debugElement
.queryAll(By.directive(CdkAccordionItem))
.map(el => {
return el.injector.get(CdkAccordionItem) as CdkAccordionItem;
});

fixture.componentInstance.multi = true;
fixture.detectChanges();
firstPanel.expanded = true;
secondPanel.expanded = true;
fixture.detectChanges();
expect(firstPanel.expanded).toBeTruthy();
expect(secondPanel.expanded).toBeTruthy();
});
});

@Component({template: `
<cdk-accordion [multi]="multi">
<cdk-accordion-item #item1></cdk-accordion-item>
<cdk-accordion-item #item2></cdk-accordion-item>
</cdk-accordion>`})
class SetOfItems {
@ViewChild('item1') item1;
@ViewChild('item2') item2;
multi: boolean = false;
}
30 changes: 30 additions & 0 deletions src/cdk/accordion/accordion.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/

import {Directive, Input} from '@angular/core';
import {coerceBooleanProperty} from '@angular/cdk/coercion';

/** Used to generate unique ID for each accordion. */
let nextId = 0;

/**
* Directive whose purpose is to manage the expanded state of CdkAccordionItem children.
*/
@Directive({
selector: 'cdk-accordion, [cdk-accordion]',
exportAs: 'cdkAccordion',
})
export class CdkAccordion {
/** A readonly id value to use for unique selection coordination. */
readonly id = `cdk-accordion-${nextId++}`;

/** Whether the accordion should allow multiple expanded accordion items simulateously. */
@Input() get multi(): boolean { return this._multi; }
set multi(multi: boolean) { this._multi = coerceBooleanProperty(multi); }
private _multi: boolean = false;
}
9 changes: 9 additions & 0 deletions src/cdk/accordion/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/

export * from './public-api';
11 changes: 11 additions & 0 deletions src/cdk/accordion/public-api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/

export {CdkAccordionItem} from './accordion-item';
export {CdkAccordion} from './accordion';
export * from './accordion-module';
13 changes: 13 additions & 0 deletions src/cdk/accordion/tsconfig-build.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"extends": "../tsconfig-build",
"files": [
"public-api.ts"
],
"angularCompilerOptions": {
"annotateForClosureCompiler": true,
"strictMetadataEmit": true,
"flatModuleOutFile": "index.js",
"flatModuleId": "@angular/cdk/accordion",
"skipTemplateCodegen": true
}
}
13 changes: 13 additions & 0 deletions src/cdk/accordion/tsconfig-es5.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"extends": "../tsconfig-es5",
"files": [
"public-api.ts"
],
"angularCompilerOptions": {
"annotateForClosureCompiler": true,
"strictMetadataEmit": true,
"flatModuleOutFile": "index.js",
"flatModuleId": "@angular/cdk/accordion",
"skipTemplateCodegen": true
}
}
2 changes: 2 additions & 0 deletions src/demo-app/demo-material-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import {
} from '@angular/material';
import {MatNativeDateModule, MatRippleModule} from '@angular/material';
import {CdkTableModule} from '@angular/cdk/table';
import {CdkAccordionModule} from '@angular/cdk/accordion';
import {A11yModule} from '@angular/cdk/a11y';
import {BidiModule} from '@angular/cdk/bidi';
import {OverlayModule} from '@angular/cdk/overlay';
Expand Down Expand Up @@ -80,6 +81,7 @@ import {PortalModule} from '@angular/cdk/portal';
CdkTableModule,
A11yModule,
BidiModule,
CdkAccordionModule,
ObserversModule,
OverlayModule,
PlatformModule,
Expand Down
Loading