import React,{JSXElementConstructor} from 'react'; import { View,Text, TouchableOpacity, Animated } from 'react-native'; import { FilledStar, EmptyStar } from './includes/lib'; interface RatingProps { /** * Max number of stars to show (Default 5) */ maxStars?: number, /** * Rating stars to show (Default 0) */ stars?: number, /** * Size of the stars */ size?: number, /** * Color of the stars */ color?: string, /** * If bordered stars are to be shown */ bordered?: boolean, } const Rating: React.FC = ({ maxStars, stars, size, color, bordered }) => { const _number = maxStars ?? 5; const _stars = stars ?? 0; const _size = size ?? 25; const _color = color ?? "#FFDF00"; const _bordered = bordered ?? false; return( { Array(_number).fill('\u4215485').map((item, index) => { if(_bordered) { return ( ) }else{ if(_stars >= (index+1)){ return }else{ return } } }) } ) } interface RatingInputProps { /** * State variable that will store the rating */ rating: number, /** * Function to set the rating which will be stored in your local state */ setRating: React.Dispatch>, /** * Max number of stars to show (Default 5) */ maxStars?: number, /** * Rating stars to show (Default 0) */ stars?: number, /** * Size of the stars */ size?: number, /** * Color of the stars */ color?: string, /** * If bordered stars are to be shown */ bordered?: boolean, /** * Callback Function to call after the rating is given */ onRating?: () => void, } const RatingInput: React.FC = ({ rating, setRating, maxStars, size, color, bordered, onRating = () => {} }) => { const _number = maxStars ?? 5; const _size = size ?? 25; const _color = color ?? "#FFDF00"; const _bordered = bordered ?? false; const [_firstRender, setFirstRender] = React.useState(true); const _anim = React.useRef(new Animated.Value(1)).current; const _up = () => { Animated.timing(_anim, { toValue: 1.5, duration: 150, useNativeDriver: true }).start(({finished}) => { _down(); }); }; const _down = () => { Animated.timing(_anim, { toValue: 1, duration: 50, useNativeDriver: true }).start(); }; React.useEffect(() => { if(!_firstRender){ onRating(); } }, [rating]) return( { Array(_number).fill('\u4215488').map((item, index) => { if(_bordered) { return( { _up(); setRating(index+1); setFirstRender(false); } } style={(rating == (index+1)) ? {transform: [{scale: _anim}]} : null} > ) }else{ if(rating < (index+1)){ return( { _up(); setRating(index+1); setFirstRender(false); } } style={(rating == (index+1)) ? {transform: [{scale: _anim}]} : null} > ) }else{ return( { _up() setRating(index+1); setFirstRender(false); } } style={(rating == (index+1)) ? {transform: [{scale: _anim}]} : null} > ) } } }) } ) } export { Rating, RatingInput };