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 ( {
@@ -158,10 +174,7 @@ var GiftedListView = React.createClass({
158
174
_getRows ( ) { return this . _rows ; } ,
159
175
160
176
getInitialState ( ) {
161
- var ds = new ListView . DataSource ( { rowHasChanged : ( r1 , r2 ) => {
162
- return r1 !== r2 ;
163
- } } ) ;
164
-
177
+
165
178
if ( this . props . refreshable === true ) {
166
179
this . _setY ( this . props . refreshableViewHeight ) ;
167
180
} else {
@@ -170,26 +183,49 @@ var GiftedListView = React.createClass({
170
183
171
184
this . _setPage ( 1 ) ;
172
185
this . _setRows ( [ ] ) ;
173
-
174
- return {
175
- dataSource : ds . cloneWithRows ( this . _getRows ( ) ) ,
176
- refreshStatus : 'waiting' ,
177
- paginationStatus : 'firstLoad' ,
178
- } ;
186
+
187
+ if ( this . props . withSections ) {
188
+ var ds = new ListView . DataSource ( {
189
+ rowHasChanged : ( row1 , row2 ) => row1 !== row2 ,
190
+ sectionHeaderHasChanged : ( section1 , section2 ) => section1 !== section2 ,
191
+ } ) ;
192
+ return {
193
+ dataSource : ds . cloneWithRowsAndSections ( this . _getRows ( ) ) ,
194
+ refreshStatus : 'waiting' ,
195
+ paginationStatus : 'firstLoad' ,
196
+ } ;
197
+ } else {
198
+ var ds = new ListView . DataSource ( {
199
+ rowHasChanged : ( row1 , row2 ) => row1 !== row2 ,
200
+ } ) ;
201
+ return {
202
+ dataSource : ds . cloneWithRows ( this . _getRows ( ) ) ,
203
+ refreshStatus : 'waiting' ,
204
+ paginationStatus : 'firstLoad' ,
205
+ } ;
206
+ }
179
207
} ,
180
208
181
209
componentDidMount ( ) {
182
210
this . _scrollResponder = this . refs . listview . getScrollResponder ( ) ;
183
- this . props . onFetch ( this . _getPage ( ) , this . _postRefresh ) ;
211
+ this . props . onFetch ( this . _getPage ( ) , this . _postRefresh , { initial : true } ) ;
212
+ } ,
213
+
214
+ setNativeProps ( props ) {
215
+ this . refs . listview . setNativeProps ( props ) ;
216
+ } ,
217
+
218
+ onRefresh ( ) {
219
+ this . _onRefresh ( { external : true } ) ;
184
220
} ,
185
221
186
- _onRefresh ( ) {
222
+ _onRefresh ( external ) {
187
223
this . _scrollResponder . scrollTo ( 0 ) ;
188
224
this . setState ( {
189
225
refreshStatus : 'fetching' ,
190
226
} ) ;
191
227
this . _setPage ( 1 ) ;
192
- this . props . onFetch ( this . _getPage ( ) , this . _postRefresh ) ;
228
+ this . props . onFetch ( this . _getPage ( ) , this . _postRefresh , external ) ;
193
229
} ,
194
230
195
231
_postRefresh ( rows = [ ] , options = { } ) {
@@ -210,17 +246,29 @@ var GiftedListView = React.createClass({
210
246
211
247
_postPaginate ( rows = [ ] , options = { } ) {
212
248
this . _setPage ( this . _getPage ( ) + 1 ) ;
213
- var concatenatedRows = this . _getRows ( ) . concat ( rows ) ;
214
- this . _updateRows ( concatenatedRows , options ) ;
249
+ if ( this . props . withSections ) {
250
+ var mergedRows = MergeRecursive ( this . _getRows ( ) , rows ) ;
251
+ } else {
252
+ var mergedRows = this . _getRows ( ) . concat ( rows ) ;
253
+ }
254
+ this . _updateRows ( mergedRows , options ) ;
215
255
} ,
216
256
217
257
_updateRows ( rows = [ ] , options = { } ) {
218
258
this . _setRows ( rows ) ;
219
- this . setState ( {
220
- dataSource : this . state . dataSource . cloneWithRows ( rows ) ,
221
- refreshStatus : 'waiting' ,
222
- paginationStatus : ( options . allLoaded === true ? 'allLoaded' : 'waiting' ) ,
223
- } ) ;
259
+ if ( this . props . withSections ) {
260
+ this . setState ( {
261
+ dataSource : this . state . dataSource . cloneWithRowsAndSections ( rows ) ,
262
+ refreshStatus : 'waiting' ,
263
+ paginationStatus : ( options . allLoaded === true ? 'allLoaded' : 'waiting' ) ,
264
+ } ) ;
265
+ } else {
266
+ this . setState ( {
267
+ dataSource : this . state . dataSource . cloneWithRows ( rows ) ,
268
+ refreshStatus : 'waiting' ,
269
+ paginationStatus : ( options . allLoaded === true ? 'allLoaded' : 'waiting' ) ,
270
+ } ) ;
271
+ }
224
272
} ,
225
273
226
274
_onResponderRelease ( ) {
@@ -266,7 +314,7 @@ var GiftedListView = React.createClass({
266
314
_renderPaginationView ( ) {
267
315
if ( ( this . state . paginationStatus === 'fetching' && this . props . pagination === true ) || this . state . paginationStatus === 'firstLoad' && this . props . firstLoader === true ) {
268
316
return this . props . paginationFetchigView ( ) ;
269
- } else if ( this . state . paginationStatus === 'waiting' && this . props . pagination === true && this . _getRows ( ) . length > 0 ) {
317
+ } else if ( this . state . paginationStatus === 'waiting' && this . props . pagination === true && ( this . props . withSections || this . _getRows ( ) . length > 0 ) ) {
270
318
return this . props . paginationWaitingView ( this . _onPaginate ) ;
271
319
} else if ( this . state . paginationStatus === 'allLoaded' && this . props . pagination === true ) {
272
320
return this . props . paginationAllLoadedView ( ) ;
@@ -299,6 +347,7 @@ var GiftedListView = React.createClass({
299
347
ref = "listview"
300
348
dataSource = { this . state . dataSource }
301
349
renderRow = { this . props . rowView }
350
+ renderSectionHeader = { this . props . sectionView }
302
351
initialListSize = { this . props . initialListSize }
303
352
renderSeparator = { this . props . renderSeparator }
304
353
@@ -316,10 +365,12 @@ var GiftedListView = React.createClass({
316
365
automaticallyAdjustContentInsets = { false }
317
366
scrollEnabled = { true }
318
367
canCancelContentTouches = { true }
368
+
369
+
319
370
/>
320
371
) ;
321
372
} ,
322
373
} ) ;
323
374
324
375
325
- module . exports = GiftedListView ;
376
+ module . exports = GiftedListView ;
0 commit comments