|
1 | 1 | /** |
2 | | - * Copyright IBM Corp. 2016, 2023 |
| 2 | + * Copyright IBM Corp. 2016, 2025 |
3 | 3 | * |
4 | 4 | * This source code is licensed under the Apache-2.0 license found in the |
5 | 5 | * LICENSE file in the root directory of this source tree. |
@@ -635,20 +635,16 @@ class Slider extends PureComponent<SliderProps> { |
635 | 635 | } |
636 | 636 |
|
637 | 637 | /** |
638 | | - * Takes a value and ensures it fits to the steps of the range |
639 | | - * @param value |
640 | | - * @returns value of the nearest step |
| 638 | + * Rounds a given value to the nearest step defined by the slider's `step` |
| 639 | + * prop. |
| 640 | + * |
| 641 | + * @param value - The value to adjust to the nearest step. Defaults to `0`. |
| 642 | + * @returns The value rounded to the precision determined by the step. |
641 | 643 | */ |
642 | | - nearestStepValue(value) { |
643 | | - const tempInput = document.createElement('input'); |
644 | | - |
645 | | - tempInput.type = 'range'; |
646 | | - tempInput.min = `${this.props.min}`; |
647 | | - tempInput.max = `${this.props.max}`; |
648 | | - tempInput.step = `${this.props.step}`; |
649 | | - tempInput.value = `${value}`; |
| 644 | + nearestStepValue(value = 0) { |
| 645 | + const decimals = (this.props.step?.toString().split('.')[1] || '').length; |
650 | 646 |
|
651 | | - return parseFloat(tempInput.value); |
| 647 | + return Number(value.toFixed(decimals)); |
652 | 648 | } |
653 | 649 |
|
654 | 650 | /** |
@@ -839,7 +835,11 @@ class Slider extends PureComponent<SliderProps> { |
839 | 835 | ? this.state.value |
840 | 836 | : this.state.valueUpper; |
841 | 837 | const { value, left } = this.calcValue({ |
842 | | - value: this.calcValueForDelta(currentValue, delta, this.props.step), |
| 838 | + value: this.calcValueForDelta( |
| 839 | + currentValue ?? this.props.min, |
| 840 | + delta, |
| 841 | + this.props.step |
| 842 | + ), |
843 | 843 | }); |
844 | 844 | this.setValueLeftForHandle(this.state.activeHandle, { |
845 | 845 | value: this.nearestStepValue(value), |
@@ -1111,21 +1111,22 @@ class Slider extends PureComponent<SliderProps> { |
1111 | 1111 | }; |
1112 | 1112 |
|
1113 | 1113 | /** |
1114 | | - * Given the current value, delta and step, calculate the new value. |
| 1114 | + * Calculates a new slider value based on the current value, a change delta, |
| 1115 | + * and a step. |
1115 | 1116 | * |
1116 | | - * @param {number} currentValue |
1117 | | - * Current value user is moving from. |
1118 | | - * @param {number} delta |
1119 | | - * Movement from the current value. Can be positive or negative. |
1120 | | - * @param {number} step |
1121 | | - * A value determining how much the value should increase/decrease by moving |
1122 | | - * the thumb by mouse. |
| 1117 | + * @param currentValue - The starting value from which the slider is moving. |
| 1118 | + * @param delta - The amount to adjust the current value by. |
| 1119 | + * @param step - The step. Defaults to `1`. |
| 1120 | + * @returns The new slider value, rounded to the same number of decimal places |
| 1121 | + * as the step. |
1123 | 1122 | */ |
1124 | | - calcValueForDelta = (currentValue, delta, step = 1) => { |
1125 | | - return ( |
1126 | | - (delta > 0 ? Math.floor(currentValue / step) * step : currentValue) + |
1127 | | - delta |
1128 | | - ); |
| 1123 | + calcValueForDelta = (currentValue: number, delta: number, step = 1) => { |
| 1124 | + const base = |
| 1125 | + delta > 0 ? Math.floor(currentValue / step) * step : currentValue; |
| 1126 | + const newValue = base + delta; |
| 1127 | + const decimals = (step.toString().split('.')[1] || '').length; |
| 1128 | + |
| 1129 | + return Number(newValue.toFixed(decimals)); |
1129 | 1130 | }; |
1130 | 1131 |
|
1131 | 1132 | /** |
|
0 commit comments