Skip to content

fix(material/slider): Ticks updated wrongly if the max property 0 #24218

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
merged 2 commits into from
Jan 28, 2022
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
20 changes: 20 additions & 0 deletions src/material/slider/slider.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,26 @@ describe('MatSlider', () => {
expect(sliderInstance.percent).toBe(1);
},
);
it('should properly update ticks when max value changed to 0', () => {
testComponent.min = 0;
testComponent.max = 100;
fixture.detectChanges();

dispatchMouseenterEvent(sliderNativeElement);
fixture.detectChanges();

expect(ticksElement.style.backgroundSize).toBe('6% 2px');
expect(ticksElement.style.transform).toContain('translateX(3%)');

testComponent.max = 0;
fixture.detectChanges();

dispatchMouseenterEvent(sliderNativeElement);
fixture.detectChanges();

expect(ticksElement.style.backgroundSize).toBe('0% 2px');
expect(ticksElement.style.transform).toContain('translateX(0%)');
});
});

describe('slider with set value', () => {
Expand Down
14 changes: 11 additions & 3 deletions src/material/slider/slider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -860,15 +860,17 @@ export class MatSlider
return;
}

let tickIntervalPercent: number;
if (this.tickInterval == 'auto') {
let trackSize = this.vertical ? this._sliderDimensions.height : this._sliderDimensions.width;
let pixelsPerStep = (trackSize * this.step) / (this.max - this.min);
let stepsPerTick = Math.ceil(MIN_AUTO_TICK_SEPARATION / pixelsPerStep);
let pixelsPerTick = stepsPerTick * this.step;
this._tickIntervalPercent = pixelsPerTick / trackSize;
tickIntervalPercent = pixelsPerTick / trackSize;
} else {
this._tickIntervalPercent = (this.tickInterval * this.step) / (this.max - this.min);
tickIntervalPercent = (this.tickInterval * this.step) / (this.max - this.min);
}
this._tickIntervalPercent = isSafeNumber(tickIntervalPercent) ? tickIntervalPercent : 0;
}

/** Creates a slider change object from the specified value. */
Expand All @@ -883,7 +885,8 @@ export class MatSlider

/** Calculates the percentage of the slider that a value is. */
private _calculatePercentage(value: number | null) {
return ((value || 0) - this.min) / (this.max - this.min);
const percentage = ((value || 0) - this.min) / (this.max - this.min);
return isSafeNumber(percentage) ? percentage : 0;
}

/** Calculates the value a percentage of the slider corresponds to. */
Expand Down Expand Up @@ -954,6 +957,11 @@ export class MatSlider
}
}

/** Checks if number is safe for calculation */
function isSafeNumber(value: number) {
return !isNaN(value) && isFinite(value);
}

/** Returns whether an event is a touch event. */
function isTouchEvent(event: MouseEvent | TouchEvent): event is TouchEvent {
// This function is called for every pixel that the user has dragged so we need it to be
Expand Down