Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
fix(material/slider): make value non-nullable (#22912)
Makes the value of the slider a `number`, rather than `number | null` in order to make it easier to use.

Fixes #22444.

(cherry picked from commit 71afc46)
  • Loading branch information
crisbeto authored and wagnermaciel committed Jun 10, 2021
1 parent db855c2 commit 383f7b6
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 11 deletions.
13 changes: 4 additions & 9 deletions src/material/slider/slider.ts
Expand Up @@ -190,11 +190,6 @@ export class MatSlider extends _MatSliderMixinBase
get min(): number { return this._min; }
set min(v: number) {
this._min = coerceNumberProperty(v, this._min);

// If the value wasn't explicitly set by the user, set it to the min.
if (this._value === null) {
this.value = this._min;
}
this._percent = this._calculatePercentage(this._value);

// Since this also modifies the percentage, we need to let the change detection know.
Expand Down Expand Up @@ -242,16 +237,16 @@ export class MatSlider extends _MatSliderMixinBase

/** Value of the slider. */
@Input()
get value(): number | null {
get value(): number {
// If the value needs to be read and it is still uninitialized, initialize it to the min.
if (this._value === null) {
this.value = this._min;
}
return this._value;
return this._value as number;
}
set value(v: number | null) {
set value(v: number) {
if (v !== this._value) {
let value = coerceNumberProperty(v);
let value = coerceNumberProperty(v, 0);

// While incrementing by a decimal we can end up with values like 33.300000000000004.
// Truncate it to ensure that it matches the label and to make it easier to work with.
Expand Down
4 changes: 2 additions & 2 deletions tools/public_api_guard/material/slider.d.ts
Expand Up @@ -23,8 +23,8 @@ export declare class MatSlider extends _MatSliderMixinBase implements ControlVal
set thumbLabel(value: boolean);
get tickInterval(): 'auto' | number;
set tickInterval(value: 'auto' | number);
get value(): number | null;
set value(v: number | null);
get value(): number;
set value(v: number);
readonly valueChange: EventEmitter<number | null>;
valueText: string;
get vertical(): boolean;
Expand Down

0 comments on commit 383f7b6

Please sign in to comment.