Skip to content

fix(slider): displayWith function never called with "null" #16707

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,7 @@ import {Component} from '@angular/core';
styleUrls: ['slider-formatting-example.css'],
})
export class SliderFormattingExample {
formatLabel(value: number | null) {
if (!value) {
return 0;
}

formatLabel(value: number) {
if (value >= 1000) {
return Math.round(value / 1000) + 'k';
}
Expand Down
6 changes: 1 addition & 5 deletions src/material/slider/slider.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1531,11 +1531,7 @@ class SliderWithThumbLabel { }
styles: [styles],
})
class SliderWithCustomThumbLabelFormatting {
displayWith(value: number | null) {
if (!value) {
return 0;
}

displayWith(value: number) {
if (value >= 1000) {
return (value / 1000) + 'k';
}
Expand Down
6 changes: 4 additions & 2 deletions src/material/slider/slider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ export class MatSlider extends _MatSliderMixinBase
* in the thumb label. Can be used to format very large number in order
* for them to fit into the slider thumb.
*/
@Input() displayWith: (value: number | null) => string | number;
@Input() displayWith: (value: number) => string | number;

/** Whether the slider is vertical. */
@Input()
Expand All @@ -287,7 +287,9 @@ export class MatSlider extends _MatSliderMixinBase
/** The value to be used for display purposes. */
get displayValue(): string | number {
if (this.displayWith) {
return this.displayWith(this.value);
// Value is never null but since setters and getters cannot have
// different types, the value getter is also typed to return null.
return this.displayWith(this.value!);
}

// Note that this could be improved further by rounding something like 0.999 to 1 or
Expand Down
2 changes: 1 addition & 1 deletion tools/public_api_guard/material/slider.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export declare class MatSlider extends _MatSliderMixinBase implements ControlVal
};
readonly change: EventEmitter<MatSliderChange>;
readonly displayValue: string | number;
displayWith: (value: number | null) => string | number;
displayWith: (value: number) => string | number;
readonly input: EventEmitter<MatSliderChange>;
invert: boolean;
max: number;
Expand Down