Skip to content

Commit 25d2edf

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 25d2edf

File tree

3 files changed

+54
-4
lines changed

3 files changed

+54
-4
lines changed

src/material/slider/slider-input.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,10 @@ export class MatSliderThumb implements _MatSliderThumb, OnDestroy, ControlValueA
9696
return numberAttribute(this._hostElement.value, 0);
9797
}
9898
set value(value: number) {
99-
value = isNaN(value) ? 0 : value;
99+
value = value && isNaN(value) ? 0 : value;
100+
if (value === null || value === undefined) {
101+
value = this._getDefaultValue();
102+
}
100103
const stringValue = value + '';
101104
if (!this._hasSetInitialValue) {
102105
this._initialValue = stringValue;

src/material/slider/slider.spec.ts

+48-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';
@@ -1706,6 +1706,31 @@ describe('MatSlider', () => {
17061706
expect(tickEls.inactive.length).toBe(100);
17071707
});
17081708
});
1709+
1710+
describe('slider with form group', () => {
1711+
let fixture: ComponentFixture<SliderWithFormGroup>;
1712+
let slider: MatSlider;
1713+
let minInput: MatSliderThumb;
1714+
let maxInput: MatSliderThumb;
1715+
1716+
beforeEach(waitForAsync(() => {
1717+
fixture = createComponent(SliderWithFormGroup);
1718+
fixture.detectChanges();
1719+
const sliderDebugElement = fixture.debugElement.query(By.directive(MatSlider));
1720+
slider = sliderDebugElement.componentInstance;
1721+
minInput = slider._getInput(_MatThumb.START) as MatSliderRangeThumb;
1722+
maxInput = slider._getInput(_MatThumb.END) as MatSliderRangeThumb;
1723+
}));
1724+
1725+
it('should reset to initial min-max value when form reset is done', fakeAsync(() => {
1726+
expect(minInput.value).toBe(0);
1727+
expect(maxInput.value).toBe(10);
1728+
slideToValue(slider, minInput, 20);
1729+
fixture.componentInstance.fg.reset();
1730+
expect(minInput.value).toBe(0);
1731+
expect(maxInput.value).toBe(10);
1732+
}));
1733+
});
17091734
});
17101735

17111736
const SLIDER_STYLES = ['.mat-mdc-slider { width: 300px; }'];
@@ -2024,6 +2049,28 @@ class RangeSliderWithTickMarks {
20242049
@ViewChild(MatSlider) slider: MatSlider;
20252050
}
20262051

2052+
@Component({
2053+
template: `
2054+
<div [formGroup]="fg" >
2055+
<mat-slider [min]="MIN" [max]="MAX" >
2056+
<input formControlName="min" matSliderStartThumb>
2057+
<input formControlName="max" matSliderEndThumb>
2058+
</mat-slider>
2059+
<div>
2060+
`,
2061+
styles: SLIDER_STYLES,
2062+
standalone: false,
2063+
})
2064+
class SliderWithFormGroup {
2065+
readonly MIN = 0;
2066+
readonly MAX = 10;
2067+
2068+
readonly fg = new FormGroup({
2069+
min: new FormControl<number | null>(null),
2070+
max: new FormControl<number | null>(null),
2071+
});
2072+
}
2073+
20272074
/** Clicks on the MatSlider at the coordinates corresponding to the given value. */
20282075
function setValueByClick(
20292076
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 || 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 || isNaN(v) ? this._max : v;
220220
if (this._max !== max) {
221221
this._updateMax(max);
222222
}

0 commit comments

Comments
 (0)