Skip to content

Commit

Permalink
chore: add a mixin for disabled and apply it to md-button
Browse files Browse the repository at this point in the history
  • Loading branch information
jelbourn committed Apr 19, 2017
1 parent 8d0cd04 commit 5200efd
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 9 deletions.
21 changes: 12 additions & 9 deletions src/lib/button/button.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
ViewEncapsulation
} from '@angular/core';
import {coerceBooleanProperty, FocusOriginMonitor} from '../core';
import {mixinDisabled, Disabled} from '../core/common-behaviors';


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


// Boilerplate for applying mixins to MdButton.
export class MdButtonBase { }
export const _MdButtonMixinBase = mixinDisabled(MdButtonBase);


/**
* Material design button.
*/
Expand All @@ -89,14 +95,15 @@ export class MdMiniFabCssMatStyler {}
'button[mat-button], button[mat-raised-button], button[mat-icon-button],' +
'button[mat-fab], button[mat-mini-fab]',
host: {
'[disabled]': 'disabled',
'[disabled]': 'disabled || null',
},
inputs: ['disabled'],
templateUrl: 'button.html',
styleUrls: ['button.css'],
encapsulation: ViewEncapsulation.None,
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class MdButton implements OnDestroy {
export class MdButton extends _MdButtonMixinBase implements OnDestroy, Disabled {
private _color: string;

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

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

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

/** Whether the button is disabled. */
@Input()
get disabled() { return this._disabled; }
set disabled(value: boolean) { this._disabled = coerceBooleanProperty(value) ? true : null; }

constructor(private _elementRef: ElementRef, private _renderer: Renderer,
private _focusOriginMonitor: FocusOriginMonitor) {
super();
this._focusOriginMonitor.monitor(this._elementRef.nativeElement, this._renderer, true);
}

Expand Down Expand Up @@ -179,10 +181,11 @@ export class MdButton implements OnDestroy {
selector: `a[md-button], a[md-raised-button], a[md-icon-button], a[md-fab], a[md-mini-fab],
a[mat-button], a[mat-raised-button], a[mat-icon-button], a[mat-fab], a[mat-mini-fab]`,
host: {
'[attr.disabled]': 'disabled',
'[attr.disabled]': 'disabled || null',
'[attr.aria-disabled]': '_isAriaDisabled',
'(click)': '_haltDisabledEvents($event)',
},
inputs: ['disabled'],
templateUrl: 'button.html',
styleUrls: ['button.css'],
encapsulation: ViewEncapsulation.None
Expand Down
18 changes: 18 additions & 0 deletions src/lib/core/common-behaviors/disabled.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import {mixinDisabled} from './disabled';


describe('MixinDisabled', () => {
it('should augment an existing class with a disabled property', () => {
class EmptyClass { }

let classWithDisabled = mixinDisabled(EmptyClass);
let instance = new classWithDisabled();

expect(instance.disabled)
.toBe(false, 'Expected the mixed-into class to have a disabled property');

instance.disabled = true;
expect(instance.disabled)
.toBe(true, 'Expected the mixed-into class to have an updated disabled property');
});
});
22 changes: 22 additions & 0 deletions src/lib/core/common-behaviors/disabled.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import {coerceBooleanProperty} from '../coercion/boolean-property';


/** @docs-private */
export type Constructor<T> = new(...args: any[]) => T;

/** @docs-private */
export interface Disabled {
disabled: boolean;
}

/** Mixin to augment a directive with a `disabled` property. */
export function mixinDisabled<T extends Constructor<{}>>(base: T): Constructor<Disabled> & T {
return class extends base {
private _disabled: boolean = false;

get disabled() { return this._disabled; }
set disabled(value: any) { this._disabled = coerceBooleanProperty(value); }

constructor(...args: any[]) { super(...args); }
};
}
1 change: 1 addition & 0 deletions src/lib/core/common-behaviors/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export {Disabled, mixinDisabled} from './disabled';

0 comments on commit 5200efd

Please sign in to comment.