|
1 | 1 | import React, { useState, useEffect } from 'react';
|
2 | 2 |
|
3 |
| -function exponentialScale(displayValue, min, max) { |
| 3 | +function exponentialScale(displayValue, min, max, step = 1) { |
4 | 4 | const scale = max / Math.log(max + 1);
|
5 | 5 | let value = Math.exp(displayValue / scale) - 1;
|
6 | 6 |
|
7 |
| - // Dynamically generate round numbers based on max value |
| 7 | + // If step is provided and greater than 1, ensure we only return values that are multiples of step |
| 8 | + if (step > 1) { |
| 9 | + // Round to the nearest step |
| 10 | + value = Math.round(value / step) * step; |
| 11 | + return Math.min(Math.max(value, min), max); |
| 12 | + } |
| 13 | + |
| 14 | + // Dynamically generate round numbers based on max value (only when step is 1) |
8 | 15 | const roundNumbers = [];
|
9 | 16 | const magnitude = Math.floor(Math.log10(max));
|
10 | 17 | const base = Math.pow(10, magnitude);
|
@@ -54,7 +61,12 @@ export default function Slider({ min, max, step, defaultValue, onChange, noExpon
|
54 | 61 | let newValue = parseFloat(parseFloat(event.target.value).toFixed(2));
|
55 | 62 | if (isExponential) {
|
56 | 63 | // Convert from visual scale back to actual value and snap to round numbers
|
57 |
| - newValue = exponentialScale(newValue, min, max); |
| 64 | + newValue = exponentialScale(newValue, min, max, step); |
| 65 | + } else { |
| 66 | + // For non-exponential sliders, ensure step compliance |
| 67 | + if (step > 1) { |
| 68 | + newValue = Math.round(newValue / step) * step; |
| 69 | + } |
58 | 70 | }
|
59 | 71 | // Ensure value doesn't go below min
|
60 | 72 | newValue = Math.max(min, newValue);
|
|
0 commit comments