10
10
Text
11
11
} = React ;
12
12
13
+ // small helper function which merged two objects into one
14
+ function MergeRecursive ( obj1 , obj2 ) {
15
+ for ( var p in obj2 ) {
16
+ try {
17
+ if ( obj2 [ p ] . constructor == Object ) {
18
+ obj1 [ p ] = MergeRecursive ( obj1 [ p ] , obj2 [ p ] ) ;
19
+ } else {
20
+ obj1 [ p ] = obj2 [ p ] ;
21
+ }
22
+ } catch ( e ) {
23
+ obj1 [ p ] = obj2 [ p ] ;
24
+ }
25
+ }
26
+ return obj1 ;
27
+ }
28
+
13
29
var GiftedSpinner = require ( 'react-native-gifted-spinner' ) ;
14
30
15
31
var GiftedListView = React . createClass ( {
@@ -54,7 +70,9 @@ var GiftedListView = React.createClass({
54
70
refreshable : true ,
55
71
refreshableViewHeight : 50 ,
56
72
refreshableDistance : 40 ,
57
- onFetch ( page , callback ) { callback ( [ ] ) ; } ,
73
+ sectionHeaderView : null ,
74
+ withSections : false ,
75
+ onFetch ( page , callback , options ) { callback ( [ ] ) ; } ,
58
76
59
77
paginationFetchigView ( ) {
60
78
return (
@@ -157,11 +175,9 @@ var GiftedListView = React.createClass({
157
175
_setRows ( rows ) { this . _rows = rows ; } ,
158
176
_getRows ( ) { return this . _rows ; } ,
159
177
178
+
160
179
getInitialState ( ) {
161
- var ds = new ListView . DataSource ( { rowHasChanged : ( r1 , r2 ) => {
162
- return r1 !== r2 ;
163
- } } ) ;
164
-
180
+
165
181
if ( this . props . refreshable === true && Platform . OS !== 'android' ) {
166
182
this . _setY ( this . props . refreshableViewHeight ) ;
167
183
} else {
@@ -170,26 +186,50 @@ var GiftedListView = React.createClass({
170
186
171
187
this . _setPage ( 1 ) ;
172
188
this . _setRows ( [ ] ) ;
173
-
174
- return {
175
- dataSource : ds . cloneWithRows ( this . _getRows ( ) ) ,
176
- refreshStatus : 'waiting' ,
177
- paginationStatus : 'firstLoad' ,
178
- } ;
189
+
190
+ var ds = null ;
191
+ if ( this . props . withSections === true ) {
192
+ ds = new ListView . DataSource ( {
193
+ rowHasChanged : ( row1 , row2 ) => row1 !== row2 ,
194
+ sectionHeaderHasChanged : ( section1 , section2 ) => section1 !== section2 ,
195
+ } ) ;
196
+ return {
197
+ dataSource : ds . cloneWithRowsAndSections ( this . _getRows ( ) ) ,
198
+ refreshStatus : 'waiting' ,
199
+ paginationStatus : 'firstLoad' ,
200
+ } ;
201
+ } else {
202
+ ds = new ListView . DataSource ( {
203
+ rowHasChanged : ( row1 , row2 ) => row1 !== row2 ,
204
+ } ) ;
205
+ return {
206
+ dataSource : ds . cloneWithRows ( this . _getRows ( ) ) ,
207
+ refreshStatus : 'waiting' ,
208
+ paginationStatus : 'firstLoad' ,
209
+ } ;
210
+ }
179
211
} ,
180
212
181
213
componentDidMount ( ) {
182
214
this . _scrollResponder = this . refs . listview . getScrollResponder ( ) ;
183
- this . props . onFetch ( this . _getPage ( ) , this . _postRefresh ) ;
215
+ this . props . onFetch ( this . _getPage ( ) , this . _postRefresh , { firstLoad : true } ) ;
216
+ } ,
217
+
218
+ setNativeProps ( props ) {
219
+ this . refs . listview . setNativeProps ( props ) ;
220
+ } ,
221
+
222
+ _refresh ( ) {
223
+ this . _onRefresh ( { external : true } ) ;
184
224
} ,
185
225
186
- _onRefresh ( ) {
226
+ _onRefresh ( options = { } ) {
187
227
this . _scrollResponder . scrollTo ( 0 ) ;
188
228
this . setState ( {
189
229
refreshStatus : 'fetching' ,
190
230
} ) ;
191
231
this . _setPage ( 1 ) ;
192
- this . props . onFetch ( this . _getPage ( ) , this . _postRefresh ) ;
232
+ this . props . onFetch ( this . _getPage ( ) , this . _postRefresh , options ) ;
193
233
} ,
194
234
195
235
_postRefresh ( rows = [ ] , options = { } ) {
@@ -205,22 +245,35 @@ var GiftedListView = React.createClass({
205
245
this . setState ( {
206
246
paginationStatus : 'fetching' ,
207
247
} ) ;
208
- this . props . onFetch ( this . _getPage ( ) + 1 , this . _postPaginate ) ;
248
+ this . props . onFetch ( this . _getPage ( ) + 1 , this . _postPaginate , { } ) ;
209
249
} ,
210
250
211
251
_postPaginate ( rows = [ ] , options = { } ) {
212
252
this . _setPage ( this . _getPage ( ) + 1 ) ;
213
- var concatenatedRows = this . _getRows ( ) . concat ( rows ) ;
214
- this . _updateRows ( concatenatedRows , options ) ;
253
+ var mergedRows = null ;
254
+ if ( this . props . withSections === true ) {
255
+ mergedRows = MergeRecursive ( this . _getRows ( ) , rows ) ;
256
+ } else {
257
+ mergedRows = this . _getRows ( ) . concat ( rows ) ;
258
+ }
259
+ this . _updateRows ( mergedRows , options ) ;
215
260
} ,
216
261
217
262
_updateRows ( rows = [ ] , options = { } ) {
218
263
this . _setRows ( rows ) ;
219
- this . setState ( {
220
- dataSource : this . state . dataSource . cloneWithRows ( rows ) ,
221
- refreshStatus : 'waiting' ,
222
- paginationStatus : ( options . allLoaded === true ? 'allLoaded' : 'waiting' ) ,
223
- } ) ;
264
+ if ( this . props . withSections === true ) {
265
+ this . setState ( {
266
+ dataSource : this . state . dataSource . cloneWithRowsAndSections ( rows ) ,
267
+ refreshStatus : 'waiting' ,
268
+ paginationStatus : ( options . allLoaded === true ? 'allLoaded' : 'waiting' ) ,
269
+ } ) ;
270
+ } else {
271
+ this . setState ( {
272
+ dataSource : this . state . dataSource . cloneWithRows ( rows ) ,
273
+ refreshStatus : 'waiting' ,
274
+ paginationStatus : ( options . allLoaded === true ? 'allLoaded' : 'waiting' ) ,
275
+ } ) ;
276
+ }
224
277
} ,
225
278
226
279
_onResponderRelease ( ) {
@@ -264,9 +317,9 @@ var GiftedListView = React.createClass({
264
317
} ,
265
318
266
319
_renderPaginationView ( ) {
267
- if ( ( this . state . paginationStatus === 'fetching' && this . props . pagination === true ) || this . state . paginationStatus === 'firstLoad' && this . props . firstLoader === true ) {
320
+ if ( ( this . state . paginationStatus === 'fetching' && this . props . pagination === true ) || ( this . state . paginationStatus === 'firstLoad' && this . props . firstLoader === true ) ) {
268
321
return this . props . paginationFetchigView ( ) ;
269
- } else if ( this . state . paginationStatus === 'waiting' && this . props . pagination === true && this . _getRows ( ) . length > 0 ) {
322
+ } else if ( this . state . paginationStatus === 'waiting' && this . props . pagination === true && ( this . props . withSections === true || this . _getRows ( ) . length > 0 ) ) {
270
323
return this . props . paginationWaitingView ( this . _onPaginate ) ;
271
324
} else if ( this . state . paginationStatus === 'allLoaded' && this . props . pagination === true ) {
272
325
return this . props . paginationAllLoadedView ( ) ;
@@ -299,6 +352,7 @@ var GiftedListView = React.createClass({
299
352
ref = "listview"
300
353
dataSource = { this . state . dataSource }
301
354
renderRow = { this . props . rowView }
355
+ renderSectionHeader = { this . props . sectionHeaderView }
302
356
initialListSize = { this . props . initialListSize }
303
357
renderSeparator = { this . props . renderSeparator }
304
358
@@ -322,4 +376,4 @@ var GiftedListView = React.createClass({
322
376
} ) ;
323
377
324
378
325
- module . exports = GiftedListView ;
379
+ module . exports = GiftedListView ;
0 commit comments