Skip to content

Commit 6cb6576

Browse files
devversionkara
authored andcommitted
fix(progress-spinner): fix color input on md-spinner (#2396)
Fixes #2393
1 parent ed3ffe0 commit 6cb6576

File tree

2 files changed

+77
-14
lines changed

2 files changed

+77
-14
lines changed

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

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@ describe('MdProgressSpinner', () => {
1313
BasicProgressSpinner,
1414
IndeterminateProgressSpinner,
1515
ProgressSpinnerWithValueAndBoundMode,
16+
ProgressSpinnerWithColor,
1617
IndeterminateProgressSpinnerWithNgIf,
1718
SpinnerWithNgIf,
19+
SpinnerWithColor
1820
],
1921
});
2022

@@ -105,6 +107,37 @@ describe('MdProgressSpinner', () => {
105107

106108
expect(progressElement.componentInstance.interdeterminateInterval).toBeFalsy();
107109
});
110+
111+
it('should set the color class on the md-spinner', () => {
112+
let fixture = TestBed.createComponent(SpinnerWithColor);
113+
fixture.detectChanges();
114+
115+
let progressElement = fixture.debugElement.query(By.css('md-spinner'));
116+
117+
expect(progressElement.nativeElement.classList).toContain('md-primary');
118+
119+
fixture.debugElement.componentInstance.color = 'accent';
120+
fixture.detectChanges();
121+
122+
expect(progressElement.nativeElement.classList).toContain('md-accent');
123+
expect(progressElement.nativeElement.classList).not.toContain('md-primary');
124+
});
125+
126+
it('should set the color class on the md-progress-spinner', () => {
127+
let fixture = TestBed.createComponent(ProgressSpinnerWithColor);
128+
fixture.detectChanges();
129+
130+
let progressElement = fixture.debugElement.query(By.css('md-progress-spinner'));
131+
132+
expect(progressElement.nativeElement.classList).toContain('md-primary');
133+
134+
fixture.debugElement.componentInstance.color = 'accent';
135+
fixture.detectChanges();
136+
137+
expect(progressElement.nativeElement.classList).toContain('md-accent');
138+
expect(progressElement.nativeElement.classList).not.toContain('md-primary');
139+
});
140+
108141
});
109142

110143

@@ -123,3 +156,9 @@ class IndeterminateProgressSpinnerWithNgIf { }
123156

124157
@Component({template: `<md-spinner *ngIf="!isHidden"></md-spinner>`})
125158
class SpinnerWithNgIf { }
159+
160+
@Component({template: `<md-spinner [color]="color"></md-spinner>`})
161+
class SpinnerWithColor { color: string = 'primary'; }
162+
163+
@Component({template: `<md-progress-spinner value="50" [color]="color"></md-progress-spinner>`})
164+
class ProgressSpinnerWithColor { color: string = 'primary'; }

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

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ import {
33
ModuleWithProviders,
44
Component,
55
HostBinding,
6-
ChangeDetectorRef,
76
ChangeDetectionStrategy,
87
OnDestroy,
98
Input,
109
ElementRef,
11-
NgZone
10+
NgZone,
11+
Renderer
1212
} from '@angular/core';
1313
import {DefaultStyleCompatibilityModeModule} from '../core';
1414

@@ -43,10 +43,7 @@ type EasingFn = (currentTime: number, startValue: number,
4343
host: {
4444
'role': 'progressbar',
4545
'[attr.aria-valuemin]': '_ariaValueMin',
46-
'[attr.aria-valuemax]': '_ariaValueMax',
47-
'[class.md-primary]': 'color == "primary"',
48-
'[class.md-accent]': 'color == "accent"',
49-
'[class.md-warn]': 'color == "warn"',
46+
'[attr.aria-valuemax]': '_ariaValueMax'
5047
},
5148
templateUrl: 'progress-spinner.html',
5249
styleUrls: ['progress-spinner.css'],
@@ -62,6 +59,10 @@ export class MdProgressSpinner implements OnDestroy {
6259
/** The SVG <path> node that is used to draw the circle. */
6360
private _path: SVGPathElement;
6461

62+
private _mode: ProgressSpinnerMode = 'determinate';
63+
private _value: number;
64+
private _color: string = 'primary';
65+
6566
/**
6667
* Values for aria max and min are only defined as numbers when in a determinate mode. We do this
6768
* because voiceover does not report the progress indicator as indeterminate if the aria min
@@ -92,8 +93,14 @@ export class MdProgressSpinner implements OnDestroy {
9293
this._cleanupIndeterminateAnimation();
9394
}
9495

96+
/** The color of the progress-spinner. Can be primary, accent, or warn. */
97+
@Input()
98+
get color(): string { return this._color; }
99+
set color(value: string) {
100+
this._updateColor(value);
101+
}
102+
95103
/** Value of the progress circle. It is bound to the host as the attribute aria-valuenow. */
96-
private _value: number;
97104
@Input()
98105
@HostBinding('attr.aria-valuenow')
99106
get value() {
@@ -128,14 +135,11 @@ export class MdProgressSpinner implements OnDestroy {
128135
}
129136
this._mode = m;
130137
}
131-
private _mode: ProgressSpinnerMode = 'determinate';
132-
133-
@Input() color: 'primary' | 'accent' | 'warn' = 'primary';
134138

135139
constructor(
136-
private _changeDetectorRef: ChangeDetectorRef,
137140
private _ngZone: NgZone,
138-
private _elementRef: ElementRef
141+
private _elementRef: ElementRef,
142+
private _renderer: Renderer
139143
) {}
140144

141145

@@ -229,6 +233,23 @@ export class MdProgressSpinner implements OnDestroy {
229233
path.setAttribute('d', getSvgArc(currentValue, rotation));
230234
}
231235
}
236+
237+
/**
238+
* Updates the color of the progress-spinner by adding the new palette class to the element
239+
* and removing the old one.
240+
*/
241+
private _updateColor(newColor: string) {
242+
this._setElementColor(this._color, false);
243+
this._setElementColor(newColor, true);
244+
this._color = newColor;
245+
}
246+
247+
/** Sets the given palette class on the component element. */
248+
private _setElementColor(color: string, isAdd: boolean) {
249+
if (color != null && color != '') {
250+
this._renderer.setElementClass(this._elementRef.nativeElement, `md-${color}`, isAdd);
251+
}
252+
}
232253
}
233254

234255

@@ -245,12 +266,15 @@ export class MdProgressSpinner implements OnDestroy {
245266
'role': 'progressbar',
246267
'mode': 'indeterminate',
247268
},
269+
// Due to the class extending we need to explicitly say that the input exists.
270+
inputs: ['color'],
248271
templateUrl: 'progress-spinner.html',
249272
styleUrls: ['progress-spinner.css'],
250273
})
251274
export class MdSpinner extends MdProgressSpinner implements OnDestroy {
252-
constructor(changeDetectorRef: ChangeDetectorRef, elementRef: ElementRef, ngZone: NgZone) {
253-
super(changeDetectorRef, ngZone, elementRef);
275+
276+
constructor(elementRef: ElementRef, ngZone: NgZone, renderer: Renderer) {
277+
super(ngZone, elementRef, renderer);
254278
this.mode = 'indeterminate';
255279
}
256280

0 commit comments

Comments
 (0)