forked from angular/components
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Introduces a new `MdThemeable` base class that can be extended by different components to automatically support the `color` input. * This reduces a lot of repeated code in the different components and it also simplifies maintaining. Closes angular#2394.
- Loading branch information
1 parent
e908663
commit 827eb0b
Showing
10 changed files
with
136 additions
and
185 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import {async, ComponentFixture, TestBed} from '@angular/core/testing'; | ||
import {Component, ElementRef, Renderer} from '@angular/core'; | ||
import {MdThemeable} from './themeable'; | ||
import {By} from '@angular/platform-browser'; | ||
|
||
describe('MdThemeable', () => { | ||
|
||
let fixture: ComponentFixture<TestComponent>; | ||
let testComponent: TestComponent; | ||
let themeableElement: HTMLElement; | ||
|
||
beforeEach(async(() => { | ||
TestBed.configureTestingModule({ | ||
declarations: [TestComponent, ThemeableComponent], | ||
}); | ||
|
||
TestBed.compileComponents(); | ||
})); | ||
|
||
beforeEach(() => { | ||
fixture = TestBed.createComponent(TestComponent); | ||
fixture.detectChanges(); | ||
|
||
testComponent = fixture.componentInstance; | ||
themeableElement = fixture.debugElement.query(By.css('themeable-test')).nativeElement; | ||
}); | ||
|
||
it('should support a default component color', () => { | ||
expect(themeableElement.classList).toContain('mat-warn'); | ||
}); | ||
|
||
it('should update classes on color change', () => { | ||
expect(themeableElement.classList).toContain('mat-warn'); | ||
|
||
testComponent.color = 'primary'; | ||
fixture.detectChanges(); | ||
|
||
expect(themeableElement.classList).toContain('mat-primary'); | ||
expect(themeableElement.classList).not.toContain('mat-warn'); | ||
|
||
testComponent.color = 'accent'; | ||
fixture.detectChanges(); | ||
|
||
expect(themeableElement.classList).toContain('mat-accent'); | ||
expect(themeableElement.classList).not.toContain('mat-warn'); | ||
expect(themeableElement.classList).not.toContain('mat-primary'); | ||
}); | ||
|
||
}); | ||
|
||
@Component({ | ||
selector: 'themeable-test', | ||
template: '<span>Themeable</span>' | ||
}) | ||
class ThemeableComponent extends MdThemeable { | ||
constructor(renderer: Renderer, elementRef: ElementRef) { | ||
super(renderer, elementRef); | ||
} | ||
} | ||
|
||
@Component({ | ||
template: '<themeable-test [color]="color"></themeable-test>' | ||
}) | ||
class TestComponent { | ||
color: string = 'warn'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import {ElementRef, Input, Renderer} from '@angular/core'; | ||
|
||
/** Material components can extend the MdThemeable class to add an Input that can | ||
* developers use to switch palettes on the components. */ | ||
export class MdThemeable { | ||
|
||
/** Stored color for the themeable component. */ | ||
private _color: string; | ||
|
||
constructor(private renderer: Renderer, private elementRef: ElementRef) {} | ||
|
||
/** Color of the component. Values are primary, accent, or warn. */ | ||
@Input() | ||
get color(): string { | ||
return this._color; | ||
} | ||
set color(newColor: string) { | ||
this._setElementColor(this._color, false); | ||
this._setElementColor(newColor, true); | ||
this._color = newColor; | ||
} | ||
|
||
/** Toggles a color class on the components host element. */ | ||
private _setElementColor(color: string, isAdd: boolean) { | ||
if (color != null && color != '') { | ||
this.renderer.setElementClass(this.elementRef.nativeElement, `mat-${color}`, isAdd); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.