Skip to content

Commit 5c763b1

Browse files
committed
fix(prgoress-spinner): fix color input on md-spinner
* Makes color application dynamic by moving it to a getter / setter. (similar to all other components & makes it easier to share the code in the future) * Now the `md-spinner` can also have a `color` binding. * Fixes the missing documentation for the `color` and `value` inputs * Removes the unused `changeDetectorRef` from the constructor DI Fixes #2393
1 parent 026c70a commit 5c763b1

File tree

2 files changed

+75
-13
lines changed

2 files changed

+75
-13
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: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ import {
88
OnDestroy,
99
Input,
1010
ElementRef,
11-
NgZone
11+
NgZone,
12+
Renderer
1213
} from '@angular/core';
1314
import {DefaultStyleCompatibilityModeModule} from '../core';
1415

@@ -43,10 +44,7 @@ type EasingFn = (currentTime: number, startValue: number,
4344
host: {
4445
'role': 'progressbar',
4546
'[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"',
47+
'[attr.aria-valuemax]': '_ariaValueMax'
5048
},
5149
templateUrl: 'progress-spinner.html',
5250
styleUrls: ['progress-spinner.css'],
@@ -62,6 +60,10 @@ export class MdProgressSpinner implements OnDestroy {
6260
/** The SVG <path> node that is used to draw the circle. */
6361
private _path: SVGPathElement;
6462

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

97+
/** The color of the progress-spinner. Can be primary, accent, or warn. */
98+
@Input()
99+
get color(): string { return this._color; }
100+
set color(value: string) {
101+
this._updateColor(value);
102+
}
103+
95104
/** Value of the progress circle. It is bound to the host as the attribute aria-valuenow. */
96-
private _value: number;
97105
@Input()
98106
@HostBinding('attr.aria-valuenow')
99107
get value() {
@@ -128,14 +136,11 @@ export class MdProgressSpinner implements OnDestroy {
128136
}
129137
this._mode = m;
130138
}
131-
private _mode: ProgressSpinnerMode = 'determinate';
132-
133-
@Input() color: 'primary' | 'accent' | 'warn' = 'primary';
134139

135140
constructor(
136-
private _changeDetectorRef: ChangeDetectorRef,
137141
private _ngZone: NgZone,
138-
private _elementRef: ElementRef
142+
private _elementRef: ElementRef,
143+
private _renderer: Renderer
139144
) {}
140145

141146

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

234256

@@ -249,8 +271,9 @@ export class MdProgressSpinner implements OnDestroy {
249271
styleUrls: ['progress-spinner.css'],
250272
})
251273
export class MdSpinner extends MdProgressSpinner implements OnDestroy {
252-
constructor(changeDetectorRef: ChangeDetectorRef, elementRef: ElementRef, ngZone: NgZone) {
253-
super(changeDetectorRef, ngZone, elementRef);
274+
275+
constructor(elementRef: ElementRef, ngZone: NgZone, renderer: Renderer) {
276+
super(ngZone, elementRef, renderer);
254277
this.mode = 'indeterminate';
255278
}
256279

0 commit comments

Comments
 (0)