Skip to content

Commit

Permalink
fix: slider to range step values (#14460)
Browse files Browse the repository at this point in the history
Co-authored-by: Taylor Jones <tay1orjones@users.noreply.github.com>
  • Loading branch information
lee-chase and tay1orjones authored Aug 21, 2023
1 parent fa39b58 commit 1aab3b5
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions packages/react/src/components/Slider/Slider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,23 @@ export default class Slider extends PureComponent<SliderProps> {
return Math.max(min, Math.min(val, max));
}

/**
* Takes a value and ensures it fits to the steps of the range
* @param value
* @returns value of the nearest step
*/
nearestStepValue(value) {
const tempInput = document.createElement('input');

tempInput.type = 'range';
tempInput.min = `${this.props.min}`;
tempInput.max = `${this.props.max}`;
tempInput.step = `${this.props.step}`;
tempInput.value = `${value}`;

return parseFloat(tempInput.value);
}

/**
* Sets up "drag" event handlers and calls `this.onDrag` in case dragging
* started on somewhere other than the thumb without a corresponding "move"
Expand Down Expand Up @@ -534,7 +551,7 @@ export default class Slider extends PureComponent<SliderProps> {
}

const { value, left } = this.calcValue({ clientX });
this.setState({ value, left, isValid: true });
this.setState({ value: this.nearestStepValue(value), left, isValid: true });
};

/**
Expand Down Expand Up @@ -588,7 +605,7 @@ export default class Slider extends PureComponent<SliderProps> {
: this.state.value) + delta,
});

this.setState({ value, left, isValid: true });
this.setState({ value: this.nearestStepValue(value), left, isValid: true });
};

/**
Expand Down

0 comments on commit 1aab3b5

Please sign in to comment.