Skip to content

Commit e4f0e70

Browse files
committed
fix(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 f405cb6 commit e4f0e70

File tree

3 files changed

+59
-9
lines changed

3 files changed

+59
-9
lines changed

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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,41 @@ describe('MatProgressBar', () => {
183183
.toBe(false, 'Expect aria-valuenow to be cleared in query mode.');
184184
});
185185

186+
it('should update the DOM transform when the value has changed', () => {
187+
const fixture = createComponent(BasicProgressBar);
188+
fixture.detectChanges();
189+
190+
const progressElement = fixture.debugElement.query(By.css('mat-progress-bar'))!;
191+
const progressComponent = progressElement.componentInstance;
192+
const primaryBar = progressElement.nativeElement.querySelector('.mat-progress-bar-primary');
193+
194+
expect(primaryBar.style.transform).toBe('scaleX(0)');
195+
196+
progressComponent.value = 40;
197+
fixture.detectChanges();
198+
199+
expect(primaryBar.style.transform).toBe('scaleX(0.4)');
200+
});
201+
202+
it('should update the DOM transform when the bufferValue has changed', () => {
203+
const fixture = createComponent(BasicProgressBar);
204+
fixture.detectChanges();
205+
206+
const progressElement = fixture.debugElement.query(By.css('mat-progress-bar'))!;
207+
const progressComponent = progressElement.componentInstance;
208+
const bufferBar = progressElement.nativeElement.querySelector('.mat-progress-bar-buffer');
209+
210+
progressComponent.mode = 'buffer';
211+
fixture.detectChanges();
212+
213+
expect(bufferBar.style.transform).toBeFalsy();
214+
215+
progressComponent.bufferValue = 40;
216+
fixture.detectChanges();
217+
218+
expect(bufferBar.style.transform).toBe('scaleX(0.4)');
219+
});
220+
186221
});
187222

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

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

Lines changed: 21 additions & 7 deletions
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';
@@ -111,16 +112,21 @@ export class MatProgressBar extends _MatProgressBarMixinBase implements CanColor
111112
* @deprecated `location` parameter to be made required.
112113
* @breaking-change 8.0.0
113114
*/
114-
@Optional() @Inject(MAT_PROGRESS_BAR_LOCATION) location?: MatProgressBarLocation) {
115+
@Optional() @Inject(MAT_PROGRESS_BAR_LOCATION) location?: MatProgressBarLocation,
116+
117+
/**
118+
* @deprecated `_changeDetectorRef` parameter to be made required.
119+
* @breaking-change 11.0.0
120+
*/
121+
private _changeDetectorRef?: ChangeDetectorRef) {
115122
super(_elementRef);
116123

117124
// We need to prefix the SVG reference with the current path, otherwise they won't work
118125
// in Safari if the page has a `<base>` tag. Note that we need quotes inside the `url()`,
119-
120-
// because named route URLs can contain parentheses (see #12338). Also we don't use since
121-
// we can't tell the difference between whether
122-
// the consumer is using the hash location strategy or not, because `Location` normalizes
123-
// both `/#/foo/bar` and `/foo/bar` to the same thing.
126+
// because named route URLs can contain parentheses (see #12338). Also we don't use `Location`
127+
// since we can't tell the difference between whether the consumer is using the hash location
128+
// strategy or not, because `Location` normalizes both `/#/foo/bar` and `/foo/bar` to
129+
// the same thing.
124130
const path = location ? location.getPathname().split('#')[0] : '';
125131
this._rectangleFillValue = `url('${path}#${this.progressbarId}')`;
126132
this._isNoopAnimation = _animationMode === 'NoopAnimations';
@@ -134,13 +140,21 @@ export class MatProgressBar extends _MatProgressBarMixinBase implements CanColor
134140
get value(): number { return this._value; }
135141
set value(v: number) {
136142
this._value = clamp(coerceNumberProperty(v) || 0);
143+
144+
// @breaking-change 11.0.0 Remove null check for _changeDetectorRef.
145+
this._changeDetectorRef?.markForCheck();
137146
}
138147
private _value: number = 0;
139148

140149
/** Buffer value of the progress bar. Defaults to zero. */
141150
@Input()
142151
get bufferValue(): number { return this._bufferValue; }
143-
set bufferValue(v: number) { this._bufferValue = clamp(v || 0); }
152+
set bufferValue(v: number) {
153+
this._bufferValue = clamp(v || 0);
154+
155+
// @breaking-change 11.0.0 Remove null check for _changeDetectorRef.
156+
this._changeDetectorRef?.markForCheck();
157+
}
144158
private _bufferValue: number = 0;
145159

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

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

Lines changed: 3 additions & 2 deletions
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.ɵɵComponentDefWithMeta<MatProgressBar, "mat-progress-bar", ["matProgressBar"], { "color": "color"; "value": "value"; "bufferValue": "bufferValue"; "mode": "mode"; }, { "animationEnd": "animationEnd"; }, never, never>;
30-
static ɵfac: i0.ɵɵFactoryDef<MatProgressBar, [null, null, { optional: true; }, { optional: true; }]>;
31+
static ɵfac: i0.ɵɵFactoryDef<MatProgressBar, [null, null, { optional: true; }, { optional: true; }, null]>;
3132
}
3233

3334
export interface MatProgressBarLocation {

0 commit comments

Comments
 (0)