Skip to content

Commit e94a7ab

Browse files
author
Louw Swart
committed
chore(accordion): re-create CanDisable and mixinDisabled in CDK (#8141)
Re-creates the disabled mixin in the CDK to expose the disabled property to Accordion Item Closes #8141
1 parent cf11ff2 commit e94a7ab

17 files changed

+124
-31
lines changed

.github/CODEOWNERS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
/src/cdk/collections/** @jelbourn @crisbeto @andrewseguin
6060
/src/cdk/keycodes/** @jelbourn
6161
/src/cdk/layout/** @josephperrott
62+
/src/cdk/mixins/** @josephperrott @jelbourn
6263
/src/cdk/observers/** @jelbourn @crisbeto
6364
/src/cdk/overlay/** @jelbourn @crisbeto
6465
/src/cdk/platform/** @jelbourn @devversion

src/cdk/accordion/accordion-item.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,20 @@ import {
1616
ChangeDetectorRef,
1717
} from '@angular/core';
1818
import {UniqueSelectionDispatcher} from '@angular/cdk/collections';
19+
import {CanDisable, mixinDisabled} from '@angular/cdk/mixins';
1920
import {CdkAccordion} from './accordion';
2021

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

25+
// Boilerplate for applying mixins to CdkAccordionItem.
26+
/** @docs-private */
27+
export class CdkAccordionItemBase {
28+
constructor() {}
29+
}
30+
31+
export const _CdkAccordionItemBase = mixinDisabled(CdkAccordionItemBase);
32+
2433
/**
2534
* An basic directive expected to be extended and decorated as a component. Sets up all
2635
* events and attributes needed to be managed by a CdkAccordion parent.
@@ -29,7 +38,8 @@ let nextId = 0;
2938
selector: 'cdk-accordion-item',
3039
exportAs: 'cdkAccordionItem',
3140
})
32-
export class CdkAccordionItem implements OnDestroy {
41+
export class CdkAccordionItem extends _CdkAccordionItemBase
42+
implements OnDestroy, CanDisable {
3343
/** Event emitted every time the AccordionItem is closed. */
3444
@Output() closed = new EventEmitter<void>();
3545
/** Event emitted every time the AccordionItem is opened. */
@@ -71,6 +81,7 @@ export class CdkAccordionItem implements OnDestroy {
7181
constructor(@Optional() public accordion: CdkAccordion,
7282
private _changeDetectorRef: ChangeDetectorRef,
7383
protected _expansionDispatcher: UniqueSelectionDispatcher) {
84+
super();
7485
this._removeUniqueSelectionListener =
7586
_expansionDispatcher.listen((id: string, accordionId: string) => {
7687
if (this.accordion && !this.accordion.multi &&

src/cdk/accordion/accordion.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export class CdkAccordion {
2323
/** A readonly id value to use for unique selection coordination. */
2424
readonly id = `cdk-accordion-${nextId++}`;
2525

26-
/** Whether the accordion should allow multiple expanded accordion items simulateously. */
26+
/** Whether the accordion should allow multiple expanded accordion items simultaneously. */
2727
@Input() get multi(): boolean { return this._multi; }
2828
set multi(multi: boolean) { this._multi = coerceBooleanProperty(multi); }
2929
private _multi: boolean = false;

src/cdk/accordion/public-api.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
export {CdkAccordionItem} from './accordion-item';
9+
export {CdkAccordionItem} from './accordion-item';
1010
export {CdkAccordion} from './accordion';
1111
export * from './accordion-module';

src/cdk/mixins/constructor.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
9+
/** @docs-private */
10+
export type Constructor<T> = new(...args: any[]) => T;
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import {mixinDisabled} from './disabled';
2+
3+
describe('MixinDisabled', () => {
4+
it('should augment an existing class with a disabled property', () => {
5+
class EmptyClass { }
6+
7+
let classWithDisabled = mixinDisabled(EmptyClass);
8+
let instance = new classWithDisabled();
9+
10+
expect(instance.disabled)
11+
.toBe(false, 'Expected the mixed-into class to have a disabled property');
12+
13+
instance.disabled = true;
14+
expect(instance.disabled)
15+
.toBe(true, 'Expected the mixed-into class to have an updated disabled property');
16+
});
17+
});

src/cdk/mixins/disabled/disabled.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
9+
import {coerceBooleanProperty} from '@angular/cdk/coercion';
10+
import {Constructor} from '../constructor';
11+
12+
/** @docs-private */
13+
export interface CanDisable {
14+
disabled: boolean;
15+
}
16+
17+
/** Mixin to augment a directive with a `disabled` property. */
18+
export function mixinDisabled<T extends Constructor<{}>>(base: T): Constructor<CanDisable> & T {
19+
return class extends base {
20+
private _disabled: boolean = false;
21+
22+
get disabled() { return this._disabled; }
23+
set disabled(value: any) { this._disabled = coerceBooleanProperty(value); }
24+
25+
constructor(...args: any[]) { super(...args); }
26+
};
27+
}

src/cdk/mixins/index.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
9+
export * from './public-api';

src/cdk/mixins/mixins.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
### Mixins
2+
3+
Mixins that can augment directives when certain properties are present.
4+
5+
#### disabled
6+
Augments a directive with a `disabled` property.

src/cdk/mixins/public-api.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
9+
export * from './disabled/disabled';

src/cdk/mixins/tsconfig-build.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"extends": "../tsconfig-build",
3+
"files": [
4+
"public-api.ts"
5+
],
6+
"angularCompilerOptions": {
7+
"annotateForClosureCompiler": true,
8+
"strictMetadataEmit": true,
9+
"flatModuleOutFile": "index.js",
10+
"flatModuleId": "@angular/cdk/mixins",
11+
"skipTemplateCodegen": true
12+
}
13+
}

src/cdk/mixins/tsconfig-es5.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"extends": "../tsconfig-es5",
3+
"files": [
4+
"public-api.ts"
5+
],
6+
"angularCompilerOptions": {
7+
"annotateForClosureCompiler": true,
8+
"strictMetadataEmit": true,
9+
"flatModuleOutFile": "index.js",
10+
"flatModuleId": "@angular/cdk/mixins",
11+
"skipTemplateCodegen": true
12+
}
13+
}

src/demo-app/system-config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ System.config({
3939
'@angular/cdk/collections': 'dist/packages/cdk/collections/index.js',
4040
'@angular/cdk/keycodes': 'dist/packages/cdk/keycodes/index.js',
4141
'@angular/cdk/layout': 'dist/packages/cdk/layout/index.js',
42+
'@angular/cdk/mixins': 'dist/packages/cdk/mixins/index.js',
4243
'@angular/cdk/observers': 'dist/packages/cdk/observers/index.js',
4344
'@angular/cdk/overlay': 'dist/packages/cdk/overlay/index.js',
4445
'@angular/cdk/platform': 'dist/packages/cdk/platform/index.js',

src/e2e-app/system-config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ System.config({
3737
'@angular/cdk/collections': 'dist/bundles/cdk-collections.umd.js',
3838
'@angular/cdk/keycodes': 'dist/bundles/cdk-keycodes.umd.js',
3939
'@angular/cdk/layout': 'dist/bundles/cdk-layout.umd.js',
40+
'@angular/cdk/mixins': 'dist/bundles/cdk-mixins.umd.js',
4041
'@angular/cdk/observers': 'dist/bundles/cdk-observers.umd.js',
4142
'@angular/cdk/overlay': 'dist/bundles/cdk-overlay.umd.js',
4243
'@angular/cdk/platform': 'dist/bundles/cdk-platform.umd.js',

src/lib/expansion/expansion-module.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ import {A11yModule} from '@angular/cdk/a11y';
1414
import {MatAccordion} from './accordion';
1515
import {
1616
MatExpansionPanel,
17-
MatExpansionPanelActionRow,
18-
MatExpansionPanelBase
17+
MatExpansionPanelActionRow
1918
} from './expansion-panel';
2019
import {
2120
MatExpansionPanelDescription,
@@ -35,7 +34,6 @@ import {
3534
MatExpansionPanelDescription
3635
],
3736
declarations: [
38-
MatExpansionPanelBase,
3937
MatAccordion,
4038
MatExpansionPanel,
4139
MatExpansionPanelActionRow,

src/lib/expansion/expansion-panel.ts

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import {
1212
ChangeDetectorRef,
1313
Component,
1414
Directive,
15-
forwardRef,
1615
Host,
1716
Input,
1817
OnChanges,
@@ -23,7 +22,6 @@ import {
2322
} from '@angular/core';
2423
import {CdkAccordionItem} from '@angular/cdk/accordion';
2524
import {UniqueSelectionDispatcher} from '@angular/cdk/collections';
26-
import {CanDisable, mixinDisabled} from '@angular/material/core';
2725
import {Subject} from 'rxjs/Subject';
2826
import {MatAccordion} from './accordion';
2927

@@ -33,24 +31,6 @@ export const _CdkAccordionItem = CdkAccordionItem;
3331
/** Time and timing curve for expansion panel animations. */
3432
export const EXPANSION_PANEL_ANIMATION_TIMING = '225ms cubic-bezier(0.4,0.0,0.2,1)';
3533

36-
// Boilerplate for applying mixins to MatExpansionPanel.
37-
/** @docs-private */
38-
@Component({
39-
template: '',
40-
moduleId: module.id,
41-
encapsulation: ViewEncapsulation.None,
42-
preserveWhitespaces: false,
43-
changeDetection: ChangeDetectionStrategy.OnPush,
44-
})
45-
export class MatExpansionPanelBase extends _CdkAccordionItem {
46-
constructor(accordion: MatAccordion,
47-
_changeDetectorRef: ChangeDetectorRef,
48-
_uniqueSelectionDispatcher: UniqueSelectionDispatcher) {
49-
super(accordion, _changeDetectorRef, _uniqueSelectionDispatcher);
50-
}
51-
}
52-
export const _MatExpansionPanelMixinBase = mixinDisabled(MatExpansionPanelBase);
53-
5434
/** MatExpansionPanel's states. */
5535
export type MatExpansionPanelState = 'expanded' | 'collapsed';
5636

@@ -77,9 +57,6 @@ export type MatExpansionPanelState = 'expanded' | 'collapsed';
7757
'[class.mat-expanded]': 'expanded',
7858
'[class.mat-expansion-panel-spacing]': '_hasSpacing()',
7959
},
80-
providers: [
81-
{provide: _MatExpansionPanelMixinBase, useExisting: forwardRef(() => MatExpansionPanel)}
82-
],
8360
animations: [
8461
trigger('bodyExpansion', [
8562
state('collapsed', style({height: '0px', visibility: 'hidden'})),
@@ -88,8 +65,7 @@ export type MatExpansionPanelState = 'expanded' | 'collapsed';
8865
]),
8966
],
9067
})
91-
export class MatExpansionPanel extends _MatExpansionPanelMixinBase
92-
implements CanDisable, OnChanges, OnDestroy {
68+
export class MatExpansionPanel extends _CdkAccordionItem implements OnChanges, OnDestroy {
9369
/** Whether the toggle indicator should be hidden. */
9470
@Input() hideToggle: boolean = false;
9571

test/karma-test-shim.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ System.config({
5858
'@angular/cdk/collections': 'dist/packages/cdk/collections/index.js',
5959
'@angular/cdk/keycodes': 'dist/packages/cdk/keycodes/index.js',
6060
'@angular/cdk/layout': 'dist/packages/cdk/layout/index.js',
61+
'@angular/cdk/mixins': 'dist/packages/cdk/mixins/index.js',
6162
'@angular/cdk/observers': 'dist/packages/cdk/observers/index.js',
6263
'@angular/cdk/overlay': 'dist/packages/cdk/overlay/index.js',
6364
'@angular/cdk/platform': 'dist/packages/cdk/platform/index.js',

0 commit comments

Comments
 (0)