@@ -9,14 +9,27 @@ export default class GridList extends PureComponent {
99 static propTypes = {
1010 animationDuration : PropTypes . number ,
1111 animationInitialBackgroundColor : PropTypes . string ,
12- data : PropTypes . array . isRequired ,
1312 itemStyle : ViewPropTypes . style ,
1413 numColumns : PropTypes . number . isRequired ,
15- renderItem : PropTypes . func . isRequired ,
1614 separatorBorderColor : PropTypes . string ,
1715 separatorBorderWidth : PropTypes . number ,
1816 showAnimation : PropTypes . bool ,
1917 showSeparator : PropTypes . bool ,
18+
19+ data : PropTypes . array ,
20+ renderItem : PropTypes . func ,
21+
22+ // Only is allowed children or data not both
23+ children ( props , propName ) {
24+ const { data } = props ;
25+ if ( ! props [ propName ] && data && data . length === 0 ) {
26+ return new Error ( 'Invalid props, `data` or `children` is required' ) ;
27+ }
28+ if ( data && data . length !== 0 && ! props . renderItem ) {
29+ return new Error ( 'Invalid props, `renderItem` is required' ) ;
30+ }
31+ return undefined ;
32+ } ,
2033 } ;
2134
2235 static defaultProps = {
@@ -53,17 +66,30 @@ export default class GridList extends PureComponent {
5366 }
5467 this . styles = generateStyles ( stylesOptions ) ;
5568
69+ this . setup ( this . props ) ;
5670 this . animate ( ) ;
5771 }
58- componentWillUpdate ( ) {
72+ componentWillUpdate ( nextProps ) {
73+ this . setup ( nextProps ) ;
5974 this . animate ( ) ;
6075 }
76+
77+ setup = ( { children, data, renderItem } ) => {
78+ if ( children ) {
79+ this . _data = children ;
80+ this . _renderItem = this . renderChildren ;
81+ } else if ( data ) {
82+ this . _data = data ;
83+ this . _renderItem = renderItem ;
84+ }
85+ } ;
86+
6187 animate ( ) {
6288 if ( this . props . showAnimation ) {
63- const { data , numColumns, animationDuration } = this . props ;
89+ const { numColumns, animationDuration } = this . props ;
6490 this . animatedValue = [ ] ;
6591
66- this . animations = data . map ( ( _ , index ) => {
92+ this . animations = this . _data . map ( ( _ , index ) => {
6793 this . animatedValue [ index ] = new Animated . Value ( 0 ) ;
6894 return Animated . stagger ( 0 , [
6995 Animated . timing ( this . animatedValue [ index ] , {
@@ -77,8 +103,8 @@ export default class GridList extends PureComponent {
77103
78104 _keyExtractor = ( item , index ) => index ;
79105
80- _renderItem = ( { item, index } ) => {
81- const { showAnimation, showSeparator, renderItem , itemStyle } = this . props ;
106+ renderItem = ( { item, index } ) => {
107+ const { showAnimation, showSeparator, itemStyle } = this . props ;
82108
83109 const viewStyles = [ ] ;
84110 viewStyles . push ( this . styles . itemContainer ) ;
@@ -100,15 +126,26 @@ export default class GridList extends PureComponent {
100126 { opacity : this . animatedValue [ index ] } ,
101127 ] }
102128 >
103- { renderItem ( { item, index, animation : this . animations [ index ] } ) }
129+ { this . _renderItem ( {
130+ item,
131+ index,
132+ animation : this . animations [ index ] ,
133+ } ) }
104134 </ Animated . View >
105135 ) : (
106- renderItem ( { item, index } )
136+ this . _renderItem ( { item, index } )
107137 ) }
108138 </ View >
109139 ) ;
110140 } ;
111141
142+ renderChildren = ( { item, animation } ) => {
143+ if ( animation ) {
144+ animation . start ( ) ;
145+ }
146+ return item ;
147+ } ;
148+
112149 render ( ) {
113150 const { showSeparator, ...props } = this . props ;
114151 return (
@@ -120,7 +157,8 @@ export default class GridList extends PureComponent {
120157 }
121158 showsVerticalScrollIndicator = { false }
122159 { ...props }
123- renderItem = { this . _renderItem }
160+ data = { this . _data }
161+ renderItem = { this . renderItem }
124162 />
125163 ) ;
126164 }
0 commit comments