Skip to content

Commit 6bfbe9b

Browse files
authored
fix(material/slider): update inactive input width on value change (#28275)
* fix(material/slider): update inactive input width on value change * fixes #28206 * fixup! fix(material/slider): update inactive input width on value change * fixup! fix(material/slider): update inactive input width on value change
1 parent 5ab2e7a commit 6bfbe9b

File tree

3 files changed

+45
-1
lines changed

3 files changed

+45
-1
lines changed

src/material/slider/slider-input.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,21 @@ export class MatSliderThumb implements _MatSliderThumb, OnDestroy, ControlValueA
9898
if (this._isActive) {
9999
return;
100100
}
101-
this._hostElement.value = stringValue;
101+
this._setValue(stringValue);
102+
}
103+
104+
/**
105+
* Handles programmatic value setting. This has been split out to
106+
* allow the range thumb to override it and add additional necessary logic.
107+
*/
108+
protected _setValue(value: string) {
109+
this._hostElement.value = value;
102110
this._updateThumbUIByValue();
103111
this._slider._onValueChange(this);
104112
this._cdr.detectChanges();
105113
this._slider._cdr.markForCheck();
106114
}
115+
107116
/** Event emitted when the `value` is changed. */
108117
@Output() readonly valueChange: EventEmitter<number> = new EventEmitter<number>();
109118

@@ -793,4 +802,10 @@ export class MatSliderRangeThumb extends MatSliderThumb implements _MatSliderRan
793802
this._updateSibling();
794803
}
795804
}
805+
806+
override _setValue(value: string) {
807+
super._setValue(value);
808+
this._updateWidthInactive();
809+
this._updateSibling();
810+
}
796811
}

src/material/slider/slider.spec.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1022,6 +1022,32 @@ describe('MDC-based MatSlider', () => {
10221022
fixture.detectChanges();
10231023
expect(endInput.value).toBe(70);
10241024
});
1025+
1026+
it('should update the input width when the start value changes', () => {
1027+
const startInputEl = startInput._elementRef.nativeElement;
1028+
const endInputEl = endInput._elementRef.nativeElement;
1029+
const startInputWidthBefore = startInputEl.getBoundingClientRect().width;
1030+
const endInputWidthBefore = endInputEl.getBoundingClientRect().width;
1031+
fixture.componentInstance.startValue = 10;
1032+
fixture.detectChanges();
1033+
const startInputWidthAfter = startInputEl.getBoundingClientRect().width;
1034+
const endInputWidthAfter = endInputEl.getBoundingClientRect().width;
1035+
expect(startInputWidthBefore).not.toBe(startInputWidthAfter);
1036+
expect(endInputWidthBefore).not.toBe(endInputWidthAfter);
1037+
});
1038+
1039+
it('should update the input width when the end value changes', () => {
1040+
const startInputEl = startInput._elementRef.nativeElement;
1041+
const endInputEl = endInput._elementRef.nativeElement;
1042+
const startInputWidthBefore = startInputEl.getBoundingClientRect().width;
1043+
const endInputWidthBefore = endInputEl.getBoundingClientRect().width;
1044+
fixture.componentInstance.endValue = 90;
1045+
fixture.detectChanges();
1046+
const startInputWidthAfter = startInputEl.getBoundingClientRect().width;
1047+
const endInputWidthAfter = endInputEl.getBoundingClientRect().width;
1048+
expect(startInputWidthBefore).not.toBe(startInputWidthAfter);
1049+
expect(endInputWidthBefore).not.toBe(endInputWidthAfter);
1050+
});
10251051
});
10261052

10271053
describe('slider with direction', () => {

tools/public_api_guard/material/slider.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,8 @@ export class MatSliderRangeThumb extends MatSliderThumb implements _MatSliderRan
180180
// (undocumented)
181181
_setIsLeftThumb(): void;
182182
// (undocumented)
183+
_setValue(value: string): void;
184+
// (undocumented)
183185
_updateMinMax(): void;
184186
// (undocumented)
185187
_updateStaticStyles(): void;
@@ -261,6 +263,7 @@ export class MatSliderThumb implements _MatSliderThumb, OnDestroy, ControlValueA
261263
registerOnChange(fn: any): void;
262264
registerOnTouched(fn: any): void;
263265
setDisabledState(isDisabled: boolean): void;
266+
protected _setValue(value: string): void;
264267
_skipUIUpdate: boolean;
265268
// (undocumented)
266269
protected _slider: _MatSlider;

0 commit comments

Comments
 (0)