From c2650b7655e1646609406f4f824f15fc08a258ee Mon Sep 17 00:00:00 2001 From: Wesley Moses Date: Sat, 22 Oct 2022 12:30:42 +0300 Subject: [PATCH] [@mantine/core] Slider: Fix incorrect min/max boundaries handling when 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 --- src/mantine-core/src/Slider/Slider.story.tsx | 12 +++++++++++- .../utils/get-change-value/get-change-value.ts | 12 ++++++------ 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/src/mantine-core/src/Slider/Slider.story.tsx b/src/mantine-core/src/Slider/Slider.story.tsx index 111b289d2d2..8d695a1caa2 100644 --- a/src/mantine-core/src/Slider/Slider.story.tsx +++ b/src/mantine-core/src/Slider/Slider.story.tsx @@ -176,4 +176,14 @@ storiesOf('Slider', module) /> )) - .add('Thumb size', () => ); + .add('Thumb size', () => ) + .add('Min and max numbers', () => ( +
+ +
+ )) + .add('Negative min number', () => ( +
+ +
+ )); diff --git a/src/mantine-core/src/Slider/utils/get-change-value/get-change-value.ts b/src/mantine-core/src/Slider/utils/get-change-value/get-change-value.ts index cca06ce52ec..a18af5f0561 100644 --- a/src/mantine-core/src/Slider/utils/get-change-value/get-change-value.ts +++ b/src/mantine-core/src/Slider/utils/get-change-value/get-change-value.ts @@ -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; }