Skip to content

Commit

Permalink
chore(custom): 添加冲突之后的文件
Browse files Browse the repository at this point in the history
  • Loading branch information
SunshineH2 committed Aug 10, 2023
1 parent d819da4 commit f8bde97
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 13 deletions.
Binary file removed packages/.DS_Store
Binary file not shown.
25 changes: 12 additions & 13 deletions packages/react-native-rating/src/useSwipeRating.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { useEffect } from 'react';
import { Gesture } from 'react-native-gesture-handler';
import { runOnJS, useAnimatedStyle, useSharedValue } from 'react-native-reanimated';
import { runOnJS, useAnimatedGestureHandler, useAnimatedStyle, useSharedValue } from 'react-native-reanimated';

import { SwipeRatingProps } from './type';

Expand All @@ -13,7 +12,6 @@ export default function useSwipeRating({
ratingFillColor,
}: Pick<SwipeRatingProps, 'size' | 'count' | 'rating' | 'fractions' | 'onFinishRating'> & { ratingFillColor: string }) {
const translateX = useSharedValue(0);
const startPosition = useSharedValue(0);

const getCurrentRating = (translateX: number) => {
'worklet';
Expand All @@ -24,18 +22,19 @@ export default function useSwipeRating({
translateX.value = rating * size;
}, [rating, size, translateX]);

const gesture = Gesture.Pan()
.onStart(() => {
startPosition.value = translateX.value;
})
.onUpdate(e => {
const value = e.translationX + startPosition.value;
const handler = useAnimatedGestureHandler({
onStart(_, ctx: Record<string, number>) {
ctx.offsetX = translateX.value;
},
onActive(event, ctx) {
const value = event.translationX + ctx.offsetX;
translateX.value = value >= count * size ? count * size : value;
})
.onEnd(() => {
},
onEnd() {
const currentRating = getCurrentRating(translateX.value);
onFinishRating && runOnJS(onFinishRating)(currentRating);
});
},
});

const primaryViewStyle = useAnimatedStyle(() => {
return {
Expand All @@ -45,5 +44,5 @@ export default function useSwipeRating({
};
});

return { primaryViewStyle, gesture };
return { primaryViewStyle, handler };
}

0 comments on commit f8bde97

Please sign in to comment.