Skip to content

Commit c098480

Browse files
committed
Address feedback
1 parent fb3af01 commit c098480

File tree

8 files changed

+68
-24
lines changed

8 files changed

+68
-24
lines changed

src/lib/button/button.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,8 @@ export class MdButton extends MdThemeable {
120120
get disabled() { return this._disabled; }
121121
set disabled(value: boolean) { this._disabled = coerceBooleanProperty(value) ? true : null; }
122122

123-
constructor(private _elementRef: ElementRef, private _renderer: Renderer) {
124-
super(_renderer, _elementRef);
123+
constructor(elementRef: ElementRef, renderer: Renderer) {
124+
super(renderer, elementRef);
125125
}
126126

127127
_setMousedown() {

src/lib/checkbox/checkbox.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -173,10 +173,10 @@ export class MdCheckbox extends MdThemeable implements ControlValueAccessor {
173173

174174
_hasFocus: boolean = false;
175175

176-
constructor(private _renderer: Renderer,
177-
private _elementRef: ElementRef,
178-
private _changeDetectorRef: ChangeDetectorRef) {
179-
super(_renderer, _elementRef);
176+
constructor(private _changeDetectorRef: ChangeDetectorRef,
177+
renderer: Renderer,
178+
elementRef: ElementRef) {
179+
super(renderer, elementRef);
180180

181181
// By default set the component color to accent.
182182
this.color = 'accent';

src/lib/chips/chip.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ export class MdChip extends MdThemeable implements Focusable, OnInit, OnDestroy
5656
/** Emitted when the chip is destroyed. */
5757
@Output() destroy = new EventEmitter<MdChipEvent>();
5858

59-
constructor(protected _renderer: Renderer, protected _elementRef: ElementRef) {
60-
super(_renderer, _elementRef);
59+
constructor(renderer: Renderer, elementRef: ElementRef) {
60+
super(renderer, elementRef);
6161

6262
// By default the chip elements should use the primary palette.
6363
this.color = 'primary';

src/lib/core/style/themeable.spec.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,19 @@ describe('MdThemeable', () => {
4444
expect(themeableElement.classList).toContain('mat-accent');
4545
expect(themeableElement.classList).not.toContain('mat-warn');
4646
expect(themeableElement.classList).not.toContain('mat-primary');
47+
48+
testComponent.color = null;
49+
fixture.detectChanges();
50+
51+
expect(themeableElement.classList).not.toContain('mat-accent');
52+
expect(themeableElement.classList).not.toContain('mat-warn');
53+
expect(themeableElement.classList).not.toContain('mat-primary');
54+
});
55+
56+
it('should throw an error when using an invalid color', () => {
57+
testComponent.color = 'Invalid';
58+
59+
expect(() => fixture.detectChanges()).toThrow();
4760
});
4861

4962
});

src/lib/core/style/themeable.ts

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,61 @@
11
import {ElementRef, Input, Renderer} from '@angular/core';
2+
import {MdError} from '../errors/error';
23

3-
/** Material components can extend the MdThemeable class to add an Input that can
4-
* developers use to switch palettes on the components. */
4+
/** Possible color values for the color input. */
5+
export type MdThemeColor = 'primary' | 'accent' | 'warn';
6+
7+
const VALID_COLOR_VALUES = ['primary', 'accent', 'warn'];
8+
9+
/**
10+
* Material components can extend the MdThemeable class to add an Input that can
11+
* developers use to switch palettes on the components.
12+
**/
513
export class MdThemeable {
614

715
/** Stored color for the themeable component. */
8-
private _color: string;
16+
private _color: MdThemeColor;
917

10-
constructor(private renderer: Renderer, private elementRef: ElementRef) {}
18+
// Constructor initializers need to have the `protected` modifier to avoid interferences.
19+
// TypeScript throws if similar declarations, regardless of the modifier, have been
20+
// found across the different classes. Because of that, the child classes should just use
21+
// the protected properties from the superclass.
22+
constructor(protected _renderer: Renderer, protected _elementRef: ElementRef) {}
1123

1224
/** Color of the component. Values are primary, accent, or warn. */
1325
@Input()
14-
get color(): string {
26+
get color(): MdThemeColor {
1527
return this._color;
1628
}
17-
set color(newColor: string) {
29+
set color(newColor: MdThemeColor) {
30+
this._validateColor(newColor);
31+
1832
this._setElementColor(this._color, false);
1933
this._setElementColor(newColor, true);
2034
this._color = newColor;
2135
}
2236

37+
/** Validates the specified color value and throws an error if invalid. */
38+
private _validateColor(color: string) {
39+
if (color && VALID_COLOR_VALUES.indexOf(color) === -1) {
40+
throw new MdInvalidColorValueError(color);
41+
}
42+
}
43+
2344
/** Toggles a color class on the components host element. */
2445
private _setElementColor(color: string, isAdd: boolean) {
25-
if (color != null && color != '') {
26-
this.renderer.setElementClass(this.elementRef.nativeElement, `mat-${color}`, isAdd);
46+
if (color) {
47+
this._renderer.setElementClass(this._elementRef.nativeElement, `mat-${color}`, isAdd);
2748
}
2849
}
2950

3051
}
52+
53+
/** Error that will be thrown if the color input is set to an invalid value. */
54+
export class MdInvalidColorValueError extends MdError {
55+
constructor(invalidColor: string) {
56+
super(
57+
`The color "${invalidColor}" for is not valid. ` +
58+
`Possible values are: ${VALID_COLOR_VALUES.join(', ')} or null.`
59+
);
60+
}
61+
}

src/lib/icon/icon.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,10 @@ export class MdIcon extends MdThemeable implements OnChanges, OnInit, AfterViewC
9595
private _previousAriaLabel: string;
9696

9797
constructor(
98-
private _elementRef: ElementRef,
99-
private _renderer: Renderer,
100-
private _mdIconRegistry: MdIconRegistry) {
101-
super(_renderer, _elementRef);
98+
private _mdIconRegistry: MdIconRegistry,
99+
elementRef: ElementRef,
100+
renderer: Renderer) {
101+
super(renderer, elementRef);
102102
}
103103

104104
/**

src/lib/progress-spinner/progress-spinner.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,8 @@ export class MdProgressSpinner extends MdThemeable implements OnDestroy {
153153
this._mode = m;
154154
}
155155

156-
constructor(private _ngZone: NgZone, private _elementRef: ElementRef, renderer: Renderer) {
157-
super(renderer, _elementRef);
156+
constructor(private _ngZone: NgZone, elementRef: ElementRef, renderer: Renderer) {
157+
super(renderer, elementRef);
158158
}
159159

160160

src/lib/slide-toggle/slide-toggle.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,8 @@ export class MdSlideToggle extends MdThemeable implements AfterContentInit, Cont
115115

116116
@ViewChild('input') _inputElement: ElementRef;
117117

118-
constructor(private _elementRef: ElementRef, private _renderer: Renderer) {
119-
super(_renderer, _elementRef);
118+
constructor(elementRef: ElementRef, renderer: Renderer) {
119+
super(renderer, elementRef);
120120
}
121121

122122
ngAfterContentInit() {

0 commit comments

Comments
 (0)