Skip to content
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

fix: calculatePercentage returns NAN when min,max,value are 0 #24136

Closed
wants to merge 1 commit into from
Closed
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
7 changes: 7 additions & 0 deletions src/material/slider/slider.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,13 @@ describe('MatSlider', () => {
expect(sliderInstance.percent).toBe(1);
},
);

it('should calculate percentage propertly when min,max,value are zero', () => {
sliderInstance.value = 0;
sliderInstance.min = 0;
sliderInstance.max = 0;
expect(sliderInstance.percent).toBe(1);
});
});

describe('slider with set value', () => {
Expand Down
6 changes: 6 additions & 0 deletions src/material/slider/slider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -874,6 +874,12 @@ export class MatSlider

/** Calculates the percentage of the slider that a value is. */
private _calculatePercentage(value: number | null) {
//when min,max,value are all zero, the fraction becomes NAN, because we divide 0/0
// so we have to explicitely return 1 in this case
if (value === 0 && this.min === 0 && this.max === 0) {
return 1;
}

return ((value || 0) - this.min) / (this.max - this.min);
}

Expand Down