Skip to content

Commit

Permalink
fix(progress-spinner): unable to change mode on spinner directive
Browse files Browse the repository at this point in the history
Currently we have the `mat-spinner` directive which is a shortcut to a `mat-progress-spinner` with `mode="indeterminate"`. Since the spinner inherits all of the inputs from the progress spinner, there's nothing stoping people from changing the mode back to `determinate`, however the element will look half-broken because the host bindings assume that the mode won't change. These changes update the host bindings to allow switching between modes.

Fixes #14511.
  • Loading branch information
crisbeto committed Dec 20, 2018
1 parent ecaec18 commit c97bdb1
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 40 deletions.
4 changes: 1 addition & 3 deletions src/lib/progress-spinner/progress-spinner-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,17 @@
import {NgModule} from '@angular/core';
import {CommonModule} from '@angular/common';
import {MatCommonModule} from '@angular/material/core';
import {MatProgressSpinner, MatSpinner} from './progress-spinner';
import {MatProgressSpinner} from './progress-spinner';


@NgModule({
imports: [MatCommonModule, CommonModule],
exports: [
MatProgressSpinner,
MatSpinner,
MatCommonModule
],
declarations: [
MatProgressSpinner,
MatSpinner
],
})
class MatProgressSpinnerModule {}
Expand Down
13 changes: 13 additions & 0 deletions src/lib/progress-spinner/progress-spinner.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ describe('MatProgressSpinner', () => {
ProgressSpinnerCustomDiameter,
SpinnerWithColor,
ProgressSpinnerWithStringValues,
SpinnerWithMode,
],
}).compileComponents();
}));
Expand Down Expand Up @@ -296,6 +297,14 @@ describe('MatProgressSpinner', () => {
expect(progressElement.componentInstance.strokeWidth).toBe(7);
});

it('should be able to change the mode on a mat-spinner', () => {
const fixture = TestBed.createComponent(SpinnerWithMode);
fixture.detectChanges();

const progressElement = fixture.debugElement.query(By.css('mat-spinner')).nativeElement;
expect(progressElement.getAttribute('mode')).toBe('determinate');
});

});


Expand Down Expand Up @@ -335,3 +344,7 @@ class ProgressSpinnerWithColor { color: string = 'primary'; }
`
})
class ProgressSpinnerWithStringValues { }

@Component({template: '<mat-spinner mode="determinate"></mat-spinner>'})
class SpinnerWithMode { }

42 changes: 5 additions & 37 deletions src/lib/progress-spinner/progress-spinner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ const INDETERMINATE_ANIMATION_TEMPLATE = `
*/
@Component({
moduleId: module.id,
selector: 'mat-progress-spinner',
selector: 'mat-progress-spinner, mat-spinner',
exportAs: 'matProgressSpinner',
host: {
'role': 'progressbar',
Expand Down Expand Up @@ -186,6 +186,10 @@ export class MatProgressSpinner extends _MatProgressSpinnerMixinBase implements
super(_elementRef);
this._fallbackAnimation = platform.EDGE || platform.TRIDENT;

if (_elementRef.nativeElement.nodeName.toLowerCase() === 'mat-spinner') {
this.mode = 'indeterminate';
}

if (defaults) {
if (defaults.diameter) {
this.diameter = defaults.diameter;
Expand Down Expand Up @@ -265,39 +269,3 @@ export class MatProgressSpinner extends _MatProgressSpinnerMixinBase implements
.replace(/DIAMETER/g, `${this.diameter}`);
}
}


/**
* `<mat-spinner>` component.
*
* This is a component definition to be used as a convenience reference to create an
* indeterminate `<mat-progress-spinner>` instance.
*/
@Component({
moduleId: module.id,
selector: 'mat-spinner',
host: {
'role': 'progressbar',
'mode': 'indeterminate',
'class': 'mat-spinner mat-progress-spinner',
'[class._mat-animation-noopable]': `_noopAnimations`,
'[style.width.px]': 'diameter',
'[style.height.px]': 'diameter',
},
inputs: ['color'],
templateUrl: 'progress-spinner.html',
styleUrls: ['progress-spinner.css'],
changeDetection: ChangeDetectionStrategy.OnPush,
encapsulation: ViewEncapsulation.None,
})
export class MatSpinner extends MatProgressSpinner {
constructor(elementRef: ElementRef, platform: Platform,
@Optional() @Inject(DOCUMENT) document: any,
// @breaking-change 8.0.0 animationMode and defaults parameters to be made required.
@Optional() @Inject(ANIMATION_MODULE_TYPE) animationMode?: string,
@Inject(MAT_PROGRESS_SPINNER_DEFAULT_OPTIONS)
defaults?: MatProgressSpinnerDefaultOptions) {
super(elementRef, platform, document, animationMode, defaults);
this.mode = 'indeterminate';
}
}
6 changes: 6 additions & 0 deletions src/lib/progress-spinner/public-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,9 @@
export * from './progress-spinner-module';
export * from './progress-spinner';

/**
* @deprecated Import `MatProgressSpinner` instead. Note that the
* `mat-spinner` selector isn't deprecated.
* @breaking-change 8.0.0
*/
export {MatProgressSpinner as MatSpinner} from './progress-spinner';

0 comments on commit c97bdb1

Please sign in to comment.