Skip to content

Commit

Permalink
[@mantine/core] Slider: Fix incorrect min/max boundaries handling whe…
Browse files Browse the repository at this point in the history
…n step is larger than the difference between current value and min/max (#2656)

* [@mantine/core] Select: only use open/close callback when value changes

* [@mantine/core] Select: do not use early return

* [@mantine/core] Slider: fix numeric max and negative numbers

* [@mantine/core] Slider: return min val is value is min

* [@mantine/core] Menu - revert PR #2646

* [@mantine/core] Slider: dont return value lower than min or greater than max
  • Loading branch information
wes337 committed Oct 22, 2022
1 parent 15405e8 commit c2650b7
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
12 changes: 11 additions & 1 deletion src/mantine-core/src/Slider/Slider.story.tsx
Expand Up @@ -176,4 +176,14 @@ storiesOf('Slider', module)
/>
</div>
))
.add('Thumb size', () => <ThumbSize />);
.add('Thumb size', () => <ThumbSize />)
.add('Min and max numbers', () => (
<div style={{ padding: 40 }}>
<Slider labelAlwaysOn min={1} max={100} step={10} />
</div>
))
.add('Negative min number', () => (
<div style={{ padding: 40 }}>
<Slider labelAlwaysOn min={-100} max={10} step={10} />
</div>
));
Expand Up @@ -19,11 +19,11 @@ export function getChangeValue({
? value
: Math.min(Math.max(value, 0), containerWidth) / containerWidth;
const dx = left * (max - min);
const nextValue = (dx !== 0 ? Math.round(dx / step) * step : 0) + min;
const minIsNegative = min <= 0;
const nextValue =
dx !== 0 ? Math.round(dx / step) * step + (minIsNegative ? min : 0) : Math.min(min, 0);
const nextValueWithPrecision = precision ? Number(nextValue.toFixed(precision)) : nextValue;
const finalValue = Math.min(nextValueWithPrecision, max);

if (precision !== undefined) {
return Number(nextValue.toFixed(precision));
}

return nextValue;
return finalValue <= min ? min : finalValue;
}

0 comments on commit c2650b7

Please sign in to comment.