@@ -12,6 +12,7 @@ import {
1212 TouchableOpacityProps ,
1313 ViewStyle ,
1414 LayoutChangeEvent ,
15+ useAnimatedValue ,
1516} from 'react-native' ;
1617
1718export type ThumbnailItem = {
@@ -26,7 +27,7 @@ export type ThumbnailItemIndex = {
2627
2728export type ThumbnailSelectorProps = {
2829 thumbnails : ThumbnailItem [ ] ;
29- toggle ?: ( func : ( ) => Promise < Animated . EndResult > ) => void ;
30+ toggle ?: ( func : ( ) => void ) => void ;
3031 renderThumbnail ?: (
3132 item : ThumbnailItem ,
3233 index : number ,
@@ -84,25 +85,26 @@ const ThumbnailSelector: React.FunctionComponent<ThumbnailSelectorProps> = ({
8485 animatedViewTestID = 'ThumbnailSelector' ,
8586} ) => {
8687 const window = useWindowDimensions ( ) ;
87- const [ itemIndex , setItemIndex ] = React . useState ( initialIndex ) ;
88- const [ animViewHeight , setAnimViewHeight ] = React . useState ( 0 ) ;
89- const animatedValue = React . useRef ( new Animated . Value ( 0 ) ) ;
90- const toValue = React . useRef ( animationConfig . toValue ) ;
88+ const animatedValue = useAnimatedValue (
89+ Number ( animationConfig . toValue ) ,
90+ animationConfig ,
91+ ) ;
92+ const [ itemIndex , setItemIndex ] = React . useState < number > ( initialIndex ) ;
93+ const [ animViewHeight , setAnimViewHeight ] = React . useState < number > ( 0 ) ;
94+ const [ hasCalledToggle , setHasCalledToggle ] = React . useState < boolean > ( false ) ;
9195
92- function _toggle ( ) : Promise < Animated . EndResult > {
93- if ( toValue . current ) {
94- animationConfig . toValue = 0 ;
95- } else {
96- animationConfig . toValue = 1 ;
97- }
98- toValue . current = animationConfig . toValue ;
99- return new Promise < Animated . EndResult > ( resolve => {
100- Animated . spring ( animatedValue . current , animationConfig ) . start ( resolve ) ;
101- } ) ;
96+ function _toggle ( ) : void {
97+ const value = Number ( JSON . stringify ( animatedValue ) ) ;
98+ const config : Animated . SpringAnimationConfig = {
99+ ...animationConfig ,
100+ toValue : value == 1 ? 0 : 1 ,
101+ } ;
102+ Animated . spring ( animatedValue , config ) . start ( ) ;
102103 }
103104
104- if ( toggle ) {
105+ if ( toggle != undefined && ! hasCalledToggle ) {
105106 toggle ( _toggle ) ;
107+ setHasCalledToggle ( true ) ;
106108 }
107109
108110 function _renderItem ( obj : ThumbnailItemIndex ) : React . JSX . Element {
@@ -158,7 +160,7 @@ const ThumbnailSelector: React.FunctionComponent<ThumbnailSelectorProps> = ({
158160 {
159161 transform : [
160162 {
161- translateY : animatedValue . current . interpolate ( {
163+ translateY : animatedValue . interpolate ( {
162164 inputRange : [ 0 , 1 ] ,
163165 outputRange : [ start , end ] ,
164166 } ) ,
0 commit comments