Skip to content

Commit cac8b48

Browse files
committed
fix(material/progress-bar): unable to change value through property setter
Fixes the progress bar not updating when its value is changed through the setter. Normally we don't really handle cases like this, but I decided to do it in this one, because: 1. We were already paying the payload price for the setter anyway so adding the `markForCheck` call won't be too expensive. 2. The progress bar is a bit of a special case where it might make sense not to go through the view to change a value. E.g. for something like a file upload where everything is being done in memory. Fixes #18676.
1 parent 0f9c1e6 commit cac8b48

File tree

3 files changed

+59
-9
lines changed

3 files changed

+59
-9
lines changed

src/material/progress-bar/progress-bar.spec.ts

+35
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,41 @@ describe('MatProgressBar', () => {
196196
.toBe(false, 'Expect aria-valuenow to be cleared in query mode.');
197197
});
198198

199+
it('should update the DOM transform when the value has changed', () => {
200+
const fixture = createComponent(BasicProgressBar);
201+
fixture.detectChanges();
202+
203+
const progressElement = fixture.debugElement.query(By.css('mat-progress-bar'))!;
204+
const progressComponent = progressElement.componentInstance;
205+
const primaryBar = progressElement.nativeElement.querySelector('.mat-progress-bar-primary');
206+
207+
expect(primaryBar.style.transform).toBe('scaleX(0)');
208+
209+
progressComponent.value = 40;
210+
fixture.detectChanges();
211+
212+
expect(primaryBar.style.transform).toBe('scaleX(0.4)');
213+
});
214+
215+
it('should update the DOM transform when the bufferValue has changed', () => {
216+
const fixture = createComponent(BasicProgressBar);
217+
fixture.detectChanges();
218+
219+
const progressElement = fixture.debugElement.query(By.css('mat-progress-bar'))!;
220+
const progressComponent = progressElement.componentInstance;
221+
const bufferBar = progressElement.nativeElement.querySelector('.mat-progress-bar-buffer');
222+
223+
progressComponent.mode = 'buffer';
224+
fixture.detectChanges();
225+
226+
expect(bufferBar.style.transform).toBeFalsy();
227+
228+
progressComponent.bufferValue = 40;
229+
fixture.detectChanges();
230+
231+
expect(bufferBar.style.transform).toBe('scaleX(0.4)');
232+
});
233+
199234
});
200235

201236
describe('animation trigger on determinate setting', () => {

src/material/progress-bar/progress-bar.ts

+21-7
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import {
2323
Output,
2424
ViewChild,
2525
ViewEncapsulation,
26+
ChangeDetectorRef,
2627
} from '@angular/core';
2728
import {CanColor, CanColorCtor, mixinColor} from '@angular/material/core';
2829
import {ANIMATION_MODULE_TYPE} from '@angular/platform-browser/animations';
@@ -114,16 +115,21 @@ export class MatProgressBar extends _MatProgressBarMixinBase implements CanColor
114115
* @deprecated `location` parameter to be made required.
115116
* @breaking-change 8.0.0
116117
*/
117-
@Optional() @Inject(MAT_PROGRESS_BAR_LOCATION) location?: MatProgressBarLocation) {
118+
@Optional() @Inject(MAT_PROGRESS_BAR_LOCATION) location?: MatProgressBarLocation,
119+
120+
/**
121+
* @deprecated `_changeDetectorRef` parameter to be made required.
122+
* @breaking-change 11.0.0
123+
*/
124+
private _changeDetectorRef?: ChangeDetectorRef) {
118125
super(_elementRef);
119126

120127
// We need to prefix the SVG reference with the current path, otherwise they won't work
121128
// in Safari if the page has a `<base>` tag. Note that we need quotes inside the `url()`,
122-
123-
// because named route URLs can contain parentheses (see #12338). Also we don't use since
124-
// we can't tell the difference between whether
125-
// the consumer is using the hash location strategy or not, because `Location` normalizes
126-
// both `/#/foo/bar` and `/foo/bar` to the same thing.
129+
// because named route URLs can contain parentheses (see #12338). Also we don't use `Location`
130+
// since we can't tell the difference between whether the consumer is using the hash location
131+
// strategy or not, because `Location` normalizes both `/#/foo/bar` and `/foo/bar` to
132+
// the same thing.
127133
const path = location ? location.getPathname().split('#')[0] : '';
128134
this._rectangleFillValue = `url('${path}#${this.progressbarId}')`;
129135
this._isNoopAnimation = _animationMode === 'NoopAnimations';
@@ -137,13 +143,21 @@ export class MatProgressBar extends _MatProgressBarMixinBase implements CanColor
137143
get value(): number { return this._value; }
138144
set value(v: number) {
139145
this._value = clamp(coerceNumberProperty(v) || 0);
146+
147+
// @breaking-change 11.0.0 Remove null check for _changeDetectorRef.
148+
this._changeDetectorRef?.markForCheck();
140149
}
141150
private _value: number = 0;
142151

143152
/** Buffer value of the progress bar. Defaults to zero. */
144153
@Input()
145154
get bufferValue(): number { return this._bufferValue; }
146-
set bufferValue(v: number) { this._bufferValue = clamp(v || 0); }
155+
set bufferValue(v: number) {
156+
this._bufferValue = clamp(v || 0);
157+
158+
// @breaking-change 11.0.0 Remove null check for _changeDetectorRef.
159+
this._changeDetectorRef?.markForCheck();
160+
}
147161
private _bufferValue: number = 0;
148162

149163
@ViewChild('primaryValueBar') _primaryValueBar: ElementRef;

tools/public_api_guard/material/progress-bar.d.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ export declare class MatProgressBar extends _MatProgressBarMixinBase implements
1616
get value(): number;
1717
set value(v: number);
1818
constructor(_elementRef: ElementRef, _ngZone: NgZone, _animationMode?: string | undefined,
19-
location?: MatProgressBarLocation);
19+
location?: MatProgressBarLocation,
20+
_changeDetectorRef?: ChangeDetectorRef | undefined);
2021
_bufferTransform(): {
2122
transform: string;
2223
} | null;
@@ -27,7 +28,7 @@ export declare class MatProgressBar extends _MatProgressBarMixinBase implements
2728
ngOnDestroy(): void;
2829
static ngAcceptInputType_value: NumberInput;
2930
static ɵcmp: i0.ɵɵComponentDeclaration<MatProgressBar, "mat-progress-bar", ["matProgressBar"], { "color": "color"; "value": "value"; "bufferValue": "bufferValue"; "mode": "mode"; }, { "animationEnd": "animationEnd"; }, never, never>;
30-
static ɵfac: i0.ɵɵFactoryDeclaration<MatProgressBar, [null, null, { optional: true; }, { optional: true; }]>;
31+
static ɵfac: i0.ɵɵFactoryDeclaration<MatProgressBar, [null, null, { optional: true; }, { optional: true; }, null]>;
3132
}
3233

3334
export interface MatProgressBarLocation {

0 commit comments

Comments
 (0)