Skip to content

Commit

Permalink
fix: 修复slider滑动判断step的逻辑bug
Browse files Browse the repository at this point in the history
  • Loading branch information
chj-damon committed Mar 22, 2024
1 parent cfad6ff commit 9ea47b1
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
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

0 comments on commit 9ea47b1

Please sign in to comment.