Skip to content

Commit

Permalink
fix bug: slider cannot drag to max value (#714)
Browse files Browse the repository at this point in the history
  • Loading branch information
liuchao233 authored Nov 21, 2020
1 parent 569fa38 commit 7ab3876
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
3 changes: 2 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ export function isNotTouchEvent(e: React.TouchEvent) {
export function getClosestPoint(val: number, { marks, step, min, max }) {
const points = Object.keys(marks).map(parseFloat);
if (step !== null) {
const maxSteps = Math.floor((max - min) / step);
const baseNum = 10 ** getPrecision(step);
const maxSteps = Math.floor((max * baseNum - min * baseNum) / (step * baseNum));
const steps = Math.min((val - min) / step, maxSteps);
const closestStep = Math.round(steps) * step + min;
points.push(closestStep);
Expand Down
35 changes: 32 additions & 3 deletions tests/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ describe('utils', () => {
marks: { 0: 0, 30: 30, 60: 60 },
step: null,
min: 0,
max: 100
max: 100,
};

expect(utils.getClosestPoint(value, props)).toBe(30);
Expand All @@ -21,7 +21,7 @@ describe('utils', () => {
marks: { 0: 0, 30: 30, 60: 60 },
step: 3,
min: 0,
max: 100
max: 100,
};

expect(utils.getClosestPoint(value, props)).toBe(39);
Expand All @@ -33,10 +33,39 @@ describe('utils', () => {
marks: {},
step: 6,
min: 0,
max: 100
max: 100,
};

expect(utils.getClosestPoint(value, props)).toBe(96);
});

it('should return closest precision float value', () => {
expect(
utils.ensureValuePrecision(8151.23, {
marks: {},
step: 0.01,
min: 0.2,
max: 8151.23,
}),
).toBe(8151.23);

expect(
utils.ensureValuePrecision(0.2, {
marks: {},
step: 0.01,
min: 0.2,
max: 8151.23,
}),
).toBe(0.2);

expect(
utils.ensureValuePrecision(815.23, {
marks: {},
step: 0.01,
min: 0.2,
max: 8151.23,
}),
).toBe(815.23);
});
});
});

1 comment on commit 7ab3876

@vercel
Copy link

@vercel vercel bot commented on 7ab3876 Nov 21, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.