From 383f7b650b46172b8ac4f91813cd485db8dba84f Mon Sep 17 00:00:00 2001 From: Kristiyan Kostadinov Date: Thu, 10 Jun 2021 14:44:11 +0200 Subject: [PATCH] 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 71afc4616baafdb46571a7b58d2037c18cd75317) --- src/material/slider/slider.ts | 13 ++++--------- tools/public_api_guard/material/slider.d.ts | 4 ++-- 2 files changed, 6 insertions(+), 11 deletions(-) diff --git a/src/material/slider/slider.ts b/src/material/slider/slider.ts index 6a2bf4dee9f3..2a66edc9c471 100644 --- a/src/material/slider/slider.ts +++ b/src/material/slider/slider.ts @@ -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. @@ -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. diff --git a/tools/public_api_guard/material/slider.d.ts b/tools/public_api_guard/material/slider.d.ts index 725bbb87bfe8..3cb115319061 100644 --- a/tools/public_api_guard/material/slider.d.ts +++ b/tools/public_api_guard/material/slider.d.ts @@ -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; valueText: string; get vertical(): boolean;