Skip to content

Commit cda2bc6

Browse files
committed
[ListView] Allow different types of ScrollView to be composed
This enables code like: ```js <ListView renderScrollView={(props) => <CustomScrollView {...props} />} /> ``` where CustomScrollView might be inverted or support pull-to-refresh, etc.
1 parent e81e29f commit cda2bc6

File tree

1 file changed

+24
-15
lines changed

1 file changed

+24
-15
lines changed

Libraries/CustomComponents/ListView/ListView.js

+24-15
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ var ScrollResponder = require('ScrollResponder');
3434
var StaticRenderer = require('StaticRenderer');
3535
var TimerMixin = require('react-timer-mixin');
3636

37+
var isEmpty = require('isEmpty');
3738
var logError = require('logError');
3839
var merge = require('merge');
39-
var isEmpty = require('isEmpty');
4040

4141
var PropTypes = React.PropTypes;
4242

@@ -173,6 +173,13 @@ var ListView = React.createClass({
173173
* header.
174174
*/
175175
renderSectionHeader: PropTypes.func,
176+
/**
177+
* (props) => renderable
178+
*
179+
* A function that returns the scrollable component in which the list rows
180+
* are rendered. Defaults to returning a ScrollView with the given props.
181+
*/
182+
renderScrollComponent: React.PropTypes.func.isRequired,
176183
/**
177184
* How early to start rendering rows before they come on screen, in
178185
* pixels.
@@ -228,6 +235,7 @@ var ListView = React.createClass({
228235
return {
229236
initialListSize: DEFAULT_INITIAL_ROWS,
230237
pageSize: DEFAULT_PAGE_SIZE,
238+
renderScrollComponent: props => <ScrollView {...props} />,
231239
scrollRenderAheadDistance: DEFAULT_SCROLL_RENDER_AHEAD,
232240
onEndReachedThreshold: DEFAULT_END_REACHED_THRESHOLD,
233241
};
@@ -363,23 +371,24 @@ var ListView = React.createClass({
363371
}
364372
}
365373

366-
var props = merge(
367-
this.props, {
368-
onScroll: this._onScroll,
369-
stickyHeaderIndices: sectionHeaderIndices,
370-
}
371-
);
374+
var {
375+
renderScrollComponent,
376+
...props,
377+
} = this.props;
372378
if (!props.scrollEventThrottle) {
373379
props.scrollEventThrottle = DEFAULT_SCROLL_CALLBACK_THROTTLE;
374380
}
375-
return (
376-
<ScrollView {...props}
377-
ref={SCROLLVIEW_REF}>
378-
{header}
379-
{bodyComponents}
380-
{footer}
381-
</ScrollView>
382-
);
381+
Object.assign(props, {
382+
onScroll: this._onScroll,
383+
stickyHeaderIndices: sectionHeaderIndices,
384+
children: [header, bodyComponents, footer],
385+
});
386+
387+
// TODO(ide): Use function refs so we can compose with the scroll
388+
// component's original ref instead of clobbering it
389+
return React.cloneElement(renderScrollComponent(props), {
390+
ref: SCROLLVIEW_REF,
391+
});
383392
},
384393

385394
/**

0 commit comments

Comments
 (0)