Skip to content

Commit bf33332

Browse files
committed
Update GiftedListView.js
1 parent af454a7 commit bf33332

File tree

1 file changed

+73
-22
lines changed

1 file changed

+73
-22
lines changed

GiftedListView.js

Lines changed: 73 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,22 @@ var {
1010
Text
1111
} = React;
1212

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+
1329
var GiftedSpinner = require('react-native-gifted-spinner');
1430

1531
var GiftedListView = React.createClass({
@@ -158,10 +174,7 @@ var GiftedListView = React.createClass({
158174
_getRows() { return this._rows; },
159175

160176
getInitialState() {
161-
var ds = new ListView.DataSource({rowHasChanged: (r1, r2) => {
162-
return r1 !== r2;
163-
}});
164-
177+
165178
if (this.props.refreshable === true) {
166179
this._setY(this.props.refreshableViewHeight);
167180
} else {
@@ -170,26 +183,49 @@ var GiftedListView = React.createClass({
170183

171184
this._setPage(1);
172185
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+
}
179207
},
180208

181209
componentDidMount() {
182210
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});
184220
},
185221

186-
_onRefresh() {
222+
_onRefresh(external) {
187223
this._scrollResponder.scrollTo(0);
188224
this.setState({
189225
refreshStatus: 'fetching',
190226
});
191227
this._setPage(1);
192-
this.props.onFetch(this._getPage(), this._postRefresh);
228+
this.props.onFetch(this._getPage(), this._postRefresh, external);
193229
},
194230

195231
_postRefresh(rows = [], options = {}) {
@@ -210,17 +246,29 @@ var GiftedListView = React.createClass({
210246

211247
_postPaginate(rows = [], options = {}) {
212248
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);
215255
},
216256

217257
_updateRows(rows = [], options = {}) {
218258
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+
}
224272
},
225273

226274
_onResponderRelease() {
@@ -266,7 +314,7 @@ var GiftedListView = React.createClass({
266314
_renderPaginationView() {
267315
if ((this.state.paginationStatus === 'fetching' && this.props.pagination === true) || this.state.paginationStatus === 'firstLoad' && this.props.firstLoader === true) {
268316
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)) {
270318
return this.props.paginationWaitingView(this._onPaginate);
271319
} else if (this.state.paginationStatus === 'allLoaded' && this.props.pagination === true) {
272320
return this.props.paginationAllLoadedView();
@@ -299,6 +347,7 @@ var GiftedListView = React.createClass({
299347
ref="listview"
300348
dataSource={this.state.dataSource}
301349
renderRow={this.props.rowView}
350+
renderSectionHeader={this.props.sectionView}
302351
initialListSize={this.props.initialListSize}
303352
renderSeparator={this.props.renderSeparator}
304353

@@ -316,10 +365,12 @@ var GiftedListView = React.createClass({
316365
automaticallyAdjustContentInsets={false}
317366
scrollEnabled={true}
318367
canCancelContentTouches={true}
368+
369+
319370
/>
320371
);
321372
},
322373
});
323374

324375

325-
module.exports = GiftedListView;
376+
module.exports = GiftedListView;

0 commit comments

Comments
 (0)