Skip to content

Commit 87cbe59

Browse files
committed
fix(material/slider): handle null and undefined values in slider input
The slider values behaved inconsistently when we reset the formGroup. This MR will resolve that issue by setting `value` to default value when its `undefined` or `null` Fixes #30614
1 parent 810495c commit 87cbe59

File tree

3 files changed

+47
-3
lines changed

3 files changed

+47
-3
lines changed

src/material/slider/slider-input.ts

+3
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,9 @@ export class MatSliderThumb implements _MatSliderThumb, OnDestroy, ControlValueA
9696
return numberAttribute(this._hostElement.value, 0);
9797
}
9898
set value(value: number) {
99+
if (value === null) {
100+
value = this._getDefaultValue();
101+
}
99102
value = isNaN(value) ? 0 : value;
100103
const stringValue = value + '';
101104
if (!this._hasSetInitialValue) {

src/material/slider/slider.spec.ts

+42-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import {
1818
tick,
1919
waitForAsync,
2020
} from '@angular/core/testing';
21-
import {FormControl, FormsModule, ReactiveFormsModule} from '@angular/forms';
21+
import {FormControl, FormGroup, FormsModule, ReactiveFormsModule} from '@angular/forms';
2222
import {By} from '@angular/platform-browser';
2323
import {of} from 'rxjs';
2424
import {MatSliderModule} from './module';
@@ -1314,6 +1314,25 @@ describe('MatSlider', () => {
13141314
}));
13151315
});
13161316

1317+
describe('slider with form group', () => {
1318+
it('should reset to initial min-max value when form reset is done', fakeAsync(() => {
1319+
const fixture = createComponent(SliderWithFormGroup);
1320+
fixture.detectChanges();
1321+
const sliderDebugElement = fixture.debugElement.query(By.directive(MatSlider));
1322+
const slider = sliderDebugElement.componentInstance;
1323+
const minInput = slider._getInput(_MatThumb.START) as MatSliderRangeThumb;
1324+
const maxInput = slider._getInput(_MatThumb.END) as MatSliderRangeThumb;
1325+
flush();
1326+
1327+
expect(minInput.value).toBe(0);
1328+
expect(maxInput.value).toBe(10);
1329+
slideToValue(slider, minInput, 20);
1330+
fixture.componentInstance.fg.reset();
1331+
expect(minInput.value).toBe(0);
1332+
expect(maxInput.value).toBe(10);
1333+
}));
1334+
});
1335+
13171336
describe('slider as a custom form control', () => {
13181337
let fixture: ComponentFixture<SliderWithFormControl>;
13191338
let slider: MatSlider;
@@ -2024,6 +2043,28 @@ class RangeSliderWithTickMarks {
20242043
@ViewChild(MatSlider) slider: MatSlider;
20252044
}
20262045

2046+
@Component({
2047+
template: `
2048+
<div [formGroup]="fg" >
2049+
<mat-slider [min]="MIN" [max]="MAX" >
2050+
<input formControlName="min" matSliderStartThumb>
2051+
<input formControlName="max" matSliderEndThumb>
2052+
</mat-slider>
2053+
<div>
2054+
`,
2055+
styles: SLIDER_STYLES,
2056+
standalone: false,
2057+
})
2058+
class SliderWithFormGroup {
2059+
readonly MIN = 0;
2060+
readonly MAX = 10;
2061+
2062+
readonly fg = new FormGroup({
2063+
min: new FormControl<number | null>(null),
2064+
max: new FormControl<number | null>(null),
2065+
});
2066+
}
2067+
20272068
/** Clicks on the MatSlider at the coordinates corresponding to the given value. */
20282069
function setValueByClick(
20292070
slider: MatSlider,

src/material/slider/slider.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ export class MatSlider implements AfterViewInit, OnDestroy, _MatSlider {
140140
return this._min;
141141
}
142142
set min(v: number) {
143-
const min = isNaN(v) ? this._min : v;
143+
const min = v === undefined || v === null || isNaN(v) ? this._min : v;
144144
if (this._min !== min) {
145145
this._updateMin(min);
146146
}
@@ -216,7 +216,7 @@ export class MatSlider implements AfterViewInit, OnDestroy, _MatSlider {
216216
return this._max;
217217
}
218218
set max(v: number) {
219-
const max = isNaN(v) ? this._max : v;
219+
const max = v === undefined || v === null || isNaN(v) ? this._max : v;
220220
if (this._max !== max) {
221221
this._updateMax(max);
222222
}

0 commit comments

Comments
 (0)