Skip to content

Commit 2f337d1

Browse files
committed
fix(button): apply color classes correctly.
Fixes angular#75. Closes angular#89
1 parent 358d923 commit 2f337d1

File tree

4 files changed

+55
-5
lines changed

4 files changed

+55
-5
lines changed

src/components/button/button.spec.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,30 @@ export function main() {
4343
});
4444
});
4545

46+
it('should should not clear previous defined classes', (done: () => void) => {
47+
return builder.createAsync(TestApp).then((fixture) => {
48+
let testComponent = fixture.debugElement.componentInstance;
49+
let buttonDebugElement = fixture.debugElement.query(By.css('button'));
50+
51+
buttonDebugElement.nativeElement.classList.add('custom-class');
52+
53+
testComponent.buttonColor = 'primary';
54+
fixture.detectChanges();
55+
56+
expect(buttonDebugElement.nativeElement.classList.contains('md-primary')).toBe(true);
57+
expect(buttonDebugElement.nativeElement.classList.contains('custom-class')).toBe(true);
58+
59+
testComponent.buttonColor = 'accent';
60+
fixture.detectChanges();
61+
62+
expect(buttonDebugElement.nativeElement.classList.contains('md-primary')).toBe(false);
63+
expect(buttonDebugElement.nativeElement.classList.contains('md-accent')).toBe(true);
64+
expect(buttonDebugElement.nativeElement.classList.contains('custom-class')).toBe(true);
65+
66+
done();
67+
});
68+
});
69+
4670
// Regular button tests
4771
describe('button[md-button]', () => {
4872
it('should handle a click on the button', (done: () => void) => {

src/components/button/button.ts

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import {
55
HostBinding,
66
HostListener,
77
ChangeDetectionStrategy,
8+
ElementRef,
9+
Renderer,
810
} from 'angular2/core';
911
import {TimerWrapper} from 'angular2/src/facade/async';
1012

@@ -18,7 +20,6 @@ import {TimerWrapper} from 'angular2/src/facade/async';
1820
inputs: ['color'],
1921
host: {
2022
'[class.md-button-focus]': 'isKeyboardFocused',
21-
'[class]': 'setClassList()',
2223
'(mousedown)': 'setMousedown()',
2324
'(focus)': 'setKeyboardFocus()',
2425
'(blur)': 'removeKeyboardFocus()'
@@ -29,15 +30,23 @@ import {TimerWrapper} from 'angular2/src/facade/async';
2930
changeDetection: ChangeDetectionStrategy.OnPush,
3031
})
3132
export class MdButton {
32-
color: string;
33+
private _color: string;
3334

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

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

40-
setClassList() { return `md-${this.color}`; }
41+
constructor(private elementRef: ElementRef, private renderer: Renderer) { }
42+
43+
get color(): string {
44+
return this._color;
45+
}
46+
47+
set color(value: string) {
48+
this._updateColor(value);
49+
}
4150

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

60+
_updateColor(newColor: string) {
61+
if (this._color != null && this._color != '') {
62+
this.renderer.setElementClass(this.elementRef.nativeElement, `md-${this._color}`, false);
63+
}
64+
65+
if (newColor != null && newColor != '') {
66+
this.renderer.setElementClass(this.elementRef.nativeElement, `md-${newColor}`, true);
67+
}
68+
69+
this._color = newColor;
70+
}
71+
5172
setKeyboardFocus($event: any) {
5273
this.isKeyboardFocused = !this.isMouseDown;
5374
}
@@ -62,7 +83,6 @@ export class MdButton {
6283
inputs: ['color'],
6384
host: {
6485
'[class.md-button-focus]': 'isKeyboardFocused',
65-
'[class]': 'setClassList()',
6686
'(mousedown)': 'setMousedown()',
6787
'(focus)': 'setKeyboardFocus()',
6888
'(blur)': 'removeKeyboardFocus()'
@@ -74,6 +94,10 @@ export class MdButton {
7494
export class MdAnchor extends MdButton {
7595
_disabled: boolean = null;
7696

97+
constructor(elementRef: ElementRef, renderer: Renderer) {
98+
super(elementRef, renderer);
99+
}
100+
77101
@HostBinding('tabIndex')
78102
get tabIndex(): number {
79103
return this.disabled ? -1 : 0;

src/components/toolbar/toolbar.spec.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,13 @@ export function main() {
3636
testComponent.toolbarColor = 'accent';
3737
fixture.detectChanges();
3838

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

4142
testComponent.toolbarColor = 'warn';
4243
fixture.detectChanges();
4344

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

4648
done();

src/components/toolbar/toolbar.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export class MdToolbar {
2929

3030
_updateColor(newColor: string) {
3131
if (this._color != null && this._color != '') {
32-
this.renderer.setElementClass(this.elementRef.nativeElement, `md-${newColor}`, false);
32+
this.renderer.setElementClass(this.elementRef.nativeElement, `md-${this._color}`, false);
3333
}
3434

3535
if (newColor != null && newColor != '') {

0 commit comments

Comments
 (0)