import React, { useCallback, useRef } from 'react'; import { View, StyleSheet, Animated, Pressable } from 'react-native'; import LottieView from 'lottie-react-native'; import { RatingWithActionType } from './Rating'; import { useMount } from 'react-use-native'; import { STAR_COUNT } from './constants'; const AnimatedLottieView = Animated.createAnimatedComponent(LottieView); export function ActionRating({ score = 0, onPressStar }: RatingWithActionType) { const animValueList = useRef( Array.from({ length: STAR_COUNT }, () => new Animated.Value(0)), ).current; // 초기 별 상태 설정 useMount(() => { if (score === 0) return; const initialScore = Math.floor(score); animValueList.forEach((value, i) => { value.setValue(i < initialScore ? 1 : 0); }); }); const handlePress = useCallback( (index: number) => { const newRating = index + 1; onPressStar(newRating); Animated.sequence([ // 1. 모든 별을 비우는 애니메이션 ...animValueList.map((value) => Animated.timing(value, { toValue: 0, duration: 1, useNativeDriver: true, }), ), // 2. 80ms 간격으로 선택한 별까지 채우는 애니메이션 Animated.stagger( 80, animValueList.slice(0, newRating).map((value) => Animated.timing(value, { toValue: 1, duration: 250, useNativeDriver: true, }), ), ), ]).start(); }, [onPressStar, animValueList], ); return ( {animValueList.map((value, index) => { return ( handlePress(index)}> ); })} ); } const styles = StyleSheet.create({ container: { flexDirection: 'row', alignItems: 'center', gap: 4, }, star: { width: 36, height: 36, }, });