2015-01-30 01:10:49 +00:00
|
|
|
/**
|
2017-03-24 21:18:39 +00:00
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
2016-03-17 19:10:35 +00:00
|
|
|
*
|
2018-02-17 02:24:55 +00:00
|
|
|
* This source code is licensed under the MIT license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree.
|
2015-03-23 22:07:33 +00:00
|
|
|
*
|
2016-06-23 16:23:09 +00:00
|
|
|
* @flow
|
2017-06-13 05:32:58 +00:00
|
|
|
* @format
|
2015-01-30 01:10:49 +00:00
|
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
|
2018-05-14 07:09:10 +00:00
|
|
|
const InternalListViewType = require('InternalListViewType');
|
2018-05-10 22:44:52 +00:00
|
|
|
const ListViewDataSource = require('ListViewDataSource');
|
|
|
|
const Platform = require('Platform');
|
|
|
|
const React = require('React');
|
|
|
|
const PropTypes = require('prop-types');
|
|
|
|
const ReactNative = require('ReactNative');
|
|
|
|
const RCTScrollViewManager = require('NativeModules').ScrollViewManager;
|
|
|
|
const ScrollView = require('ScrollView');
|
|
|
|
const ScrollResponder = require('ScrollResponder');
|
|
|
|
const StaticRenderer = require('StaticRenderer');
|
|
|
|
const TimerMixin = require('react-timer-mixin');
|
|
|
|
const View = require('View');
|
|
|
|
const cloneReferencedElement = require('react-clone-referenced-element');
|
|
|
|
const createReactClass = require('create-react-class');
|
|
|
|
const isEmpty = require('isEmpty');
|
|
|
|
const merge = require('merge');
|
2015-01-30 01:10:49 +00:00
|
|
|
|
2018-05-14 07:09:10 +00:00
|
|
|
import type {Props as ScrollViewProps} from 'ScrollView';
|
|
|
|
|
2018-05-10 22:44:52 +00:00
|
|
|
const DEFAULT_PAGE_SIZE = 1;
|
|
|
|
const DEFAULT_INITIAL_ROWS = 10;
|
|
|
|
const DEFAULT_SCROLL_RENDER_AHEAD = 1000;
|
|
|
|
const DEFAULT_END_REACHED_THRESHOLD = 1000;
|
|
|
|
const DEFAULT_SCROLL_CALLBACK_THROTTLE = 50;
|
2015-03-09 23:18:15 +00:00
|
|
|
|
2018-05-14 07:09:10 +00:00
|
|
|
type Props = $ReadOnly<{|
|
|
|
|
...ScrollViewProps,
|
|
|
|
|
|
|
|
dataSource: ListViewDataSource,
|
|
|
|
renderSeparator?: ?Function,
|
|
|
|
renderRow: Function,
|
|
|
|
initialListSize?: ?number,
|
|
|
|
onEndReached?: ?Function,
|
|
|
|
onEndReachedThreshold?: ?number,
|
|
|
|
pageSize?: ?number,
|
|
|
|
renderFooter?: ?Function,
|
|
|
|
renderHeader?: ?Function,
|
|
|
|
renderSectionHeader?: ?Function,
|
|
|
|
renderScrollComponent?: ?Function,
|
|
|
|
scrollRenderAheadDistance?: ?number,
|
|
|
|
onChangeVisibleRows?: ?Function,
|
|
|
|
removeClippedSubviews?: ?boolean,
|
|
|
|
stickySectionHeadersEnabled?: ?boolean,
|
|
|
|
stickyHeaderIndices?: ?$ReadOnlyArray<number>,
|
|
|
|
enableEmptySections?: ?boolean,
|
|
|
|
|}>;
|
|
|
|
|
2015-01-30 01:10:49 +00:00
|
|
|
/**
|
2017-07-06 18:46:48 +00:00
|
|
|
* DEPRECATED - use one of the new list components, such as [`FlatList`](docs/flatlist.html)
|
|
|
|
* or [`SectionList`](docs/sectionlist.html) for bounded memory use, fewer bugs,
|
|
|
|
* better performance, an easier to use API, and more features. Check out this
|
|
|
|
* [blog post](https://facebook.github.io/react-native/blog/2017/03/13/better-list-views.html)
|
|
|
|
* for more details.
|
|
|
|
*
|
2015-01-30 01:10:49 +00:00
|
|
|
* ListView - A core component designed for efficient display of vertically
|
2017-01-09 11:08:05 +00:00
|
|
|
* scrolling lists of changing data. The minimal API is to create a
|
2016-04-24 15:12:28 +00:00
|
|
|
* [`ListView.DataSource`](docs/listviewdatasource.html), populate it with a simple
|
|
|
|
* array of data blobs, and instantiate a `ListView` component with that data
|
|
|
|
* source and a `renderRow` callback which takes a blob from the data array and
|
|
|
|
* returns a renderable component.
|
2015-03-09 23:18:15 +00:00
|
|
|
*
|
|
|
|
* Minimal example:
|
2015-01-30 01:10:49 +00:00
|
|
|
*
|
2015-03-09 23:18:15 +00:00
|
|
|
* ```
|
2016-08-16 16:48:13 +00:00
|
|
|
* class MyComponent extends Component {
|
|
|
|
* constructor() {
|
|
|
|
* super();
|
|
|
|
* const ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2});
|
|
|
|
* this.state = {
|
|
|
|
* dataSource: ds.cloneWithRows(['row 1', 'row 2']),
|
|
|
|
* };
|
|
|
|
* }
|
2015-01-30 01:10:49 +00:00
|
|
|
*
|
2016-08-16 16:48:13 +00:00
|
|
|
* render() {
|
|
|
|
* return (
|
|
|
|
* <ListView
|
|
|
|
* dataSource={this.state.dataSource}
|
|
|
|
* renderRow={(rowData) => <Text>{rowData}</Text>}
|
|
|
|
* />
|
|
|
|
* );
|
|
|
|
* }
|
|
|
|
* }
|
2015-03-09 23:18:15 +00:00
|
|
|
* ```
|
2015-01-30 01:10:49 +00:00
|
|
|
*
|
|
|
|
* ListView also supports more advanced features, including sections with sticky
|
|
|
|
* section headers, header and footer support, callbacks on reaching the end of
|
|
|
|
* the available data (`onEndReached`) and on the set of rows that are visible
|
|
|
|
* in the device viewport change (`onChangeVisibleRows`), and several
|
|
|
|
* performance optimizations.
|
|
|
|
*
|
|
|
|
* There are a few performance operations designed to make ListView scroll
|
|
|
|
* smoothly while dynamically loading potentially very large (or conceptually
|
|
|
|
* infinite) data sets:
|
|
|
|
*
|
2015-05-14 16:45:38 +00:00
|
|
|
* * Only re-render changed rows - the rowHasChanged function provided to the
|
2015-01-30 01:10:49 +00:00
|
|
|
* data source tells the ListView if it needs to re-render a row because the
|
|
|
|
* source data has changed - see ListViewDataSource for more details.
|
|
|
|
*
|
|
|
|
* * Rate-limited row rendering - By default, only one row is rendered per
|
2017-01-09 11:08:05 +00:00
|
|
|
* event-loop (customizable with the `pageSize` prop). This breaks up the
|
2015-01-30 01:10:49 +00:00
|
|
|
* work into smaller chunks to reduce the chance of dropping frames while
|
|
|
|
* rendering rows.
|
|
|
|
*/
|
|
|
|
|
2018-05-10 22:44:52 +00:00
|
|
|
const ListView = createReactClass({
|
2017-07-07 21:24:25 +00:00
|
|
|
displayName: 'ListView',
|
2016-06-23 16:23:09 +00:00
|
|
|
_childFrames: ([]: Array<Object>),
|
|
|
|
_sentEndForContentLength: (null: ?number),
|
2018-05-14 07:09:10 +00:00
|
|
|
_scrollComponent: (null: ?React.ElementRef<typeof ScrollView>),
|
2016-06-23 16:23:09 +00:00
|
|
|
_prevRenderedRowsCount: 0,
|
|
|
|
_visibleRows: ({}: Object),
|
|
|
|
scrollProperties: ({}: Object),
|
|
|
|
|
2015-01-30 01:10:49 +00:00
|
|
|
mixins: [ScrollResponder.Mixin, TimerMixin],
|
|
|
|
|
2015-03-03 16:38:50 +00:00
|
|
|
statics: {
|
|
|
|
DataSource: ListViewDataSource,
|
|
|
|
},
|
|
|
|
|
2015-01-30 01:10:49 +00:00
|
|
|
/**
|
|
|
|
* You must provide a renderRow function. If you omit any of the other render
|
|
|
|
* functions, ListView will simply skip rendering them.
|
|
|
|
*
|
2015-05-26 22:16:42 +00:00
|
|
|
* - renderRow(rowData, sectionID, rowID, highlightRow);
|
2015-01-30 01:10:49 +00:00
|
|
|
* - renderSectionHeader(sectionData, sectionID);
|
|
|
|
*/
|
2015-03-05 05:06:49 +00:00
|
|
|
propTypes: {
|
|
|
|
...ScrollView.propTypes,
|
2016-04-24 15:12:28 +00:00
|
|
|
/**
|
|
|
|
* An instance of [ListView.DataSource](docs/listviewdatasource.html) to use
|
|
|
|
*/
|
2015-03-05 05:06:49 +00:00
|
|
|
dataSource: PropTypes.instanceOf(ListViewDataSource).isRequired,
|
|
|
|
/**
|
2015-05-26 22:16:42 +00:00
|
|
|
* (sectionID, rowID, adjacentRowHighlighted) => renderable
|
2015-12-03 18:53:40 +00:00
|
|
|
*
|
2015-05-26 22:16:42 +00:00
|
|
|
* If provided, a renderable component to be rendered as the separator
|
|
|
|
* below each row but not the last row if there is a section header below.
|
|
|
|
* Take a sectionID and rowID of the row above and whether its adjacent row
|
|
|
|
* is highlighted.
|
|
|
|
*/
|
|
|
|
renderSeparator: PropTypes.func,
|
|
|
|
/**
|
|
|
|
* (rowData, sectionID, rowID, highlightRow) => renderable
|
2015-12-03 18:53:40 +00:00
|
|
|
*
|
2015-03-05 05:06:49 +00:00
|
|
|
* Takes a data entry from the data source and its ids and should return
|
2017-01-09 11:08:05 +00:00
|
|
|
* a renderable component to be rendered as the row. By default the data
|
2015-03-05 05:06:49 +00:00
|
|
|
* is exactly what was put into the data source, but it's also possible to
|
2015-05-26 22:16:42 +00:00
|
|
|
* provide custom extractors. ListView can be notified when a row is
|
2016-05-05 10:42:02 +00:00
|
|
|
* being highlighted by calling `highlightRow(sectionID, rowID)`. This
|
|
|
|
* sets a boolean value of adjacentRowHighlighted in renderSeparator, allowing you
|
|
|
|
* to control the separators above and below the highlighted row. The highlighted
|
|
|
|
* state of a row can be reset by calling highlightRow(null).
|
2015-03-05 05:06:49 +00:00
|
|
|
*/
|
|
|
|
renderRow: PropTypes.func.isRequired,
|
|
|
|
/**
|
2017-01-09 11:08:05 +00:00
|
|
|
* How many rows to render on initial component mount. Use this to make
|
2015-12-15 17:08:39 +00:00
|
|
|
* it so that the first screen worth of data appears at one time instead of
|
2015-03-05 05:06:49 +00:00
|
|
|
* over the course of multiple frames.
|
|
|
|
*/
|
2016-06-23 16:23:09 +00:00
|
|
|
initialListSize: PropTypes.number.isRequired,
|
2015-03-05 05:06:49 +00:00
|
|
|
/**
|
|
|
|
* Called when all rows have been rendered and the list has been scrolled
|
2017-01-09 11:08:05 +00:00
|
|
|
* to within onEndReachedThreshold of the bottom. The native scroll
|
2015-03-05 05:06:49 +00:00
|
|
|
* event is provided.
|
|
|
|
*/
|
|
|
|
onEndReached: PropTypes.func,
|
|
|
|
/**
|
Fixed missing rows on UIExplorer <ListView> - Grid Layout example
Summary:
public
I was looking into the missing panels at the bottom of the <ListView> - Grid Layout example, and found that it was caused by several problems, some in the example and some in ListView itself.
The first problem seemed to be a bug in the `_getDistanceFromEnd()` method, which calculates whether the ListView needs to load more content based on the distance of the visible content from the bottom of the scrollview. This was previously using the function
Math.max(scrollProperties.contentLength, scrollProperties.visibleLength) - scrollProperties.visibleLength - scrollProperties.offset
to calculate the amount the user could scroll before they run out of content. This sort-of works in most cases because `scrollProperties.contentLength` is usually longer than `scrollProperties.visibleLength`, so this would generally evaluate to
scrollProperties.contentLength - scrollProperties.visibleLength - scrollProperties.offset
which meant that it would be positive as long as there was content still to be displayed offscreen, and negative when you reached the end of the content. This logic breaks down if `contentLength` is less than `visibleLength`, however. For example, if you have 300pts of content loaded, and your scrollView is 500pts tall, and your scroll position is zero, this evaluates to
Math.max(300, 500) - 500 - 0 = 0
In other words, the algorithm is saying that you have zero pts of scroll content remaining before you need to reload. But actually, the bottom 200pts of the screen are empty, so you're really 200pts in debt, and need to load extra rows to fill that space. The correct algorithm is simply to get rid of the `Math.max` and just use
scrollProperties.contentLength - scrollProperties.visibleLength - scrollProperties.offset
I originally thought that this was the cause of the gap, but it isn't, because ListView has `DEFAULT_SCROLL_RENDER_AHEAD = 1000`, which means that it tries to load at least 1000pts more content than is currently visible, to avoid gaps. This masked the bug, so in practice it wasn't causing an issue.
The next problem I found was that there is an implict assumption in ListView that the first page of content you load is sufficient to cover the screen, or rather, that the first _ second page is sufficient. The constants `DEFAULT_INITIAL_ROWS = 10` and `DEFAULT_PAGE_SIZE = 1`, mean that when the ListView first loads, the following happens:
1. It loads 10 rows of content.
2. It checks if `_getDistanceFromEnd() < DEFAULT_SCROLL_RENDER_AHEAD` (1000).
3. If it is, it loads another `DEFAULT_PAGE_SIZE` rows of content, then stops.
In the case of the ListView Grid Layout example, this meant that it first loaded 10 cells, then loaded another 1, for a total of 11. The problem was that going from 10 to 11 cells isn't sufficient to fill the visible scroll area, and it doesn't change the `contentSize` (since the cells wrap onto the same line), and since ListView doesn't try to load any more until the `contentSize` or `scrollOffset ` changes, it stops loading new rows at that point.
I tried fixing this by calling `_renderMoreRowsIfNeeded()` after `_pageInNewRows()` so that it will continue to fetch new rows until the `_getDistanceFromEnd()` is less than the threshold, rather than stopping after the first page and waiting until the `contentSize` or `scrollOffset` change, but although this solves the problem for the Grid Layout example, it leads to over-fetching in the more common case of a standard row-based ListView.
In the end, I just increased the `pageSize` to 3 for the Grid Layout example, which makes more sense anyway since loading a page that is not a multiple of the number of cells per row confuses the `_renderMoreRowsIfNeeded` algorithm, and leads to gaps at the bottom of the view.
This solved the problem, however there was still a "pop-in" effect, where the additional rows were paged in after the ListView appeared. This was simply a misconfiguration in the example itself: The default of 10 rows was insufficient to fill the screen, so I changed the `initialListSize` prop to `20`.
Reviewed By: javache
Differential Revision: D2911690
fb-gh-sync-id: 8d6bd78843335fb091e7e24f7c2e6a416b0321d3
shipit-source-id: 8d6bd78843335fb091e7e24f7c2e6a416b0321d3
2016-02-10 16:36:15 +00:00
|
|
|
* Threshold in pixels (virtual, not physical) for calling onEndReached.
|
2015-03-05 05:06:49 +00:00
|
|
|
*/
|
2016-06-23 16:23:09 +00:00
|
|
|
onEndReachedThreshold: PropTypes.number.isRequired,
|
2015-03-05 05:06:49 +00:00
|
|
|
/**
|
Fixed missing rows on UIExplorer <ListView> - Grid Layout example
Summary:
public
I was looking into the missing panels at the bottom of the <ListView> - Grid Layout example, and found that it was caused by several problems, some in the example and some in ListView itself.
The first problem seemed to be a bug in the `_getDistanceFromEnd()` method, which calculates whether the ListView needs to load more content based on the distance of the visible content from the bottom of the scrollview. This was previously using the function
Math.max(scrollProperties.contentLength, scrollProperties.visibleLength) - scrollProperties.visibleLength - scrollProperties.offset
to calculate the amount the user could scroll before they run out of content. This sort-of works in most cases because `scrollProperties.contentLength` is usually longer than `scrollProperties.visibleLength`, so this would generally evaluate to
scrollProperties.contentLength - scrollProperties.visibleLength - scrollProperties.offset
which meant that it would be positive as long as there was content still to be displayed offscreen, and negative when you reached the end of the content. This logic breaks down if `contentLength` is less than `visibleLength`, however. For example, if you have 300pts of content loaded, and your scrollView is 500pts tall, and your scroll position is zero, this evaluates to
Math.max(300, 500) - 500 - 0 = 0
In other words, the algorithm is saying that you have zero pts of scroll content remaining before you need to reload. But actually, the bottom 200pts of the screen are empty, so you're really 200pts in debt, and need to load extra rows to fill that space. The correct algorithm is simply to get rid of the `Math.max` and just use
scrollProperties.contentLength - scrollProperties.visibleLength - scrollProperties.offset
I originally thought that this was the cause of the gap, but it isn't, because ListView has `DEFAULT_SCROLL_RENDER_AHEAD = 1000`, which means that it tries to load at least 1000pts more content than is currently visible, to avoid gaps. This masked the bug, so in practice it wasn't causing an issue.
The next problem I found was that there is an implict assumption in ListView that the first page of content you load is sufficient to cover the screen, or rather, that the first _ second page is sufficient. The constants `DEFAULT_INITIAL_ROWS = 10` and `DEFAULT_PAGE_SIZE = 1`, mean that when the ListView first loads, the following happens:
1. It loads 10 rows of content.
2. It checks if `_getDistanceFromEnd() < DEFAULT_SCROLL_RENDER_AHEAD` (1000).
3. If it is, it loads another `DEFAULT_PAGE_SIZE` rows of content, then stops.
In the case of the ListView Grid Layout example, this meant that it first loaded 10 cells, then loaded another 1, for a total of 11. The problem was that going from 10 to 11 cells isn't sufficient to fill the visible scroll area, and it doesn't change the `contentSize` (since the cells wrap onto the same line), and since ListView doesn't try to load any more until the `contentSize` or `scrollOffset ` changes, it stops loading new rows at that point.
I tried fixing this by calling `_renderMoreRowsIfNeeded()` after `_pageInNewRows()` so that it will continue to fetch new rows until the `_getDistanceFromEnd()` is less than the threshold, rather than stopping after the first page and waiting until the `contentSize` or `scrollOffset` change, but although this solves the problem for the Grid Layout example, it leads to over-fetching in the more common case of a standard row-based ListView.
In the end, I just increased the `pageSize` to 3 for the Grid Layout example, which makes more sense anyway since loading a page that is not a multiple of the number of cells per row confuses the `_renderMoreRowsIfNeeded` algorithm, and leads to gaps at the bottom of the view.
This solved the problem, however there was still a "pop-in" effect, where the additional rows were paged in after the ListView appeared. This was simply a misconfiguration in the example itself: The default of 10 rows was insufficient to fill the screen, so I changed the `initialListSize` prop to `20`.
Reviewed By: javache
Differential Revision: D2911690
fb-gh-sync-id: 8d6bd78843335fb091e7e24f7c2e6a416b0321d3
shipit-source-id: 8d6bd78843335fb091e7e24f7c2e6a416b0321d3
2016-02-10 16:36:15 +00:00
|
|
|
* Number of rows to render per event loop. Note: if your 'rows' are actually
|
|
|
|
* cells, i.e. they don't span the full width of your view (as in the
|
|
|
|
* ListViewGridLayoutExample), you should set the pageSize to be a multiple
|
|
|
|
* of the number of cells per row, otherwise you're likely to see gaps at
|
|
|
|
* the edge of the ListView as new pages are loaded.
|
2015-03-05 05:06:49 +00:00
|
|
|
*/
|
2016-06-23 16:23:09 +00:00
|
|
|
pageSize: PropTypes.number.isRequired,
|
2015-03-05 05:06:49 +00:00
|
|
|
/**
|
|
|
|
* () => renderable
|
|
|
|
*
|
|
|
|
* The header and footer are always rendered (if these props are provided)
|
2017-01-09 11:08:05 +00:00
|
|
|
* on every render pass. If they are expensive to re-render, wrap them
|
|
|
|
* in StaticContainer or other mechanism as appropriate. Footer is always
|
2015-03-05 05:06:49 +00:00
|
|
|
* at the bottom of the list, and header at the top, on every render pass.
|
2017-07-21 21:17:47 +00:00
|
|
|
* In a horizontal ListView, the header is rendered on the left and the
|
|
|
|
* footer on the right.
|
2015-03-05 05:06:49 +00:00
|
|
|
*/
|
|
|
|
renderFooter: PropTypes.func,
|
|
|
|
renderHeader: PropTypes.func,
|
|
|
|
/**
|
|
|
|
* (sectionData, sectionID) => renderable
|
|
|
|
*
|
2017-01-09 11:08:05 +00:00
|
|
|
* If provided, a header is rendered for this section.
|
2015-03-05 05:06:49 +00:00
|
|
|
*/
|
|
|
|
renderSectionHeader: PropTypes.func,
|
2015-07-01 16:02:38 +00:00
|
|
|
/**
|
|
|
|
* (props) => renderable
|
|
|
|
*
|
|
|
|
* A function that returns the scrollable component in which the list rows
|
|
|
|
* are rendered. Defaults to returning a ScrollView with the given props.
|
|
|
|
*/
|
2017-04-12 23:09:48 +00:00
|
|
|
renderScrollComponent: PropTypes.func.isRequired,
|
2015-03-05 05:06:49 +00:00
|
|
|
/**
|
|
|
|
* How early to start rendering rows before they come on screen, in
|
|
|
|
* pixels.
|
|
|
|
*/
|
2017-04-12 23:09:48 +00:00
|
|
|
scrollRenderAheadDistance: PropTypes.number.isRequired,
|
2015-03-05 05:06:49 +00:00
|
|
|
/**
|
|
|
|
* (visibleRows, changedRows) => void
|
|
|
|
*
|
2017-01-09 11:08:05 +00:00
|
|
|
* Called when the set of visible rows changes. `visibleRows` maps
|
2015-03-05 05:06:49 +00:00
|
|
|
* { sectionID: { rowID: true }} for all the visible rows, and
|
|
|
|
* `changedRows` maps { sectionID: { rowID: true | false }} for the rows
|
|
|
|
* that have changed their visibility, with true indicating visible, and
|
|
|
|
* false indicating the view has moved out of view.
|
|
|
|
*/
|
2017-04-12 23:09:48 +00:00
|
|
|
onChangeVisibleRows: PropTypes.func,
|
2015-03-05 05:06:49 +00:00
|
|
|
/**
|
2015-12-28 15:55:39 +00:00
|
|
|
* A performance optimization for improving scroll perf of
|
2015-03-05 05:06:49 +00:00
|
|
|
* large lists, used in conjunction with overflow: 'hidden' on the row
|
2017-01-09 11:08:05 +00:00
|
|
|
* containers. This is enabled by default.
|
2015-03-05 05:06:49 +00:00
|
|
|
*/
|
2017-04-12 23:09:48 +00:00
|
|
|
removeClippedSubviews: PropTypes.bool,
|
2017-01-09 11:08:05 +00:00
|
|
|
/**
|
|
|
|
* Makes the sections headers sticky. The sticky behavior means that it
|
|
|
|
* will scroll with the content at the top of the section until it reaches
|
|
|
|
* the top of the screen, at which point it will stick to the top until it
|
2017-03-10 21:25:31 +00:00
|
|
|
* is pushed off the screen by the next section header. This property is
|
|
|
|
* not supported in conjunction with `horizontal={true}`. Only enabled by
|
|
|
|
* default on iOS because of typical platform standards.
|
2017-01-09 11:08:05 +00:00
|
|
|
*/
|
2017-04-12 23:09:48 +00:00
|
|
|
stickySectionHeadersEnabled: PropTypes.bool,
|
2016-01-06 17:16:40 +00:00
|
|
|
/**
|
|
|
|
* An array of child indices determining which children get docked to the
|
|
|
|
* top of the screen when scrolling. For example, passing
|
|
|
|
* `stickyHeaderIndices={[0]}` will cause the first child to be fixed to the
|
|
|
|
* top of the scroll view. This property is not supported in conjunction
|
|
|
|
* with `horizontal={true}`.
|
|
|
|
*/
|
2016-06-23 16:23:09 +00:00
|
|
|
stickyHeaderIndices: PropTypes.arrayOf(PropTypes.number).isRequired,
|
2016-03-15 19:13:34 +00:00
|
|
|
/**
|
|
|
|
* Flag indicating whether empty section headers should be rendered. In the future release
|
|
|
|
* empty section headers will be rendered by default, and the flag will be deprecated.
|
|
|
|
* If empty sections are not desired to be rendered their indices should be excluded from sectionID object.
|
|
|
|
*/
|
|
|
|
enableEmptySections: PropTypes.bool,
|
2015-03-05 05:06:49 +00:00
|
|
|
},
|
2015-01-30 01:10:49 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Exports some data, e.g. for perf investigations or analytics.
|
|
|
|
*/
|
|
|
|
getMetrics: function() {
|
|
|
|
return {
|
2015-07-15 17:05:13 +00:00
|
|
|
contentLength: this.scrollProperties.contentLength,
|
2017-06-13 05:32:58 +00:00
|
|
|
totalRows: this.props.enableEmptySections
|
|
|
|
? this.props.dataSource.getRowAndSectionCount()
|
|
|
|
: this.props.dataSource.getRowCount(),
|
2015-01-30 01:10:49 +00:00
|
|
|
renderedRows: this.state.curRenderedRowsCount,
|
|
|
|
visibleRows: Object.keys(this._visibleRows).length,
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2016-02-03 17:16:15 +00:00
|
|
|
* Provides a handle to the underlying scroll responder.
|
2016-04-14 13:40:54 +00:00
|
|
|
* Note that `this._scrollComponent` might not be a `ScrollView`, so we
|
2016-02-19 13:02:27 +00:00
|
|
|
* need to check that it responds to `getScrollResponder` before calling it.
|
2015-01-30 01:10:49 +00:00
|
|
|
*/
|
|
|
|
getScrollResponder: function() {
|
2016-04-14 13:40:54 +00:00
|
|
|
if (this._scrollComponent && this._scrollComponent.getScrollResponder) {
|
|
|
|
return this._scrollComponent.getScrollResponder();
|
|
|
|
}
|
2015-01-30 01:10:49 +00:00
|
|
|
},
|
|
|
|
|
2016-12-14 04:31:30 +00:00
|
|
|
getScrollableNode: function() {
|
|
|
|
if (this._scrollComponent && this._scrollComponent.getScrollableNode) {
|
|
|
|
return this._scrollComponent.getScrollableNode();
|
|
|
|
} else {
|
|
|
|
return ReactNative.findNodeHandle(this._scrollComponent);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2016-04-09 18:12:46 +00:00
|
|
|
/**
|
|
|
|
* Scrolls to a given x, y offset, either immediately or with a smooth animation.
|
|
|
|
*
|
|
|
|
* See `ScrollView#scrollTo`.
|
|
|
|
*/
|
2016-06-23 16:23:09 +00:00
|
|
|
scrollTo: function(...args: Array<mixed>) {
|
2016-04-14 13:40:54 +00:00
|
|
|
if (this._scrollComponent && this._scrollComponent.scrollTo) {
|
|
|
|
this._scrollComponent.scrollTo(...args);
|
|
|
|
}
|
2016-01-06 18:24:48 +00:00
|
|
|
},
|
|
|
|
|
2017-01-27 18:09:20 +00:00
|
|
|
/**
|
|
|
|
* If this is a vertical ListView scrolls to the bottom.
|
|
|
|
* If this is a horizontal ListView scrolls to the right.
|
|
|
|
*
|
|
|
|
* Use `scrollToEnd({animated: true})` for smooth animated scrolling,
|
|
|
|
* `scrollToEnd({animated: false})` for immediate scrolling.
|
|
|
|
* If no options are passed, `animated` defaults to true.
|
|
|
|
*
|
|
|
|
* See `ScrollView#scrollToEnd`.
|
|
|
|
*/
|
2017-06-13 05:32:58 +00:00
|
|
|
scrollToEnd: function(options?: ?{animated?: ?boolean}) {
|
2017-01-27 18:09:20 +00:00
|
|
|
if (this._scrollComponent) {
|
|
|
|
if (this._scrollComponent.scrollToEnd) {
|
|
|
|
this._scrollComponent.scrollToEnd(options);
|
|
|
|
} else {
|
|
|
|
console.warn(
|
|
|
|
'The scroll component used by the ListView does not support ' +
|
2017-06-13 05:32:58 +00:00
|
|
|
'scrollToEnd. Check the renderScrollComponent prop of your ListView.',
|
2017-01-27 18:09:20 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2017-06-06 20:00:30 +00:00
|
|
|
/**
|
|
|
|
* Displays the scroll indicators momentarily.
|
|
|
|
*
|
|
|
|
* @platform ios
|
|
|
|
*/
|
|
|
|
flashScrollIndicators: function() {
|
|
|
|
if (this._scrollComponent && this._scrollComponent.flashScrollIndicators) {
|
|
|
|
this._scrollComponent.flashScrollIndicators();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2016-06-23 16:23:09 +00:00
|
|
|
setNativeProps: function(props: Object) {
|
2016-04-14 13:40:54 +00:00
|
|
|
if (this._scrollComponent) {
|
|
|
|
this._scrollComponent.setNativeProps(props);
|
|
|
|
}
|
2015-01-30 01:10:49 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* React life cycle hooks.
|
|
|
|
*/
|
|
|
|
|
|
|
|
getDefaultProps: function() {
|
|
|
|
return {
|
|
|
|
initialListSize: DEFAULT_INITIAL_ROWS,
|
|
|
|
pageSize: DEFAULT_PAGE_SIZE,
|
2015-07-01 16:02:38 +00:00
|
|
|
renderScrollComponent: props => <ScrollView {...props} />,
|
2015-01-30 01:10:49 +00:00
|
|
|
scrollRenderAheadDistance: DEFAULT_SCROLL_RENDER_AHEAD,
|
|
|
|
onEndReachedThreshold: DEFAULT_END_REACHED_THRESHOLD,
|
2017-03-03 23:26:17 +00:00
|
|
|
stickySectionHeadersEnabled: Platform.OS === 'ios',
|
2016-01-06 17:16:40 +00:00
|
|
|
stickyHeaderIndices: [],
|
2015-01-30 01:10:49 +00:00
|
|
|
};
|
|
|
|
},
|
|
|
|
|
|
|
|
getInitialState: function() {
|
|
|
|
return {
|
|
|
|
curRenderedRowsCount: this.props.initialListSize,
|
2017-06-13 05:32:58 +00:00
|
|
|
highlightedRow: ({}: Object),
|
2015-01-30 01:10:49 +00:00
|
|
|
};
|
|
|
|
},
|
|
|
|
|
2015-07-24 00:57:42 +00:00
|
|
|
getInnerViewNode: function() {
|
2016-04-14 13:40:54 +00:00
|
|
|
return this._scrollComponent.getInnerViewNode();
|
2015-07-24 00:57:42 +00:00
|
|
|
},
|
|
|
|
|
2018-02-08 18:26:45 +00:00
|
|
|
UNSAFE_componentWillMount: function() {
|
2015-01-30 01:10:49 +00:00
|
|
|
// this data should never trigger a render pass, so don't put in state
|
|
|
|
this.scrollProperties = {
|
2015-07-15 17:05:13 +00:00
|
|
|
visibleLength: null,
|
|
|
|
contentLength: null,
|
2017-06-13 05:32:58 +00:00
|
|
|
offset: 0,
|
2015-01-30 01:10:49 +00:00
|
|
|
};
|
|
|
|
this._childFrames = [];
|
|
|
|
this._visibleRows = {};
|
2015-12-20 01:12:56 +00:00
|
|
|
this._prevRenderedRowsCount = 0;
|
2015-12-21 16:40:52 +00:00
|
|
|
this._sentEndForContentLength = null;
|
2015-01-30 01:10:49 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
componentDidMount: function() {
|
|
|
|
// do this in animation frame until componentDidMount actually runs after
|
|
|
|
// the component is laid out
|
|
|
|
this.requestAnimationFrame(() => {
|
|
|
|
this._measureAndUpdateScrollProps();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2018-02-08 18:26:45 +00:00
|
|
|
UNSAFE_componentWillReceiveProps: function(nextProps: Object) {
|
2017-06-13 05:32:58 +00:00
|
|
|
if (
|
|
|
|
this.props.dataSource !== nextProps.dataSource ||
|
|
|
|
this.props.initialListSize !== nextProps.initialListSize
|
|
|
|
) {
|
|
|
|
this.setState(
|
|
|
|
(state, props) => {
|
|
|
|
this._prevRenderedRowsCount = 0;
|
|
|
|
return {
|
|
|
|
curRenderedRowsCount: Math.min(
|
|
|
|
Math.max(state.curRenderedRowsCount, props.initialListSize),
|
|
|
|
props.enableEmptySections
|
|
|
|
? props.dataSource.getRowAndSectionCount()
|
|
|
|
: props.dataSource.getRowCount(),
|
2016-02-05 19:08:59 +00:00
|
|
|
),
|
2017-06-13 05:32:58 +00:00
|
|
|
};
|
|
|
|
},
|
|
|
|
() => this._renderMoreRowsIfNeeded(),
|
|
|
|
);
|
2015-01-30 01:10:49 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2015-06-22 20:37:25 +00:00
|
|
|
componentDidUpdate: function() {
|
2015-06-26 17:25:54 +00:00
|
|
|
this.requestAnimationFrame(() => {
|
|
|
|
this._measureAndUpdateScrollProps();
|
|
|
|
});
|
2015-06-22 20:37:25 +00:00
|
|
|
},
|
|
|
|
|
2016-06-23 16:23:09 +00:00
|
|
|
_onRowHighlighted: function(sectionID: string, rowID: string) {
|
2015-05-26 22:16:42 +00:00
|
|
|
this.setState({highlightedRow: {sectionID, rowID}});
|
|
|
|
},
|
|
|
|
|
2015-01-30 01:10:49 +00:00
|
|
|
render: function() {
|
2018-05-10 22:44:52 +00:00
|
|
|
const bodyComponents = [];
|
2015-01-30 01:10:49 +00:00
|
|
|
|
2018-05-10 22:44:52 +00:00
|
|
|
const dataSource = this.props.dataSource;
|
|
|
|
const allRowIDs = dataSource.rowIdentities;
|
|
|
|
let rowCount = 0;
|
|
|
|
const stickySectionHeaderIndices = [];
|
2015-01-30 01:10:49 +00:00
|
|
|
|
2017-03-04 04:05:50 +00:00
|
|
|
const {renderSectionHeader} = this.props;
|
|
|
|
|
2018-05-10 22:44:52 +00:00
|
|
|
const header = this.props.renderHeader && this.props.renderHeader();
|
|
|
|
const footer = this.props.renderFooter && this.props.renderFooter();
|
|
|
|
let totalIndex = header ? 1 : 0;
|
2015-01-30 01:10:49 +00:00
|
|
|
|
2018-05-10 22:44:52 +00:00
|
|
|
for (let sectionIdx = 0; sectionIdx < allRowIDs.length; sectionIdx++) {
|
|
|
|
const sectionID = dataSource.sectionIdentities[sectionIdx];
|
|
|
|
const rowIDs = allRowIDs[sectionIdx];
|
2015-01-30 01:10:49 +00:00
|
|
|
if (rowIDs.length === 0) {
|
2016-03-15 19:13:34 +00:00
|
|
|
if (this.props.enableEmptySections === undefined) {
|
2018-05-10 22:44:52 +00:00
|
|
|
const warning = require('fbjs/lib/warning');
|
2017-06-13 05:32:58 +00:00
|
|
|
warning(
|
|
|
|
false,
|
|
|
|
'In next release empty section headers will be rendered.' +
|
|
|
|
" In this release you can use 'enableEmptySections' flag to render empty section headers.",
|
|
|
|
);
|
2016-03-15 19:13:34 +00:00
|
|
|
continue;
|
|
|
|
} else {
|
2018-05-10 22:44:52 +00:00
|
|
|
const invariant = require('fbjs/lib/invariant');
|
2016-03-15 19:13:34 +00:00
|
|
|
invariant(
|
|
|
|
this.props.enableEmptySections,
|
2017-06-13 05:32:58 +00:00
|
|
|
"In next release 'enableEmptySections' flag will be deprecated, empty section headers will always be rendered." +
|
|
|
|
' If empty section headers are not desirable their indices should be excluded from sectionIDs object.' +
|
|
|
|
" In this release 'enableEmptySections' may only have value 'true' to allow empty section headers rendering.",
|
|
|
|
);
|
2016-03-15 19:13:34 +00:00
|
|
|
}
|
2015-01-30 01:10:49 +00:00
|
|
|
}
|
|
|
|
|
2017-03-04 04:05:50 +00:00
|
|
|
if (renderSectionHeader) {
|
2017-03-07 05:16:55 +00:00
|
|
|
const element = renderSectionHeader(
|
|
|
|
dataSource.getSectionHeaderData(sectionIdx),
|
2017-06-13 05:32:58 +00:00
|
|
|
sectionID,
|
2017-03-07 05:16:55 +00:00
|
|
|
);
|
|
|
|
if (element) {
|
2017-06-13 05:32:58 +00:00
|
|
|
bodyComponents.push(
|
|
|
|
React.cloneElement(element, {key: 's_' + sectionID}),
|
|
|
|
);
|
2017-03-07 05:16:55 +00:00
|
|
|
if (this.props.stickySectionHeadersEnabled) {
|
|
|
|
stickySectionHeaderIndices.push(totalIndex);
|
|
|
|
}
|
|
|
|
totalIndex++;
|
2017-01-09 11:08:05 +00:00
|
|
|
}
|
2015-01-30 01:10:49 +00:00
|
|
|
}
|
|
|
|
|
2018-05-10 22:44:52 +00:00
|
|
|
for (let rowIdx = 0; rowIdx < rowIDs.length; rowIdx++) {
|
|
|
|
const rowID = rowIDs[rowIdx];
|
|
|
|
const comboID = sectionID + '_' + rowID;
|
|
|
|
const shouldUpdateRow =
|
2017-06-13 05:32:58 +00:00
|
|
|
rowCount >= this._prevRenderedRowsCount &&
|
2015-01-30 01:10:49 +00:00
|
|
|
dataSource.rowShouldUpdate(sectionIdx, rowIdx);
|
2018-05-10 22:44:52 +00:00
|
|
|
const row = (
|
2015-01-30 01:10:49 +00:00
|
|
|
<StaticRenderer
|
|
|
|
key={'r_' + comboID}
|
|
|
|
shouldUpdate={!!shouldUpdateRow}
|
|
|
|
render={this.props.renderRow.bind(
|
|
|
|
null,
|
|
|
|
dataSource.getRowData(sectionIdx, rowIdx),
|
|
|
|
sectionID,
|
2015-05-26 22:16:42 +00:00
|
|
|
rowID,
|
2017-06-13 05:32:58 +00:00
|
|
|
this._onRowHighlighted,
|
2015-01-30 01:10:49 +00:00
|
|
|
)}
|
2017-06-13 05:32:58 +00:00
|
|
|
/>
|
|
|
|
);
|
2015-01-30 01:10:49 +00:00
|
|
|
bodyComponents.push(row);
|
|
|
|
totalIndex++;
|
2015-05-26 22:16:42 +00:00
|
|
|
|
2017-06-13 05:32:58 +00:00
|
|
|
if (
|
|
|
|
this.props.renderSeparator &&
|
|
|
|
(rowIdx !== rowIDs.length - 1 || sectionIdx === allRowIDs.length - 1)
|
|
|
|
) {
|
2018-05-10 22:44:52 +00:00
|
|
|
const adjacentRowHighlighted =
|
2017-06-13 05:32:58 +00:00
|
|
|
this.state.highlightedRow.sectionID === sectionID &&
|
|
|
|
(this.state.highlightedRow.rowID === rowID ||
|
|
|
|
this.state.highlightedRow.rowID === rowIDs[rowIdx + 1]);
|
2018-05-10 22:44:52 +00:00
|
|
|
const separator = this.props.renderSeparator(
|
2015-05-26 22:16:42 +00:00
|
|
|
sectionID,
|
|
|
|
rowID,
|
2017-06-13 05:32:58 +00:00
|
|
|
adjacentRowHighlighted,
|
2015-05-26 22:16:42 +00:00
|
|
|
);
|
2016-02-26 01:07:37 +00:00
|
|
|
if (separator) {
|
2017-09-27 06:33:30 +00:00
|
|
|
bodyComponents.push(<View key={'s_' + comboID}>{separator}</View>);
|
2016-02-26 01:07:37 +00:00
|
|
|
totalIndex++;
|
|
|
|
}
|
2015-05-26 22:16:42 +00:00
|
|
|
}
|
2015-01-30 01:10:49 +00:00
|
|
|
if (++rowCount === this.state.curRenderedRowsCount) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (rowCount >= this.state.curRenderedRowsCount) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-10 22:44:52 +00:00
|
|
|
const {renderScrollComponent, ...props} = this.props;
|
2015-03-31 03:12:32 +00:00
|
|
|
if (!props.scrollEventThrottle) {
|
|
|
|
props.scrollEventThrottle = DEFAULT_SCROLL_CALLBACK_THROTTLE;
|
2015-01-30 01:10:49 +00:00
|
|
|
}
|
2015-10-13 15:04:34 +00:00
|
|
|
if (props.removeClippedSubviews === undefined) {
|
|
|
|
props.removeClippedSubviews = true;
|
|
|
|
}
|
2015-07-01 16:02:38 +00:00
|
|
|
Object.assign(props, {
|
|
|
|
onScroll: this._onScroll,
|
2017-06-13 05:32:58 +00:00
|
|
|
stickyHeaderIndices: this.props.stickyHeaderIndices.concat(
|
|
|
|
stickySectionHeaderIndices,
|
|
|
|
),
|
2015-07-27 18:11:59 +00:00
|
|
|
|
|
|
|
// Do not pass these events downstream to ScrollView since they will be
|
|
|
|
// registered in ListView's own ScrollResponder.Mixin
|
|
|
|
onKeyboardWillShow: undefined,
|
|
|
|
onKeyboardWillHide: undefined,
|
|
|
|
onKeyboardDidShow: undefined,
|
|
|
|
onKeyboardDidHide: undefined,
|
2015-07-01 16:02:38 +00:00
|
|
|
});
|
|
|
|
|
2017-06-13 05:32:58 +00:00
|
|
|
return cloneReferencedElement(
|
|
|
|
renderScrollComponent(props),
|
|
|
|
{
|
|
|
|
ref: this._setScrollComponentRef,
|
|
|
|
onContentSizeChange: this._onContentSizeChange,
|
|
|
|
onLayout: this._onLayout,
|
2017-06-28 19:39:28 +00:00
|
|
|
DEPRECATED_sendUpdatedChildFrames:
|
|
|
|
typeof props.onChangeVisibleRows !== undefined,
|
2017-06-13 05:32:58 +00:00
|
|
|
},
|
|
|
|
header,
|
|
|
|
bodyComponents,
|
|
|
|
footer,
|
|
|
|
);
|
2015-01-30 01:10:49 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Private methods
|
|
|
|
*/
|
|
|
|
|
|
|
|
_measureAndUpdateScrollProps: function() {
|
2018-05-10 22:44:52 +00:00
|
|
|
const scrollComponent = this.getScrollResponder();
|
2015-07-13 19:27:02 +00:00
|
|
|
if (!scrollComponent || !scrollComponent.getInnerViewNode) {
|
|
|
|
return;
|
|
|
|
}
|
2015-07-20 19:32:14 +00:00
|
|
|
|
|
|
|
// RCTScrollViewManager.calculateChildFrames is not available on
|
|
|
|
// every platform
|
2017-06-13 05:32:58 +00:00
|
|
|
RCTScrollViewManager &&
|
|
|
|
RCTScrollViewManager.calculateChildFrames &&
|
2015-07-20 19:32:14 +00:00
|
|
|
RCTScrollViewManager.calculateChildFrames(
|
2016-04-08 02:43:49 +00:00
|
|
|
ReactNative.findNodeHandle(scrollComponent),
|
2015-12-20 01:12:56 +00:00
|
|
|
this._updateVisibleRows,
|
2015-07-20 19:32:14 +00:00
|
|
|
);
|
2015-01-30 01:10:49 +00:00
|
|
|
},
|
|
|
|
|
2018-05-14 07:09:10 +00:00
|
|
|
_setScrollComponentRef: function(scrollComponent) {
|
2016-04-14 13:40:54 +00:00
|
|
|
this._scrollComponent = scrollComponent;
|
|
|
|
},
|
|
|
|
|
2016-06-23 16:23:09 +00:00
|
|
|
_onContentSizeChange: function(width: number, height: number) {
|
2018-05-10 22:44:52 +00:00
|
|
|
const contentLength = !this.props.horizontal ? height : width;
|
2015-12-30 12:52:34 +00:00
|
|
|
if (contentLength !== this.scrollProperties.contentLength) {
|
|
|
|
this.scrollProperties.contentLength = contentLength;
|
|
|
|
this._updateVisibleRows();
|
|
|
|
this._renderMoreRowsIfNeeded();
|
2015-12-10 20:42:22 +00:00
|
|
|
}
|
2017-06-13 05:32:58 +00:00
|
|
|
this.props.onContentSizeChange &&
|
|
|
|
this.props.onContentSizeChange(width, height);
|
2015-11-20 15:36:23 +00:00
|
|
|
},
|
|
|
|
|
2016-06-23 16:23:09 +00:00
|
|
|
_onLayout: function(event: Object) {
|
2018-05-10 22:44:52 +00:00
|
|
|
const {width, height} = event.nativeEvent.layout;
|
|
|
|
const visibleLength = !this.props.horizontal ? height : width;
|
2015-12-30 12:52:34 +00:00
|
|
|
if (visibleLength !== this.scrollProperties.visibleLength) {
|
|
|
|
this.scrollProperties.visibleLength = visibleLength;
|
|
|
|
this._updateVisibleRows();
|
|
|
|
this._renderMoreRowsIfNeeded();
|
2016-02-01 22:52:33 +00:00
|
|
|
}
|
2015-12-30 12:52:34 +00:00
|
|
|
this.props.onLayout && this.props.onLayout(event);
|
2015-01-30 01:10:49 +00:00
|
|
|
},
|
|
|
|
|
2016-06-23 16:23:09 +00:00
|
|
|
_maybeCallOnEndReached: function(event?: Object) {
|
2017-06-13 05:32:58 +00:00
|
|
|
if (
|
|
|
|
this.props.onEndReached &&
|
|
|
|
this.scrollProperties.contentLength !== this._sentEndForContentLength &&
|
|
|
|
this._getDistanceFromEnd(this.scrollProperties) <
|
|
|
|
this.props.onEndReachedThreshold &&
|
|
|
|
this.state.curRenderedRowsCount ===
|
|
|
|
(this.props.enableEmptySections
|
|
|
|
? this.props.dataSource.getRowAndSectionCount()
|
|
|
|
: this.props.dataSource.getRowCount())
|
|
|
|
) {
|
2015-10-05 20:42:25 +00:00
|
|
|
this._sentEndForContentLength = this.scrollProperties.contentLength;
|
|
|
|
this.props.onEndReached(event);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
},
|
|
|
|
|
2015-01-30 01:10:49 +00:00
|
|
|
_renderMoreRowsIfNeeded: function() {
|
2017-06-13 05:32:58 +00:00
|
|
|
if (
|
|
|
|
this.scrollProperties.contentLength === null ||
|
2015-07-15 17:05:13 +00:00
|
|
|
this.scrollProperties.visibleLength === null ||
|
2017-06-13 05:32:58 +00:00
|
|
|
this.state.curRenderedRowsCount ===
|
|
|
|
(this.props.enableEmptySections
|
|
|
|
? this.props.dataSource.getRowAndSectionCount()
|
|
|
|
: this.props.dataSource.getRowCount())
|
|
|
|
) {
|
2015-10-05 20:42:25 +00:00
|
|
|
this._maybeCallOnEndReached();
|
2015-01-30 01:10:49 +00:00
|
|
|
return;
|
|
|
|
}
|
2016-02-01 22:52:33 +00:00
|
|
|
|
2018-05-10 22:44:52 +00:00
|
|
|
const distanceFromEnd = this._getDistanceFromEnd(this.scrollProperties);
|
2015-01-30 01:10:49 +00:00
|
|
|
if (distanceFromEnd < this.props.scrollRenderAheadDistance) {
|
|
|
|
this._pageInNewRows();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_pageInNewRows: function() {
|
2017-06-13 05:32:58 +00:00
|
|
|
this.setState(
|
|
|
|
(state, props) => {
|
2018-05-10 22:44:52 +00:00
|
|
|
const rowsToRender = Math.min(
|
2017-06-13 05:32:58 +00:00
|
|
|
state.curRenderedRowsCount + props.pageSize,
|
|
|
|
props.enableEmptySections
|
|
|
|
? props.dataSource.getRowAndSectionCount()
|
|
|
|
: props.dataSource.getRowCount(),
|
|
|
|
);
|
|
|
|
this._prevRenderedRowsCount = state.curRenderedRowsCount;
|
|
|
|
return {
|
|
|
|
curRenderedRowsCount: rowsToRender,
|
|
|
|
};
|
|
|
|
},
|
|
|
|
() => {
|
|
|
|
this._measureAndUpdateScrollProps();
|
|
|
|
this._prevRenderedRowsCount = this.state.curRenderedRowsCount;
|
|
|
|
},
|
|
|
|
);
|
2015-01-30 01:10:49 +00:00
|
|
|
},
|
|
|
|
|
2016-06-23 16:23:09 +00:00
|
|
|
_getDistanceFromEnd: function(scrollProperties: Object) {
|
2017-06-13 05:32:58 +00:00
|
|
|
return (
|
|
|
|
scrollProperties.contentLength -
|
|
|
|
scrollProperties.visibleLength -
|
|
|
|
scrollProperties.offset
|
|
|
|
);
|
2015-01-30 01:10:49 +00:00
|
|
|
},
|
|
|
|
|
2016-06-23 16:23:09 +00:00
|
|
|
_updateVisibleRows: function(updatedFrames?: Array<Object>) {
|
2015-01-30 01:10:49 +00:00
|
|
|
if (!this.props.onChangeVisibleRows) {
|
|
|
|
return; // No need to compute visible rows if there is no callback
|
|
|
|
}
|
|
|
|
if (updatedFrames) {
|
2017-06-13 05:32:58 +00:00
|
|
|
updatedFrames.forEach(newFrame => {
|
2015-06-01 20:27:21 +00:00
|
|
|
this._childFrames[newFrame.index] = merge(newFrame);
|
2015-01-30 01:10:49 +00:00
|
|
|
});
|
|
|
|
}
|
2018-05-10 22:44:52 +00:00
|
|
|
const isVertical = !this.props.horizontal;
|
|
|
|
const dataSource = this.props.dataSource;
|
|
|
|
const visibleMin = this.scrollProperties.offset;
|
|
|
|
const visibleMax = visibleMin + this.scrollProperties.visibleLength;
|
|
|
|
const allRowIDs = dataSource.rowIdentities;
|
|
|
|
|
|
|
|
const header = this.props.renderHeader && this.props.renderHeader();
|
|
|
|
let totalIndex = header ? 1 : 0;
|
|
|
|
let visibilityChanged = false;
|
|
|
|
const changedRows = {};
|
|
|
|
for (let sectionIdx = 0; sectionIdx < allRowIDs.length; sectionIdx++) {
|
|
|
|
const rowIDs = allRowIDs[sectionIdx];
|
2015-01-30 01:10:49 +00:00
|
|
|
if (rowIDs.length === 0) {
|
|
|
|
continue;
|
|
|
|
}
|
2018-05-10 22:44:52 +00:00
|
|
|
const sectionID = dataSource.sectionIdentities[sectionIdx];
|
2015-01-30 01:10:49 +00:00
|
|
|
if (this.props.renderSectionHeader) {
|
|
|
|
totalIndex++;
|
|
|
|
}
|
2018-05-10 22:44:52 +00:00
|
|
|
let visibleSection = this._visibleRows[sectionID];
|
2015-01-30 01:10:49 +00:00
|
|
|
if (!visibleSection) {
|
|
|
|
visibleSection = {};
|
|
|
|
}
|
2018-05-10 22:44:52 +00:00
|
|
|
for (let rowIdx = 0; rowIdx < rowIDs.length; rowIdx++) {
|
|
|
|
const rowID = rowIDs[rowIdx];
|
|
|
|
const frame = this._childFrames[totalIndex];
|
2015-01-30 01:10:49 +00:00
|
|
|
totalIndex++;
|
2017-06-13 05:32:58 +00:00
|
|
|
if (
|
|
|
|
this.props.renderSeparator &&
|
|
|
|
(rowIdx !== rowIDs.length - 1 || sectionIdx === allRowIDs.length - 1)
|
|
|
|
) {
|
2016-05-20 15:04:10 +00:00
|
|
|
totalIndex++;
|
|
|
|
}
|
2015-01-30 01:10:49 +00:00
|
|
|
if (!frame) {
|
|
|
|
break;
|
|
|
|
}
|
2018-05-10 22:44:52 +00:00
|
|
|
const rowVisible = visibleSection[rowID];
|
|
|
|
const min = isVertical ? frame.y : frame.x;
|
|
|
|
const max = min + (isVertical ? frame.height : frame.width);
|
2017-06-13 05:32:58 +00:00
|
|
|
if ((!min && !max) || min === max) {
|
2016-05-17 01:18:58 +00:00
|
|
|
break;
|
|
|
|
}
|
2015-07-15 17:05:13 +00:00
|
|
|
if (min > visibleMax || max < visibleMin) {
|
2015-01-30 01:10:49 +00:00
|
|
|
if (rowVisible) {
|
|
|
|
visibilityChanged = true;
|
|
|
|
delete visibleSection[rowID];
|
|
|
|
if (!changedRows[sectionID]) {
|
|
|
|
changedRows[sectionID] = {};
|
|
|
|
}
|
|
|
|
changedRows[sectionID][rowID] = false;
|
|
|
|
}
|
|
|
|
} else if (!rowVisible) {
|
|
|
|
visibilityChanged = true;
|
|
|
|
visibleSection[rowID] = true;
|
|
|
|
if (!changedRows[sectionID]) {
|
|
|
|
changedRows[sectionID] = {};
|
|
|
|
}
|
|
|
|
changedRows[sectionID][rowID] = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!isEmpty(visibleSection)) {
|
|
|
|
this._visibleRows[sectionID] = visibleSection;
|
|
|
|
} else if (this._visibleRows[sectionID]) {
|
|
|
|
delete this._visibleRows[sectionID];
|
|
|
|
}
|
|
|
|
}
|
2017-06-13 05:32:58 +00:00
|
|
|
visibilityChanged &&
|
|
|
|
this.props.onChangeVisibleRows(this._visibleRows, changedRows);
|
2015-01-30 01:10:49 +00:00
|
|
|
},
|
|
|
|
|
2016-06-23 16:23:09 +00:00
|
|
|
_onScroll: function(e: Object) {
|
2018-05-10 22:44:52 +00:00
|
|
|
const isVertical = !this.props.horizontal;
|
2017-06-13 05:32:58 +00:00
|
|
|
this.scrollProperties.visibleLength =
|
|
|
|
e.nativeEvent.layoutMeasurement[isVertical ? 'height' : 'width'];
|
|
|
|
this.scrollProperties.contentLength =
|
|
|
|
e.nativeEvent.contentSize[isVertical ? 'height' : 'width'];
|
|
|
|
this.scrollProperties.offset =
|
|
|
|
e.nativeEvent.contentOffset[isVertical ? 'y' : 'x'];
|
2015-07-07 14:38:24 +00:00
|
|
|
this._updateVisibleRows(e.nativeEvent.updatedChildFrames);
|
2015-10-05 20:42:25 +00:00
|
|
|
if (!this._maybeCallOnEndReached(e)) {
|
2015-01-30 01:10:49 +00:00
|
|
|
this._renderMoreRowsIfNeeded();
|
|
|
|
}
|
|
|
|
|
2017-06-13 05:32:58 +00:00
|
|
|
if (
|
|
|
|
this.props.onEndReached &&
|
|
|
|
this._getDistanceFromEnd(this.scrollProperties) >
|
|
|
|
this.props.onEndReachedThreshold
|
|
|
|
) {
|
2015-11-04 19:25:47 +00:00
|
|
|
// Scrolled out of the end zone, so it should be able to trigger again.
|
|
|
|
this._sentEndForContentLength = null;
|
|
|
|
}
|
|
|
|
|
2015-01-30 01:10:49 +00:00
|
|
|
this.props.onScroll && this.props.onScroll(e);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2018-05-14 07:09:10 +00:00
|
|
|
module.exports = ((ListView: any): Class<InternalListViewType<Props>>);
|