11import React , { PureComponent } from 'react' ;
22import PropTypes from 'prop-types' ;
3- import { ViewPropTypes , View , FlatList } from 'react-native' ;
3+ import { Animated , ViewPropTypes , View , FlatList } from 'react-native' ;
44
55import { width } from '../../themes' ;
66
@@ -11,27 +11,53 @@ const calcGridDimension = numColumns => ({
1111 width : width / numColumns ,
1212} ) ;
1313
14- export default class ListGrid extends PureComponent {
14+ export default class GridList extends PureComponent {
1515 static propTypes = {
1616 data : PropTypes . array . isRequired ,
1717 numColumns : PropTypes . number . isRequired ,
1818 renderItem : PropTypes . func . isRequired ,
1919 showSeparator : PropTypes . bool ,
20+ showAnimation : PropTypes . bool ,
21+ animationDuration : PropTypes . number ,
2022 itemStyle : ViewPropTypes . style ,
2123 } ;
2224
2325 static defaultProps = {
2426 numColumns : 3 ,
2527 itemStyle : { } ,
2628 showSeparator : false ,
29+ showAnimation : false ,
30+ animationDuration : 500 ,
2731 } ;
2832
33+ componentWillMount ( ) {
34+ if ( this . props . showAnimation ) {
35+ const { data, numColumns, animationDuration } = this . props ;
36+ this . animatedValue = [ ] ;
37+
38+ this . stagger = data . map ( ( _ , index ) => {
39+ this . animatedValue [ index ] = new Animated . Value ( 0 ) ;
40+ return Animated . stagger ( 0 , [
41+ Animated . timing ( this . animatedValue [ index ] , {
42+ toValue : 1 ,
43+ duration : animationDuration * Math . ceil ( ( index + 1 ) / numColumns ) ,
44+ } ) ,
45+ ] ) ;
46+ } ) ;
47+ }
48+ }
2949 itemDimension = calcGridDimension ( this . props . numColumns ) ;
3050
3151 _keyExtractor = ( item , index ) => index ;
3252
3353 _renderItem = ( { item, index } ) => {
34- const { showSeparator, renderItem, numColumns, itemStyle } = this . props ;
54+ const {
55+ showAnimation,
56+ showSeparator,
57+ renderItem,
58+ numColumns,
59+ itemStyle,
60+ } = this . props ;
3561 let style = { } ;
3662 if ( showSeparator ) {
3763 const position = index % numColumns ;
@@ -43,7 +69,19 @@ export default class ListGrid extends PureComponent {
4369 style = styles . imageCenter ;
4470 }
4571 }
46- return (
72+
73+ return showAnimation ? (
74+ < Animated . View
75+ style = { [
76+ style ,
77+ this . itemDimension ,
78+ { opacity : this . animatedValue [ index ] } ,
79+ itemStyle ,
80+ ] }
81+ >
82+ { renderItem ( { item, index, stagger : this . stagger [ index ] } ) }
83+ </ Animated . View >
84+ ) : (
4785 < View style = { [ style , this . itemDimension , itemStyle ] } >
4886 { renderItem ( { item, index } ) }
4987 </ View >
0 commit comments