Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(button): apply color classes correctly. #195

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a bit of repetition between lines 61-63 and lines 65-67. Can you refactor to share more code, like:

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

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

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kara Changed it as discussed. Thanks for the suggestion.

}

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;
}

}