Skip to content

Commit 37ec78e

Browse files
authored
Merge pull request #843 from thundersdata-frontend/rn-issue
fix: 修复slider滑动判断step的逻辑bug
2 parents b4b17c5 + 9ea47b1 commit 37ec78e

File tree

3 files changed

+17
-2
lines changed

3 files changed

+17
-2
lines changed

.changeset/itchy-humans-greet.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@td-design/react-native': patch
3+
---
4+
5+
fix: 修复slider滑动判断step的逻辑bug

packages/react-native/src/slider/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ const Slider: FC<SliderProps> = props => {
6464
} = props;
6565
const KNOB_WIDTH = height;
6666
const sliderRange = width - KNOB_WIDTH;
67-
const oneStepValue = sliderRange / max;
67+
const oneStepValue = Math.floor(sliderRange / (max - min)) || 1;
6868

6969
const { progressStyle, knobStyle, onGestureEvent, label } = useSlider({
7070
min,

packages/react-native/src/slider/useSlider.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,18 @@ export default function useSlider({
4646
translateX.value = clamp(event.translationX + ctx.offsetX, min * oneStepValue, max * oneStepValue);
4747
},
4848
onEnd() {
49+
// 判断当前停留的位置处于第几步
50+
const currentStep = translateX.value / oneStepValue;
51+
// 取余数进行判断,是否超过一半
52+
const remainder = currentStep % 1;
53+
if (remainder >= 0.5) {
54+
translateX.value = Math.ceil(currentStep) * oneStepValue;
55+
} else {
56+
translateX.value = Math.floor(currentStep) * oneStepValue;
57+
}
58+
4959
if (onChange) {
50-
runOnJS(onChange)(Number(label.value));
60+
runOnJS(onChange)(translateX.value / oneStepValue);
5161
}
5262
},
5363
});

0 commit comments

Comments
 (0)