44 * Converted to Typescript on 14/07/2020.
55 * Converted to Functional component. on 21/09/2021
66 */
7- import React , { useEffect , useState } from 'react' ;
7+ import React , { useEffect , useMemo , useState } from 'react' ;
88import { Animated } from 'react-native' ;
99import { usePrevious } from 'react-use' ;
1010import EmptyDot from './EmptyDot' ;
11- import { getDotStyle , IDotStyle } from '../util/DotUtils' ;
11+ import { getDotStyle } from '../util/DotUtils' ;
1212
1313const Dot : React . FC < {
1414 idx : number ;
@@ -26,7 +26,7 @@ const Dot: React.FC<{
2626 maxPage : props . maxPage ,
2727 } )
2828 ) ;
29- const prevType = usePrevious < IDotStyle > ( type ) ;
29+ const prevType = usePrevious ( type ) ;
3030
3131 useEffect ( ( ) => {
3232 const nextType = getDotStyle ( {
@@ -60,47 +60,52 @@ const Dot: React.FC<{
6060 } ) . start ( ) ;
6161 } , [ animVal , animate , prevType , type ] ) ;
6262
63+ const animStyle = useMemo ( ( ) => {
64+ const size = animVal . interpolate ( {
65+ inputRange : [ 0 , 1 ] ,
66+ outputRange : [
67+ ( prevType ?. size || 3 ) * props . sizeRatio ,
68+ type . size * props . sizeRatio ,
69+ ] ,
70+ } ) ;
71+ return {
72+ width : size ,
73+ height : size ,
74+ borderRadius : animVal . interpolate ( {
75+ inputRange : [ 0 , 1 ] ,
76+ outputRange : [
77+ ( prevType ?. size || 3 ) * props . sizeRatio * 0.5 ,
78+ type . size * props . sizeRatio * 0.5 ,
79+ ] ,
80+ } ) ,
81+ opacity : animVal . interpolate ( {
82+ inputRange : [ 0 , 1 ] ,
83+ outputRange : [ prevType ?. opacity || 0.2 , type . opacity ] ,
84+ } ) ,
85+ } ;
86+ } , [
87+ animVal ,
88+ prevType ?. opacity ,
89+ prevType ?. size ,
90+ props . sizeRatio ,
91+ type . opacity ,
92+ type . size ,
93+ ] ) ;
94+
6395 if ( props . curPage < 3 ) {
6496 if ( props . idx >= 5 ) return < EmptyDot sizeRatio = { props . sizeRatio } /> ;
6597 } else if ( props . curPage < 4 ) {
6698 if ( props . idx > 5 ) return < EmptyDot sizeRatio = { props . sizeRatio } /> ;
6799 }
68100
69- const opacity = animVal . interpolate ( {
70- inputRange : [ 0 , 1 ] ,
71- outputRange : [ prevType ?. opacity || 0.2 , type . opacity ] ,
72- } ) ;
73-
74- const size = animVal . interpolate ( {
75- inputRange : [ 0 , 1 ] ,
76- outputRange : [
77- ( prevType ?. size || 3 ) * props . sizeRatio ,
78- type . size * props . sizeRatio ,
79- ] ,
80- } ) ;
81-
82- const borderRadius = animVal . interpolate ( {
83- inputRange : [ 0 , 1 ] ,
84- outputRange : [
85- ( prevType ?. size || 3 ) * props . sizeRatio * 0.5 ,
86- type . size * props . sizeRatio * 0.5 ,
87- ] ,
88- } ) ;
89- const { activeColor } = props ;
90-
91101 return (
92102 < Animated . View
93103 style = { [
94104 {
95- backgroundColor : activeColor ,
105+ backgroundColor : props . activeColor ,
96106 margin : 3 * props . sizeRatio ,
97107 } ,
98- {
99- width : size ,
100- height : size ,
101- borderRadius : borderRadius ,
102- opacity : opacity ,
103- } ,
108+ animStyle ,
104109 ] }
105110 />
106111 ) ;
0 commit comments