Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Layout animation for FlatList #2674

Merged
merged 2 commits into from
Dec 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions react-native-reanimated.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ declare module 'react-native-reanimated' {
Text as ReactNativeText,
Image as ReactNativeImage,
ScrollView as ReactNativeScrollView,
FlatList as ReactNativeFlatList,
NativeScrollEvent,
NativeSyntheticEvent,
ColorValue,
Expand Down Expand Up @@ -256,6 +257,10 @@ declare module 'react-native-reanimated' {
getNode(): ReactNativeScrollView;
}
export class Code extends Component<CodeProps> {}
export class FlatList extends Component<AnimateProps<FlatList>> {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@piaskowyk this type definition is missing the generic type mentioned in issue 2769. Should probably be defined as:

export class FlatList<T = any> extends Component<AnimateProps<FlatListProps<T>>> {
  itemLayoutAnimation: ILayoutAnimationBuilder;
  getNode(): ReactNativeFlatList;
}

itemLayoutAnimation: ILayoutAnimationBuilder;
getNode(): ReactNativeFlatList;
}

type Options<P> = {
setNativeProps: (ref: any, props: P) => void;
Expand Down
8 changes: 3 additions & 5 deletions src/Animated.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
import { Image, ScrollView, Text, View, LogBox } from 'react-native';
import { LogBox } from 'react-native';
import createAnimatedComponent from './createAnimatedComponent';
import {
addWhitelistedNativeProps,
addWhitelistedUIProps,
} from './ConfigHelper';
import * as reanimated1 from './reanimated1';
import ReanimatedComponents from './reanimated2/component';

const Animated = {
// components
View: createAnimatedComponent(View),
Text: createAnimatedComponent(Text),
Image: createAnimatedComponent(Image),
ScrollView: createAnimatedComponent(ScrollView),
...ReanimatedComponents,
createAnimatedComponent,
// configuration
addWhitelistedNativeProps,
Expand Down
4 changes: 2 additions & 2 deletions src/createAnimatedComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import { initialUpdaterRun } from './reanimated2/animation';
import {
BaseAnimationBuilder,
EntryExitAnimationFunction,
LayoutAnimationFunction,
ILayoutAnimationBuilder,
} from './reanimated2/layoutReanimation';
import { SharedValue, StyleProps } from './reanimated2/commonTypes';
import {
Expand Down Expand Up @@ -133,7 +133,7 @@ export type AnimatedComponentProps<P extends Record<string, unknown>> = P & {
animatedStyle?: StyleProps;
layout?:
| BaseAnimationBuilder
| LayoutAnimationFunction
| ILayoutAnimationBuilder
| typeof BaseAnimationBuilder;
entering?:
| BaseAnimationBuilder
Expand Down
44 changes: 44 additions & 0 deletions src/reanimated2/component/FlatList.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import React from 'react';
import { FlatList, FlatListProps, LayoutChangeEvent } from 'react-native';
import WrappedComponents from './WrappedComponents';
import createAnimatedComponent from '../../createAnimatedComponent';
import { ILayoutAnimationBuilder } from '../layoutReanimation/animationBuilder/commonTypes';

const AnimatedFlatList = createAnimatedComponent(FlatList as any) as any;

const createCellRenderer = (itemLayoutAnimation?: ILayoutAnimationBuilder) => {
const cellRenderer: React.FC<{
onLayout: (event: LayoutChangeEvent) => void;
}> = (props) => {
return (
<WrappedComponents.View
layout={itemLayoutAnimation}
onLayout={props.onLayout}>
{props.children}
</WrappedComponents.View>
);
};

return cellRenderer;
};

interface ReanimatedFlatlistProps<ItemT> extends FlatListProps<ItemT> {
itemLayoutAnimation?: ILayoutAnimationBuilder;
}

type ReanimatedFlatListFC<T = any> = React.FC<ReanimatedFlatlistProps<T>>;

const ReanimatedFlatlist: ReanimatedFlatListFC = ({
itemLayoutAnimation,
...restProps
}) => {
const cellRenderer = React.useMemo(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

any reason to use memo here instead of just a simple method? Seems like updating layout animation won't have any effect given memo has empty array as dependencies now

() => createCellRenderer(itemLayoutAnimation),
[]
);
return (
<AnimatedFlatList {...restProps} CellRendererComponent={cellRenderer} />
);
};

export default ReanimatedFlatlist;
11 changes: 11 additions & 0 deletions src/reanimated2/component/WrappedComponents.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Image, ScrollView, Text, View } from 'react-native';
import createAnimatedComponent from '../../createAnimatedComponent';

const WrappedComponents = {
View: createAnimatedComponent(View),
Text: createAnimatedComponent(Text as any),
Image: createAnimatedComponent(Image as any),
ScrollView: createAnimatedComponent(ScrollView),
};

export default WrappedComponents;
9 changes: 9 additions & 0 deletions src/reanimated2/component/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import ReanimatedFlatlist from './FlatList';
import WrappedComponents from './WrappedComponents';

const ReanimatedComponents = {
...WrappedComponents,
FlatList: ReanimatedFlatlist,
};

export default ReanimatedComponents;