From 2c00a5fe978a841ce6121d76e567e49355d746b2 Mon Sep 17 00:00:00 2001 From: William Candillon Date: Thu, 4 Feb 2021 09:26:22 +0100 Subject: [PATCH] Support animatedProps and fix createAnimatedComponent typing (#1590) Add support for animatedProps and fix createAnimatedComponent typing --- react-native-reanimated-tests.tsx | 92 ++++++++++++++++++++++++++----- react-native-reanimated.d.ts | 36 +++++------- 2 files changed, 91 insertions(+), 37 deletions(-) diff --git a/react-native-reanimated-tests.tsx b/react-native-reanimated-tests.tsx index 73ac550b733d..14e4bb7e4112 100644 --- a/react-native-reanimated-tests.tsx +++ b/react-native-reanimated-tests.tsx @@ -1,11 +1,12 @@ /* eslint-disable */ -import React, { useState } from 'react'; -import { StyleSheet, Button, View } from 'react-native'; +import React, { useState, useCallback } from 'react'; +import { StyleSheet, Button, View, Image, FlatListProps } from 'react-native'; import { PanGestureHandler, PinchGestureHandlerGestureEvent, PinchGestureHandler, PanGestureHandlerGestureEvent, + FlatList, } from 'react-native-gesture-handler'; import Animated, { useSharedValue, @@ -35,6 +36,78 @@ import Animated, { useAnimatedProps, } from 'react-native-reanimated'; +class Path extends React.Component<{ fill?: string }> { + render() { + return null; + } +} + +type Item = { + id: number; +}; + +const AnimatedPath = Animated.createAnimatedComponent(Path); +const AnimatedImage = Animated.createAnimatedComponent(Image); +const AnimatedFlatList = Animated.createAnimatedComponent(FlatList); +const AnimatedTypedFlatList = Animated.createAnimatedComponent>(FlatList); + +function CreateAnimatedComponentTest1() { + const animatedProps = useAnimatedProps(() => ({ fill: 'blue' })); + return ( + + ) +} + +function CreateAnimatedComponentTest2() { + const animatedProps = useAnimatedProps(() => ({ fill2: 'blue' })); + return ( + // @ts-expect-error + + ) +} + +function CreateAnimatedComponentTest3() { + const animatedProps = useAnimatedProps(() => ({ pointerEvents: 'none' } as const)); + return ( + + + + ) +} + +function CreateAnimatedFlatList() { + const renderItem = useCallback( + ({item, index}: {item: Item[]; index: number}) => { + if (Math.random()) { + return null; + } + return ( + + + ); + }, + [ + ], + ); + return ( + <> + + null} + /> + + + ) +} + + const styles = StyleSheet.create({ container: { flex: 1, @@ -338,10 +411,7 @@ function CancelAnimationTest() { // withDelay function WithDelayTest() { const x = useSharedValue(0); - const gestureHandler = useAnimatedGestureHandler< - PanGestureHandlerGestureEvent, - { startX: number } - >({ + const gestureHandler = useAnimatedGestureHandler({ onStart: (_, _ctx) => { cancelAnimation(x); }, @@ -371,10 +441,7 @@ function WithDelayTest() { // withRepeat function WithRepeatTest() { const x = useSharedValue(0); - const gestureHandler = useAnimatedGestureHandler< - PanGestureHandlerGestureEvent, - { startX: number } - >({ + const gestureHandler = useAnimatedGestureHandler({ onStart: (_, _ctx) => { cancelAnimation(x); }, @@ -404,10 +471,7 @@ function WithRepeatTest() { // withSequence function WithSequenceTest() { const x = useSharedValue(0); - const gestureHandler = useAnimatedGestureHandler< - PanGestureHandlerGestureEvent, - { startX: number } - >({ + const gestureHandler = useAnimatedGestureHandler({ onStart: (_, _ctx) => { cancelAnimation(x); }, diff --git a/react-native-reanimated.d.ts b/react-native-reanimated.d.ts index c2689264d2dc..e30ce0755a9d 100644 --- a/react-native-reanimated.d.ts +++ b/react-native-reanimated.d.ts @@ -2,13 +2,7 @@ // TypeScript Version: 2.8 declare module 'react-native-reanimated' { - import { - ComponentClass, - ReactNode, - Component, - RefObject, - ComponentType, - } from 'react'; + import { ComponentClass, ReactNode, Component, RefObject, ComponentType, ComponentProps } from 'react'; import { ViewProps, TextProps, @@ -200,7 +194,7 @@ declare module 'react-native-reanimated' { }; export type AnimatedTransform = (AdaptTransforms)[]; - export type AnimateStyle = { + export type AnimateStyle = { [K in keyof S]: K extends 'transform' ? AnimatedTransform : (S[K] extends ReadonlyArray @@ -218,15 +212,12 @@ declare module 'react-native-reanimated' { }; export type AnimateProps< - S extends object, - P extends { - style?: StyleProp; - } + P extends object > = { [K in keyof P]: K extends 'style' - ? StyleProp> + ? StyleProp> : P[K] | AnimatedNode; - }; + } & { animatedProps?: AnimateProps

}; type CodeProps = { exec?: AnimatedNode; @@ -235,26 +226,25 @@ declare module 'react-native-reanimated' { }; // components - export class View extends Component> { + export class View extends Component> { getNode(): ReactNativeView; } - export class Text extends Component> { + export class Text extends Component> { getNode(): ReactNativeText; } - export class Image extends Component> { + export class Image extends Component> { getNode(): ReactNativeImage; } export class ScrollView extends Component< - AnimateProps + AnimateProps > { getNode(): ReactNativeScrollView; } export class Code extends Component {} - export function createAnimatedComponent< - S extends object, - P extends { style?: StyleProp } - >(component: ComponentType

): ComponentType>; - + export function createAnimatedComponent

( + component: ComponentClass

+ ): ComponentType>; + // classes export { AnimatedClock as Clock,