Skip to content

Commit

Permalink
fix(button): apply color classes correctly.
Browse files Browse the repository at this point in the history
Fixes #75. Closes #89

Closes #195
  • Loading branch information
devversion authored and jelbourn committed Mar 18, 2016
1 parent eead805 commit 62265cc
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 11 deletions.
24 changes: 24 additions & 0 deletions src/components/button/button.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,30 @@ export function main() {
});
});

it('should should not clear previous defined classes', (done: () => void) => {
return builder.createAsync(TestApp).then((fixture) => {
let testComponent = fixture.debugElement.componentInstance;
let buttonDebugElement = fixture.debugElement.query(By.css('button'));

buttonDebugElement.nativeElement.classList.add('custom-class');

testComponent.buttonColor = 'primary';
fixture.detectChanges();

expect(buttonDebugElement.nativeElement.classList.contains('md-primary')).toBe(true);
expect(buttonDebugElement.nativeElement.classList.contains('custom-class')).toBe(true);

testComponent.buttonColor = 'accent';
fixture.detectChanges();

expect(buttonDebugElement.nativeElement.classList.contains('md-primary')).toBe(false);
expect(buttonDebugElement.nativeElement.classList.contains('md-accent')).toBe(true);
expect(buttonDebugElement.nativeElement.classList.contains('custom-class')).toBe(true);

done();
});
});

// Regular button tests
describe('button[md-button]', () => {
it('should handle a click on the button', (done: () => void) => {
Expand Down
32 changes: 28 additions & 4 deletions src/components/button/button.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import {
HostBinding,
HostListener,
ChangeDetectionStrategy,
ElementRef,
Renderer,
} from 'angular2/core';
import {TimerWrapper} from 'angular2/src/facade/async';

Expand All @@ -18,7 +20,6 @@ import {TimerWrapper} from 'angular2/src/facade/async';
inputs: ['color'],
host: {
'[class.md-button-focus]': 'isKeyboardFocused',
'[class]': 'setClassList()',
'(mousedown)': 'setMousedown()',
'(focus)': 'setKeyboardFocus()',
'(blur)': 'removeKeyboardFocus()'
Expand All @@ -29,15 +30,23 @@ import {TimerWrapper} from 'angular2/src/facade/async';
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class MdButton {
color: string;
private _color: string;

/** Whether the button has focus from the keyboard (not the mouse). Used for class binding. */
isKeyboardFocused: boolean = false;

/** Whether a mousedown has occurred on this element in the last 100ms. */
isMouseDown: boolean = false;

setClassList() { return `md-${this.color}`; }
constructor(private elementRef: ElementRef, private renderer: Renderer) { }

get color(): string {
return this._color;
}

set color(value: string) {
this._updateColor(value);
}

setMousedown() {
// We only *show* the focus style when focus has come to the button via the keyboard.
Expand All @@ -48,6 +57,18 @@ export class MdButton {
TimerWrapper.setTimeout(() => { this.isMouseDown = false; }, 100);
}

_updateColor(newColor: string) {
this._setElementColor(this._color, false);
this._setElementColor(newColor, true);
this._color = newColor;
}

_setElementColor(color: string, isAdd: boolean) {
if (color != null && color != '') {
this.renderer.setElementClass(this.elementRef.nativeElement, `md-${color}`, isAdd);
}
}

setKeyboardFocus($event: any) {
this.isKeyboardFocused = !this.isMouseDown;
}
Expand All @@ -62,7 +83,6 @@ export class MdButton {
inputs: ['color'],
host: {
'[class.md-button-focus]': 'isKeyboardFocused',
'[class]': 'setClassList()',
'(mousedown)': 'setMousedown()',
'(focus)': 'setKeyboardFocus()',
'(blur)': 'removeKeyboardFocus()'
Expand All @@ -74,6 +94,10 @@ export class MdButton {
export class MdAnchor extends MdButton {
_disabled: boolean = null;

constructor(elementRef: ElementRef, renderer: Renderer) {
super(elementRef, renderer);
}

@HostBinding('tabIndex')
get tabIndex(): number {
return this.disabled ? -1 : 0;
Expand Down
2 changes: 2 additions & 0 deletions src/components/toolbar/toolbar.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,13 @@ export function main() {
testComponent.toolbarColor = 'accent';
fixture.detectChanges();

expect(toolbarDebugElement.nativeElement.classList.contains('md-primary')).toBe(false);
expect(toolbarDebugElement.nativeElement.classList.contains('md-accent')).toBe(true);

testComponent.toolbarColor = 'warn';
fixture.detectChanges();

expect(toolbarDebugElement.nativeElement.classList.contains('md-accent')).toBe(false);
expect(toolbarDebugElement.nativeElement.classList.contains('md-warn')).toBe(true);

done();
Expand Down
14 changes: 7 additions & 7 deletions src/components/toolbar/toolbar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@ export class MdToolbar {
}

_updateColor(newColor: string) {
if (this._color != null && this._color != '') {
this.renderer.setElementClass(this.elementRef.nativeElement, `md-${newColor}`, false);
}
this._setElementColor(this._color, false);
this._setElementColor(newColor, true);
this._color = newColor;
}

if (newColor != null && newColor != '') {
this.renderer.setElementClass(this.elementRef.nativeElement, `md-${newColor}`, true);
_setElementColor(color: string, isAdd: boolean) {
if (color != null && color != '') {
this.renderer.setElementClass(this.elementRef.nativeElement, `md-${color}`, isAdd);
}

this._color = newColor;
}

}

0 comments on commit 62265cc

Please sign in to comment.