Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/itchy-humans-greet.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@td-design/react-native': patch
---

fix: 修复slider滑动判断step的逻辑bug
2 changes: 1 addition & 1 deletion packages/react-native/src/slider/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ const Slider: FC<SliderProps> = props => {
} = props;
const KNOB_WIDTH = height;
const sliderRange = width - KNOB_WIDTH;
const oneStepValue = sliderRange / max;
const oneStepValue = Math.floor(sliderRange / (max - min)) || 1;

const { progressStyle, knobStyle, onGestureEvent, label } = useSlider({
min,
Expand Down
12 changes: 11 additions & 1 deletion packages/react-native/src/slider/useSlider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,18 @@ export default function useSlider({
translateX.value = clamp(event.translationX + ctx.offsetX, min * oneStepValue, max * oneStepValue);
},
onEnd() {
// 判断当前停留的位置处于第几步
const currentStep = translateX.value / oneStepValue;
// 取余数进行判断,是否超过一半
const remainder = currentStep % 1;
if (remainder >= 0.5) {
translateX.value = Math.ceil(currentStep) * oneStepValue;
} else {
translateX.value = Math.floor(currentStep) * oneStepValue;
}

if (onChange) {
runOnJS(onChange)(Number(label.value));
runOnJS(onChange)(translateX.value / oneStepValue);
}
},
});
Expand Down