Skip to content

Commit d41f2af

Browse files
committed
chore: add a mixin for disabled and apply it to md-button
1 parent 8d0cd04 commit d41f2af

File tree

4 files changed

+53
-9
lines changed

4 files changed

+53
-9
lines changed

src/lib/button/button.ts

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
ViewEncapsulation
1111
} from '@angular/core';
1212
import {coerceBooleanProperty, FocusOriginMonitor} from '../core';
13+
import {mixinDisabled, CanDisable} from '../core/common-behaviors/disabled';
1314

1415

1516
// TODO(kara): Convert attribute selectors to classes when attr maps become available
@@ -79,6 +80,11 @@ export class MdFabCssMatStyler {}
7980
export class MdMiniFabCssMatStyler {}
8081

8182

83+
// Boilerplate for applying mixins to MdButton.
84+
export class MdButtonBase { }
85+
export const _MdButtonMixinBase = mixinDisabled(MdButtonBase);
86+
87+
8288
/**
8389
* Material design button.
8490
*/
@@ -89,14 +95,15 @@ export class MdMiniFabCssMatStyler {}
8995
'button[mat-button], button[mat-raised-button], button[mat-icon-button],' +
9096
'button[mat-fab], button[mat-mini-fab]',
9197
host: {
92-
'[disabled]': 'disabled',
98+
'[disabled]': 'disabled || null',
9399
},
100+
inputs: ['disabled'],
94101
templateUrl: 'button.html',
95102
styleUrls: ['button.css'],
96103
encapsulation: ViewEncapsulation.None,
97104
changeDetection: ChangeDetectionStrategy.OnPush,
98105
})
99-
export class MdButton implements OnDestroy {
106+
export class MdButton extends _MdButtonMixinBase implements OnDestroy, CanDisable {
100107
private _color: string;
101108

102109
/** Whether the button is round. */
@@ -107,20 +114,15 @@ export class MdButton implements OnDestroy {
107114

108115
/** Whether the ripple effect on click should be disabled. */
109116
private _disableRipple: boolean = false;
110-
private _disabled: boolean = null;
111117

112118
/** Whether the ripple effect for this button is disabled. */
113119
@Input()
114120
get disableRipple() { return this._disableRipple; }
115121
set disableRipple(v) { this._disableRipple = coerceBooleanProperty(v); }
116122

117-
/** Whether the button is disabled. */
118-
@Input()
119-
get disabled() { return this._disabled; }
120-
set disabled(value: boolean) { this._disabled = coerceBooleanProperty(value) ? true : null; }
121-
122123
constructor(private _elementRef: ElementRef, private _renderer: Renderer,
123124
private _focusOriginMonitor: FocusOriginMonitor) {
125+
super();
124126
this._focusOriginMonitor.monitor(this._elementRef.nativeElement, this._renderer, true);
125127
}
126128

@@ -179,10 +181,11 @@ export class MdButton implements OnDestroy {
179181
selector: `a[md-button], a[md-raised-button], a[md-icon-button], a[md-fab], a[md-mini-fab],
180182
a[mat-button], a[mat-raised-button], a[mat-icon-button], a[mat-fab], a[mat-mini-fab]`,
181183
host: {
182-
'[attr.disabled]': 'disabled',
184+
'[attr.disabled]': 'disabled || null',
183185
'[attr.aria-disabled]': '_isAriaDisabled',
184186
'(click)': '_haltDisabledEvents($event)',
185187
},
188+
inputs: ['disabled'],
186189
templateUrl: 'button.html',
187190
styleUrls: ['button.css'],
188191
encapsulation: ViewEncapsulation.None
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import {mixinDisabled} from './disabled';
2+
3+
4+
describe('MixinDisabled', () => {
5+
it('should augment an existing class with a disabled property', () => {
6+
class EmptyClass { }
7+
8+
let classWithDisabled = mixinDisabled(EmptyClass);
9+
let instance = new classWithDisabled();
10+
11+
expect(instance.disabled)
12+
.toBe(false, 'Expected the mixed-into class to have a disabled property');
13+
14+
instance.disabled = true;
15+
expect(instance.disabled)
16+
.toBe(true, 'Expected the mixed-into class to have an updated disabled property');
17+
});
18+
});
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import {coerceBooleanProperty} from '../coercion/boolean-property';
2+
3+
4+
/** @docs-private */
5+
export type Constructor<T> = new(...args: any[]) => T;
6+
7+
/** @docs-private */
8+
export interface CanDisable {
9+
disabled: boolean;
10+
}
11+
12+
/** Mixin to augment a directive with a `disabled` property. */
13+
export function mixinDisabled<T extends Constructor<{}>>(base: T): Constructor<CanDisable> & T {
14+
return class extends base {
15+
private _disabled: boolean = false;
16+
17+
get disabled() { return this._disabled; }
18+
set disabled(value: any) { this._disabled = coerceBooleanProperty(value); }
19+
20+
constructor(...args: any[]) { super(...args); }
21+
};
22+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export {CanDisable, mixinDisabled} from './disabled';

0 commit comments

Comments
 (0)