Skip to content

Commit

Permalink
Accept generic type in Animated.FlatList (software-mansion#2862)
Browse files Browse the repository at this point in the history
## Description

Reanimated exposes animated `FlatList` component under `Animated` namespace, but using it in a real world scenario is a bit cumbersome, since it doesn't accept a type parameter that is used to describe list items. 

Fixes software-mansion#2858

## Changes

- Updated type for `Animated.FlatList`
- Added additional type test for `Animated.FlatList`

## Screenshots / GIFs

### Before
<img width="823" alt="image" src="https://user-images.githubusercontent.com/6288237/150246605-620b768b-eefa-44b5-8ff5-8e9a1fd5dd27.png">

### After
<img width="573" alt="image" src="https://user-images.githubusercontent.com/6288237/150246662-d196bf29-2089-4f4d-887a-58416e624f88.png">

## Checklist

- [ ] Included code example that can be used to test this change
- [X] Updated TS types
- [X] Added TS types tests
- [ ] Added unit / integration tests
- [ ] Updated documentation
- [ ] Ensured that CI passes
  • Loading branch information
justblender committed Jan 24, 2022
1 parent 9f8a07f commit 58e587f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
20 changes: 19 additions & 1 deletion react-native-reanimated-tests.tsx
Expand Up @@ -3,6 +3,7 @@
/* eslint-disable @typescript-eslint/no-unused-vars */
import React, { useState, useCallback, forwardRef } from 'react';
import {
Text,
StyleSheet,
Button,
View,
Expand Down Expand Up @@ -102,7 +103,7 @@ function CreateAnimatedComponentTest3() {
);
}

function CreateAnimatedFlatList() {
function CreateAnimatedFlatListTest1() {
const renderItem = useCallback(
({ item, index }: { item: Item[]; index: number }) => {
if (Math.random()) {
Expand Down Expand Up @@ -130,6 +131,23 @@ function CreateAnimatedFlatList() {
);
}

function CreateAnimatedFlatListTest2() {
return (
<>
<Animated.FlatList<Item>
// @ts-expect-error
data={[{ foo: 1 }]}
// @ts-expect-error
renderItem={({ item, index }) => <View key={item.foo} />}
/>
<Animated.FlatList<Item>
data={[{ id: 1 }]}
renderItem={({ item, index }) => <View key={item.id} />}
/>
</>
);
}

function TestClassComponentRef() {
const animatedRef = useAnimatedRef<React.Component<ImageProps>>();
return <AnimatedImage ref={animatedRef} source={{}} />;
Expand Down
3 changes: 2 additions & 1 deletion react-native-reanimated.d.ts
Expand Up @@ -16,6 +16,7 @@ declare module 'react-native-reanimated' {
TextProps,
ImageProps,
ScrollViewProps,
FlatListProps,
StyleProp,
RegisteredStyle,
ViewStyle,
Expand Down Expand Up @@ -257,7 +258,7 @@ declare module 'react-native-reanimated' {
getNode(): ReactNativeScrollView;
}
export class Code extends Component<CodeProps> {}
export class FlatList extends Component<AnimateProps<FlatList>> {
export class FlatList<T> extends Component<AnimateProps<FlatListProps<T>>> {
itemLayoutAnimation: ILayoutAnimationBuilder;
getNode(): ReactNativeFlatList;
}
Expand Down

0 comments on commit 58e587f

Please sign in to comment.