diff --git a/packages/react-core/src/components/Slider/Slider.tsx b/packages/react-core/src/components/Slider/Slider.tsx index 5284d59279d..be650458e69 100644 --- a/packages/react-core/src/components/Slider/Slider.tsx +++ b/packages/react-core/src/components/Slider/Slider.tsx @@ -215,12 +215,15 @@ export const Slider: React.FunctionComponent = ({ /* If custom steps are discrete, snap to closest step value */ if (!areCustomStepsContinuous && customSteps) { - const stepIndex = customSteps.findIndex(stepObj => stepObj.value >= newPercentage); - if (customSteps[stepIndex].value === newPercentage) { + const numSteps = customSteps.length - 1; + const percentagePerStep = 100 / numSteps; + const customStepPercentage = newPercentage / percentagePerStep; + const stepIndex = customSteps.findIndex(stepObj => stepObj.value >= customStepPercentage); + if (customSteps[stepIndex].value === customStepPercentage) { snapValue = customSteps[stepIndex].value; } else { const midpoint = (customSteps[stepIndex].value + customSteps[stepIndex - 1].value) / 2; - if (midpoint > newPercentage) { + if (midpoint > customStepPercentage) { snapValue = customSteps[stepIndex - 1].value; } else { snapValue = customSteps[stepIndex].value; @@ -306,10 +309,11 @@ export const Slider: React.FunctionComponent = ({ } }; + const getStepValue = (val: number, min: number, max: number) => ((val - min) * 100) / (max - min); const buildSteps = () => { const builtSteps = []; for (let i = min; i <= max; i = i + step) { - const stepValue = ((i - min) * 100) / (max - min); + const stepValue = getStepValue(i, min, max); // If we boundaries but not ticks just generate the needed steps // so that we don't pullute them DOM with empty divs @@ -340,15 +344,21 @@ export const Slider: React.FunctionComponent = ({ {customSteps && ( )} {!customSteps && (showTicks || showBoundaries) && (