Skip to content

Commit

Permalink
[@mantine/core] RangeSlider: Add maxRange prop support (#2998)
Browse files Browse the repository at this point in the history
* [docs] Fix typo (#2997)

* MaxRange Property

Co-authored-by: Derek <derrreks@gmail.com>
  • Loading branch information
ultracodez and Derrreks committed Nov 23, 2022
1 parent 9febdd1 commit bf500e2
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -5,6 +5,7 @@
.idea/
.vscode/
.nova/
.gitpod.yml

# node
node_modules/
Expand Down
12 changes: 12 additions & 0 deletions src/mantine-core/src/Slider/RangeSlider/RangeSlider.tsx
Expand Up @@ -45,6 +45,9 @@ export interface RangeSliderProps
/** Minimal range interval */
minRange?: number;

/** Maximum range interval */
maxRange?: number;

/** Number by which value will be incremented/decremented with thumb drag and arrows */
step?: number;

Expand Down Expand Up @@ -141,6 +144,7 @@ export const RangeSlider = forwardRef<HTMLDivElement, RangeSliderProps>((props,
min,
max,
minRange,
maxRange,
step,
precision,
defaultValue,
Expand Down Expand Up @@ -206,6 +210,10 @@ export const RangeSlider = forwardRef<HTMLDivElement, RangeSliderProps>((props,
if (val > (max - (minRange - 0.000000001) || min)) {
clone[index] = valueRef.current[index];
}

if (clone[1] - val > maxRange) {
clone[1] = val + maxRange;
}
}

if (index === 1) {
Expand All @@ -216,6 +224,10 @@ export const RangeSlider = forwardRef<HTMLDivElement, RangeSliderProps>((props,
if (val < clone[0] + minRange) {
clone[index] = valueRef.current[index];
}

if (val - clone[0] > maxRange) {
clone[0] = val - maxRange;
}
}

_setValue(clone);
Expand Down
15 changes: 15 additions & 0 deletions src/mantine-core/src/Slider/Slider.story.tsx
Expand Up @@ -73,3 +73,18 @@ export function MinRangeWithNegativeValues() {
</div>
);
}

export function MinMaxSliderDistance() {
return (
<div style={{ maxWidth: 400, padding: 40 }}>
<RangeSlider
min={0}
max={100}
minRange={5}
maxRange={20}
step={0.5}
defaultValue={[0, 100]}
/>
</div>
);
}

0 comments on commit bf500e2

Please sign in to comment.