Better ListView - FlatList
Summary:
We really need a better list view - so here it is!
Main changes from existing `ListView`:
* Items are "virtualized" to limit memory - that is, items outside of the render window are unmounted and their memory is reclaimed. This means that instance state is not preserved when items scroll out of the render window.
* No `DataSource` - just a simple `data` prop of shape `Array<any>`. By default, they are expected to be of the shape `{key: string}` but a custom `rowExtractor` function can be provided for different shapes, e.g. graphql data where you want to map `id` to `key`. Note the underlying `VirtualizedList` is much more flexible.
* Fancy `scrollTo` functionality: `scrollToEnd`, `scrollToIndex`, and `scrollToItem` in addition to the normal `scrollToOffset`.
* Built-in pull to refresh support - set set the `onRefresh` and `refreshing` props.
* Rendering additional rows is usually done with low priority, after any interactions/animations complete, unless we're about to run out of rendered content. This should help apps feel more responsive.
* Component props replace render functions, e.g. `ItemComponent: ReactClass<{item: Item, index: number}>` replaces `renderRow: (...) => React.Element<*>`
* Supports dynamic items automatically by using `onLayout`, or `getItemLayout` can be provided for a perf boost and smoother `scrollToIndex` and scroll bar behavior.
* Visibility callback replaced with more powerful viewability callback and works in vertical and horizontal mode on at least Android and iOS, but probably other platforms as well. Extra power comes from the `viewablePercentThreshold` that lets the client decide when an item should be considered viewable.
Demo:
https://www.facebook.com/groups/576288835853049/permalink/753923058089625/
Reviewed By: yungsters
Differential Revision: D4412469
fbshipit-source-id: e2d891490bf76fe14df49294ecddf78a58adcf23
2017-02-04 18:25:32 +00:00
|
|
|
/**
|
2017-03-24 21:18:39 +00:00
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
Better ListView - FlatList
Summary:
We really need a better list view - so here it is!
Main changes from existing `ListView`:
* Items are "virtualized" to limit memory - that is, items outside of the render window are unmounted and their memory is reclaimed. This means that instance state is not preserved when items scroll out of the render window.
* No `DataSource` - just a simple `data` prop of shape `Array<any>`. By default, they are expected to be of the shape `{key: string}` but a custom `rowExtractor` function can be provided for different shapes, e.g. graphql data where you want to map `id` to `key`. Note the underlying `VirtualizedList` is much more flexible.
* Fancy `scrollTo` functionality: `scrollToEnd`, `scrollToIndex`, and `scrollToItem` in addition to the normal `scrollToOffset`.
* Built-in pull to refresh support - set set the `onRefresh` and `refreshing` props.
* Rendering additional rows is usually done with low priority, after any interactions/animations complete, unless we're about to run out of rendered content. This should help apps feel more responsive.
* Component props replace render functions, e.g. `ItemComponent: ReactClass<{item: Item, index: number}>` replaces `renderRow: (...) => React.Element<*>`
* Supports dynamic items automatically by using `onLayout`, or `getItemLayout` can be provided for a perf boost and smoother `scrollToIndex` and scroll bar behavior.
* Visibility callback replaced with more powerful viewability callback and works in vertical and horizontal mode on at least Android and iOS, but probably other platforms as well. Extra power comes from the `viewablePercentThreshold` that lets the client decide when an item should be considered viewable.
Demo:
https://www.facebook.com/groups/576288835853049/permalink/753923058089625/
Reviewed By: yungsters
Differential Revision: D4412469
fbshipit-source-id: e2d891490bf76fe14df49294ecddf78a58adcf23
2017-02-04 18:25:32 +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.
|
Better ListView - FlatList
Summary:
We really need a better list view - so here it is!
Main changes from existing `ListView`:
* Items are "virtualized" to limit memory - that is, items outside of the render window are unmounted and their memory is reclaimed. This means that instance state is not preserved when items scroll out of the render window.
* No `DataSource` - just a simple `data` prop of shape `Array<any>`. By default, they are expected to be of the shape `{key: string}` but a custom `rowExtractor` function can be provided for different shapes, e.g. graphql data where you want to map `id` to `key`. Note the underlying `VirtualizedList` is much more flexible.
* Fancy `scrollTo` functionality: `scrollToEnd`, `scrollToIndex`, and `scrollToItem` in addition to the normal `scrollToOffset`.
* Built-in pull to refresh support - set set the `onRefresh` and `refreshing` props.
* Rendering additional rows is usually done with low priority, after any interactions/animations complete, unless we're about to run out of rendered content. This should help apps feel more responsive.
* Component props replace render functions, e.g. `ItemComponent: ReactClass<{item: Item, index: number}>` replaces `renderRow: (...) => React.Element<*>`
* Supports dynamic items automatically by using `onLayout`, or `getItemLayout` can be provided for a perf boost and smoother `scrollToIndex` and scroll bar behavior.
* Visibility callback replaced with more powerful viewability callback and works in vertical and horizontal mode on at least Android and iOS, but probably other platforms as well. Extra power comes from the `viewablePercentThreshold` that lets the client decide when an item should be considered viewable.
Demo:
https://www.facebook.com/groups/576288835853049/permalink/753923058089625/
Reviewed By: yungsters
Differential Revision: D4412469
fbshipit-source-id: e2d891490bf76fe14df49294ecddf78a58adcf23
2017-02-04 18:25:32 +00:00
|
|
|
*
|
2017-06-13 05:32:58 +00:00
|
|
|
*
|
|
|
|
* @format
|
2017-11-02 13:14:11 +00:00
|
|
|
* @emails oncall+react_native
|
Better ListView - FlatList
Summary:
We really need a better list view - so here it is!
Main changes from existing `ListView`:
* Items are "virtualized" to limit memory - that is, items outside of the render window are unmounted and their memory is reclaimed. This means that instance state is not preserved when items scroll out of the render window.
* No `DataSource` - just a simple `data` prop of shape `Array<any>`. By default, they are expected to be of the shape `{key: string}` but a custom `rowExtractor` function can be provided for different shapes, e.g. graphql data where you want to map `id` to `key`. Note the underlying `VirtualizedList` is much more flexible.
* Fancy `scrollTo` functionality: `scrollToEnd`, `scrollToIndex`, and `scrollToItem` in addition to the normal `scrollToOffset`.
* Built-in pull to refresh support - set set the `onRefresh` and `refreshing` props.
* Rendering additional rows is usually done with low priority, after any interactions/animations complete, unless we're about to run out of rendered content. This should help apps feel more responsive.
* Component props replace render functions, e.g. `ItemComponent: ReactClass<{item: Item, index: number}>` replaces `renderRow: (...) => React.Element<*>`
* Supports dynamic items automatically by using `onLayout`, or `getItemLayout` can be provided for a perf boost and smoother `scrollToIndex` and scroll bar behavior.
* Visibility callback replaced with more powerful viewability callback and works in vertical and horizontal mode on at least Android and iOS, but probably other platforms as well. Extra power comes from the `viewablePercentThreshold` that lets the client decide when an item should be considered viewable.
Demo:
https://www.facebook.com/groups/576288835853049/permalink/753923058089625/
Reviewed By: yungsters
Differential Revision: D4412469
fbshipit-source-id: e2d891490bf76fe14df49294ecddf78a58adcf23
2017-02-04 18:25:32 +00:00
|
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const ViewabilityHelper = require('ViewabilityHelper');
|
|
|
|
|
|
|
|
let rowFrames;
|
|
|
|
let data;
|
|
|
|
function getFrameMetrics(index: number) {
|
|
|
|
const frame = rowFrames[data[index].key];
|
|
|
|
return {length: frame.height, offset: frame.y};
|
|
|
|
}
|
2017-02-28 10:08:58 +00:00
|
|
|
function createViewToken(index: number, isViewable: boolean) {
|
2017-02-22 01:05:25 +00:00
|
|
|
return {key: data[index].key, isViewable};
|
|
|
|
}
|
Better ListView - FlatList
Summary:
We really need a better list view - so here it is!
Main changes from existing `ListView`:
* Items are "virtualized" to limit memory - that is, items outside of the render window are unmounted and their memory is reclaimed. This means that instance state is not preserved when items scroll out of the render window.
* No `DataSource` - just a simple `data` prop of shape `Array<any>`. By default, they are expected to be of the shape `{key: string}` but a custom `rowExtractor` function can be provided for different shapes, e.g. graphql data where you want to map `id` to `key`. Note the underlying `VirtualizedList` is much more flexible.
* Fancy `scrollTo` functionality: `scrollToEnd`, `scrollToIndex`, and `scrollToItem` in addition to the normal `scrollToOffset`.
* Built-in pull to refresh support - set set the `onRefresh` and `refreshing` props.
* Rendering additional rows is usually done with low priority, after any interactions/animations complete, unless we're about to run out of rendered content. This should help apps feel more responsive.
* Component props replace render functions, e.g. `ItemComponent: ReactClass<{item: Item, index: number}>` replaces `renderRow: (...) => React.Element<*>`
* Supports dynamic items automatically by using `onLayout`, or `getItemLayout` can be provided for a perf boost and smoother `scrollToIndex` and scroll bar behavior.
* Visibility callback replaced with more powerful viewability callback and works in vertical and horizontal mode on at least Android and iOS, but probably other platforms as well. Extra power comes from the `viewablePercentThreshold` that lets the client decide when an item should be considered viewable.
Demo:
https://www.facebook.com/groups/576288835853049/permalink/753923058089625/
Reviewed By: yungsters
Differential Revision: D4412469
fbshipit-source-id: e2d891490bf76fe14df49294ecddf78a58adcf23
2017-02-04 18:25:32 +00:00
|
|
|
|
|
|
|
describe('computeViewableItems', function() {
|
|
|
|
it('returns all 4 entirely visible rows as viewable', function() {
|
2017-06-13 05:32:58 +00:00
|
|
|
const helper = new ViewabilityHelper({
|
|
|
|
viewAreaCoveragePercentThreshold: 50,
|
|
|
|
});
|
Better ListView - FlatList
Summary:
We really need a better list view - so here it is!
Main changes from existing `ListView`:
* Items are "virtualized" to limit memory - that is, items outside of the render window are unmounted and their memory is reclaimed. This means that instance state is not preserved when items scroll out of the render window.
* No `DataSource` - just a simple `data` prop of shape `Array<any>`. By default, they are expected to be of the shape `{key: string}` but a custom `rowExtractor` function can be provided for different shapes, e.g. graphql data where you want to map `id` to `key`. Note the underlying `VirtualizedList` is much more flexible.
* Fancy `scrollTo` functionality: `scrollToEnd`, `scrollToIndex`, and `scrollToItem` in addition to the normal `scrollToOffset`.
* Built-in pull to refresh support - set set the `onRefresh` and `refreshing` props.
* Rendering additional rows is usually done with low priority, after any interactions/animations complete, unless we're about to run out of rendered content. This should help apps feel more responsive.
* Component props replace render functions, e.g. `ItemComponent: ReactClass<{item: Item, index: number}>` replaces `renderRow: (...) => React.Element<*>`
* Supports dynamic items automatically by using `onLayout`, or `getItemLayout` can be provided for a perf boost and smoother `scrollToIndex` and scroll bar behavior.
* Visibility callback replaced with more powerful viewability callback and works in vertical and horizontal mode on at least Android and iOS, but probably other platforms as well. Extra power comes from the `viewablePercentThreshold` that lets the client decide when an item should be considered viewable.
Demo:
https://www.facebook.com/groups/576288835853049/permalink/753923058089625/
Reviewed By: yungsters
Differential Revision: D4412469
fbshipit-source-id: e2d891490bf76fe14df49294ecddf78a58adcf23
2017-02-04 18:25:32 +00:00
|
|
|
rowFrames = {
|
|
|
|
a: {y: 0, height: 50},
|
|
|
|
b: {y: 50, height: 50},
|
|
|
|
c: {y: 100, height: 50},
|
|
|
|
d: {y: 150, height: 50},
|
|
|
|
};
|
|
|
|
data = [{key: 'a'}, {key: 'b'}, {key: 'c'}, {key: 'd'}];
|
2017-06-13 05:32:58 +00:00
|
|
|
expect(
|
|
|
|
helper.computeViewableItems(data.length, 0, 200, getFrameMetrics),
|
|
|
|
).toEqual([0, 1, 2, 3]);
|
Better ListView - FlatList
Summary:
We really need a better list view - so here it is!
Main changes from existing `ListView`:
* Items are "virtualized" to limit memory - that is, items outside of the render window are unmounted and their memory is reclaimed. This means that instance state is not preserved when items scroll out of the render window.
* No `DataSource` - just a simple `data` prop of shape `Array<any>`. By default, they are expected to be of the shape `{key: string}` but a custom `rowExtractor` function can be provided for different shapes, e.g. graphql data where you want to map `id` to `key`. Note the underlying `VirtualizedList` is much more flexible.
* Fancy `scrollTo` functionality: `scrollToEnd`, `scrollToIndex`, and `scrollToItem` in addition to the normal `scrollToOffset`.
* Built-in pull to refresh support - set set the `onRefresh` and `refreshing` props.
* Rendering additional rows is usually done with low priority, after any interactions/animations complete, unless we're about to run out of rendered content. This should help apps feel more responsive.
* Component props replace render functions, e.g. `ItemComponent: ReactClass<{item: Item, index: number}>` replaces `renderRow: (...) => React.Element<*>`
* Supports dynamic items automatically by using `onLayout`, or `getItemLayout` can be provided for a perf boost and smoother `scrollToIndex` and scroll bar behavior.
* Visibility callback replaced with more powerful viewability callback and works in vertical and horizontal mode on at least Android and iOS, but probably other platforms as well. Extra power comes from the `viewablePercentThreshold` that lets the client decide when an item should be considered viewable.
Demo:
https://www.facebook.com/groups/576288835853049/permalink/753923058089625/
Reviewed By: yungsters
Differential Revision: D4412469
fbshipit-source-id: e2d891490bf76fe14df49294ecddf78a58adcf23
2017-02-04 18:25:32 +00:00
|
|
|
});
|
|
|
|
|
2017-06-13 05:32:58 +00:00
|
|
|
it('returns top 2 rows as viewable (1. entirely visible and 2. majority)', function() {
|
|
|
|
const helper = new ViewabilityHelper({
|
|
|
|
viewAreaCoveragePercentThreshold: 50,
|
|
|
|
});
|
|
|
|
rowFrames = {
|
|
|
|
a: {y: 0, height: 50},
|
|
|
|
b: {y: 50, height: 150},
|
|
|
|
c: {y: 200, height: 50},
|
|
|
|
d: {y: 250, height: 50},
|
|
|
|
};
|
|
|
|
data = [{key: 'a'}, {key: 'b'}, {key: 'c'}, {key: 'd'}];
|
|
|
|
expect(
|
|
|
|
helper.computeViewableItems(data.length, 0, 200, getFrameMetrics),
|
|
|
|
).toEqual([0, 1]);
|
Better ListView - FlatList
Summary:
We really need a better list view - so here it is!
Main changes from existing `ListView`:
* Items are "virtualized" to limit memory - that is, items outside of the render window are unmounted and their memory is reclaimed. This means that instance state is not preserved when items scroll out of the render window.
* No `DataSource` - just a simple `data` prop of shape `Array<any>`. By default, they are expected to be of the shape `{key: string}` but a custom `rowExtractor` function can be provided for different shapes, e.g. graphql data where you want to map `id` to `key`. Note the underlying `VirtualizedList` is much more flexible.
* Fancy `scrollTo` functionality: `scrollToEnd`, `scrollToIndex`, and `scrollToItem` in addition to the normal `scrollToOffset`.
* Built-in pull to refresh support - set set the `onRefresh` and `refreshing` props.
* Rendering additional rows is usually done with low priority, after any interactions/animations complete, unless we're about to run out of rendered content. This should help apps feel more responsive.
* Component props replace render functions, e.g. `ItemComponent: ReactClass<{item: Item, index: number}>` replaces `renderRow: (...) => React.Element<*>`
* Supports dynamic items automatically by using `onLayout`, or `getItemLayout` can be provided for a perf boost and smoother `scrollToIndex` and scroll bar behavior.
* Visibility callback replaced with more powerful viewability callback and works in vertical and horizontal mode on at least Android and iOS, but probably other platforms as well. Extra power comes from the `viewablePercentThreshold` that lets the client decide when an item should be considered viewable.
Demo:
https://www.facebook.com/groups/576288835853049/permalink/753923058089625/
Reviewed By: yungsters
Differential Revision: D4412469
fbshipit-source-id: e2d891490bf76fe14df49294ecddf78a58adcf23
2017-02-04 18:25:32 +00:00
|
|
|
});
|
|
|
|
|
2017-06-13 05:32:58 +00:00
|
|
|
it('returns only 2nd row as viewable (majority)', function() {
|
|
|
|
const helper = new ViewabilityHelper({
|
|
|
|
viewAreaCoveragePercentThreshold: 50,
|
|
|
|
});
|
|
|
|
rowFrames = {
|
|
|
|
a: {y: 0, height: 50},
|
|
|
|
b: {y: 50, height: 150},
|
|
|
|
c: {y: 200, height: 50},
|
|
|
|
d: {y: 250, height: 50},
|
|
|
|
};
|
|
|
|
data = [{key: 'a'}, {key: 'b'}, {key: 'c'}, {key: 'd'}];
|
|
|
|
expect(
|
|
|
|
helper.computeViewableItems(data.length, 25, 200, getFrameMetrics),
|
|
|
|
).toEqual([1]);
|
Better ListView - FlatList
Summary:
We really need a better list view - so here it is!
Main changes from existing `ListView`:
* Items are "virtualized" to limit memory - that is, items outside of the render window are unmounted and their memory is reclaimed. This means that instance state is not preserved when items scroll out of the render window.
* No `DataSource` - just a simple `data` prop of shape `Array<any>`. By default, they are expected to be of the shape `{key: string}` but a custom `rowExtractor` function can be provided for different shapes, e.g. graphql data where you want to map `id` to `key`. Note the underlying `VirtualizedList` is much more flexible.
* Fancy `scrollTo` functionality: `scrollToEnd`, `scrollToIndex`, and `scrollToItem` in addition to the normal `scrollToOffset`.
* Built-in pull to refresh support - set set the `onRefresh` and `refreshing` props.
* Rendering additional rows is usually done with low priority, after any interactions/animations complete, unless we're about to run out of rendered content. This should help apps feel more responsive.
* Component props replace render functions, e.g. `ItemComponent: ReactClass<{item: Item, index: number}>` replaces `renderRow: (...) => React.Element<*>`
* Supports dynamic items automatically by using `onLayout`, or `getItemLayout` can be provided for a perf boost and smoother `scrollToIndex` and scroll bar behavior.
* Visibility callback replaced with more powerful viewability callback and works in vertical and horizontal mode on at least Android and iOS, but probably other platforms as well. Extra power comes from the `viewablePercentThreshold` that lets the client decide when an item should be considered viewable.
Demo:
https://www.facebook.com/groups/576288835853049/permalink/753923058089625/
Reviewed By: yungsters
Differential Revision: D4412469
fbshipit-source-id: e2d891490bf76fe14df49294ecddf78a58adcf23
2017-02-04 18:25:32 +00:00
|
|
|
});
|
|
|
|
|
2017-06-13 05:32:58 +00:00
|
|
|
it('handles empty input', function() {
|
|
|
|
const helper = new ViewabilityHelper({
|
|
|
|
viewAreaCoveragePercentThreshold: 50,
|
|
|
|
});
|
|
|
|
rowFrames = {};
|
|
|
|
data = [];
|
|
|
|
expect(
|
|
|
|
helper.computeViewableItems(data.length, 0, 200, getFrameMetrics),
|
|
|
|
).toEqual([]);
|
Better ListView - FlatList
Summary:
We really need a better list view - so here it is!
Main changes from existing `ListView`:
* Items are "virtualized" to limit memory - that is, items outside of the render window are unmounted and their memory is reclaimed. This means that instance state is not preserved when items scroll out of the render window.
* No `DataSource` - just a simple `data` prop of shape `Array<any>`. By default, they are expected to be of the shape `{key: string}` but a custom `rowExtractor` function can be provided for different shapes, e.g. graphql data where you want to map `id` to `key`. Note the underlying `VirtualizedList` is much more flexible.
* Fancy `scrollTo` functionality: `scrollToEnd`, `scrollToIndex`, and `scrollToItem` in addition to the normal `scrollToOffset`.
* Built-in pull to refresh support - set set the `onRefresh` and `refreshing` props.
* Rendering additional rows is usually done with low priority, after any interactions/animations complete, unless we're about to run out of rendered content. This should help apps feel more responsive.
* Component props replace render functions, e.g. `ItemComponent: ReactClass<{item: Item, index: number}>` replaces `renderRow: (...) => React.Element<*>`
* Supports dynamic items automatically by using `onLayout`, or `getItemLayout` can be provided for a perf boost and smoother `scrollToIndex` and scroll bar behavior.
* Visibility callback replaced with more powerful viewability callback and works in vertical and horizontal mode on at least Android and iOS, but probably other platforms as well. Extra power comes from the `viewablePercentThreshold` that lets the client decide when an item should be considered viewable.
Demo:
https://www.facebook.com/groups/576288835853049/permalink/753923058089625/
Reviewed By: yungsters
Differential Revision: D4412469
fbshipit-source-id: e2d891490bf76fe14df49294ecddf78a58adcf23
2017-02-04 18:25:32 +00:00
|
|
|
});
|
2017-02-22 01:05:25 +00:00
|
|
|
|
2017-06-13 05:32:58 +00:00
|
|
|
it('handles different view area coverage percent thresholds', function() {
|
|
|
|
rowFrames = {
|
|
|
|
a: {y: 0, height: 50},
|
|
|
|
b: {y: 50, height: 150},
|
|
|
|
c: {y: 200, height: 500},
|
|
|
|
d: {y: 700, height: 50},
|
|
|
|
};
|
|
|
|
data = [{key: 'a'}, {key: 'b'}, {key: 'c'}, {key: 'd'}];
|
2017-02-22 01:05:25 +00:00
|
|
|
|
2017-06-13 05:32:58 +00:00
|
|
|
let helper = new ViewabilityHelper({viewAreaCoveragePercentThreshold: 0});
|
|
|
|
expect(
|
|
|
|
helper.computeViewableItems(data.length, 0, 50, getFrameMetrics),
|
|
|
|
).toEqual([0]);
|
|
|
|
expect(
|
|
|
|
helper.computeViewableItems(data.length, 1, 50, getFrameMetrics),
|
|
|
|
).toEqual([0, 1]);
|
|
|
|
expect(
|
|
|
|
helper.computeViewableItems(data.length, 199, 50, getFrameMetrics),
|
|
|
|
).toEqual([1, 2]);
|
|
|
|
expect(
|
|
|
|
helper.computeViewableItems(data.length, 250, 50, getFrameMetrics),
|
|
|
|
).toEqual([2]);
|
2017-02-22 01:05:25 +00:00
|
|
|
|
2017-06-13 05:32:58 +00:00
|
|
|
helper = new ViewabilityHelper({viewAreaCoveragePercentThreshold: 100});
|
|
|
|
expect(
|
|
|
|
helper.computeViewableItems(data.length, 0, 200, getFrameMetrics),
|
|
|
|
).toEqual([0, 1]);
|
|
|
|
expect(
|
|
|
|
helper.computeViewableItems(data.length, 1, 200, getFrameMetrics),
|
|
|
|
).toEqual([1]);
|
|
|
|
expect(
|
|
|
|
helper.computeViewableItems(data.length, 400, 200, getFrameMetrics),
|
|
|
|
).toEqual([2]);
|
|
|
|
expect(
|
|
|
|
helper.computeViewableItems(data.length, 600, 200, getFrameMetrics),
|
|
|
|
).toEqual([3]);
|
2017-02-28 10:08:53 +00:00
|
|
|
|
2017-06-13 05:32:58 +00:00
|
|
|
helper = new ViewabilityHelper({viewAreaCoveragePercentThreshold: 10});
|
|
|
|
expect(
|
|
|
|
helper.computeViewableItems(data.length, 30, 200, getFrameMetrics),
|
|
|
|
).toEqual([0, 1, 2]);
|
|
|
|
expect(
|
|
|
|
helper.computeViewableItems(data.length, 31, 200, getFrameMetrics),
|
|
|
|
).toEqual([1, 2]);
|
2017-02-22 01:05:25 +00:00
|
|
|
});
|
|
|
|
|
2017-06-13 05:32:58 +00:00
|
|
|
it('handles different item visible percent thresholds', function() {
|
|
|
|
rowFrames = {
|
|
|
|
a: {y: 0, height: 50},
|
|
|
|
b: {y: 50, height: 150},
|
|
|
|
c: {y: 200, height: 50},
|
|
|
|
d: {y: 250, height: 50},
|
|
|
|
};
|
|
|
|
data = [{key: 'a'}, {key: 'b'}, {key: 'c'}, {key: 'd'}];
|
|
|
|
let helper = new ViewabilityHelper({itemVisiblePercentThreshold: 0});
|
|
|
|
expect(
|
|
|
|
helper.computeViewableItems(data.length, 0, 50, getFrameMetrics),
|
|
|
|
).toEqual([0]);
|
|
|
|
expect(
|
|
|
|
helper.computeViewableItems(data.length, 1, 50, getFrameMetrics),
|
|
|
|
).toEqual([0, 1]);
|
2017-02-28 10:08:53 +00:00
|
|
|
|
2017-06-13 05:32:58 +00:00
|
|
|
helper = new ViewabilityHelper({itemVisiblePercentThreshold: 100});
|
|
|
|
expect(
|
|
|
|
helper.computeViewableItems(data.length, 0, 250, getFrameMetrics),
|
|
|
|
).toEqual([0, 1, 2]);
|
|
|
|
expect(
|
|
|
|
helper.computeViewableItems(data.length, 1, 250, getFrameMetrics),
|
|
|
|
).toEqual([1, 2]);
|
2017-02-28 10:08:53 +00:00
|
|
|
|
2017-06-13 05:32:58 +00:00
|
|
|
helper = new ViewabilityHelper({itemVisiblePercentThreshold: 10});
|
|
|
|
expect(
|
|
|
|
helper.computeViewableItems(data.length, 184, 20, getFrameMetrics),
|
|
|
|
).toEqual([1]);
|
|
|
|
expect(
|
|
|
|
helper.computeViewableItems(data.length, 185, 20, getFrameMetrics),
|
|
|
|
).toEqual([1, 2]);
|
|
|
|
expect(
|
|
|
|
helper.computeViewableItems(data.length, 186, 20, getFrameMetrics),
|
|
|
|
).toEqual([2]);
|
2017-02-22 01:05:25 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('onUpdate', function() {
|
2017-06-13 05:32:58 +00:00
|
|
|
it('returns 1 visible row as viewable then scrolls away', function() {
|
|
|
|
const helper = new ViewabilityHelper();
|
|
|
|
rowFrames = {
|
|
|
|
a: {y: 0, height: 50},
|
|
|
|
};
|
|
|
|
data = [{key: 'a'}];
|
|
|
|
const onViewableItemsChanged = jest.fn();
|
|
|
|
helper.onUpdate(
|
|
|
|
data.length,
|
|
|
|
0,
|
|
|
|
200,
|
|
|
|
getFrameMetrics,
|
|
|
|
createViewToken,
|
|
|
|
onViewableItemsChanged,
|
|
|
|
);
|
|
|
|
expect(onViewableItemsChanged.mock.calls.length).toBe(1);
|
|
|
|
expect(onViewableItemsChanged.mock.calls[0][0]).toEqual({
|
|
|
|
changed: [{isViewable: true, key: 'a'}],
|
2017-09-06 01:33:44 +00:00
|
|
|
viewabilityConfig: {viewAreaCoveragePercentThreshold: 0},
|
2017-06-13 05:32:58 +00:00
|
|
|
viewableItems: [{isViewable: true, key: 'a'}],
|
|
|
|
});
|
|
|
|
helper.onUpdate(
|
|
|
|
data.length,
|
|
|
|
0,
|
|
|
|
200,
|
|
|
|
getFrameMetrics,
|
|
|
|
createViewToken,
|
|
|
|
onViewableItemsChanged,
|
|
|
|
);
|
|
|
|
expect(onViewableItemsChanged.mock.calls.length).toBe(1); // nothing changed!
|
|
|
|
helper.onUpdate(
|
|
|
|
data.length,
|
|
|
|
100,
|
|
|
|
200,
|
|
|
|
getFrameMetrics,
|
|
|
|
createViewToken,
|
|
|
|
onViewableItemsChanged,
|
|
|
|
);
|
|
|
|
expect(onViewableItemsChanged.mock.calls.length).toBe(2);
|
|
|
|
expect(onViewableItemsChanged.mock.calls[1][0]).toEqual({
|
|
|
|
changed: [{isViewable: false, key: 'a'}],
|
2017-09-06 01:33:44 +00:00
|
|
|
viewabilityConfig: {viewAreaCoveragePercentThreshold: 0},
|
2017-06-13 05:32:58 +00:00
|
|
|
viewableItems: [],
|
|
|
|
});
|
|
|
|
});
|
2017-02-28 10:08:53 +00:00
|
|
|
|
2017-06-13 05:32:58 +00:00
|
|
|
it('returns 1st visible row then 1st and 2nd then just 2nd', function() {
|
|
|
|
const helper = new ViewabilityHelper();
|
|
|
|
rowFrames = {
|
|
|
|
a: {y: 0, height: 200},
|
|
|
|
b: {y: 200, height: 200},
|
|
|
|
};
|
|
|
|
data = [{key: 'a'}, {key: 'b'}];
|
|
|
|
const onViewableItemsChanged = jest.fn();
|
|
|
|
helper.onUpdate(
|
|
|
|
data.length,
|
|
|
|
0,
|
|
|
|
200,
|
|
|
|
getFrameMetrics,
|
|
|
|
createViewToken,
|
|
|
|
onViewableItemsChanged,
|
|
|
|
);
|
|
|
|
expect(onViewableItemsChanged.mock.calls.length).toBe(1);
|
|
|
|
expect(onViewableItemsChanged.mock.calls[0][0]).toEqual({
|
|
|
|
changed: [{isViewable: true, key: 'a'}],
|
2017-09-06 01:33:44 +00:00
|
|
|
viewabilityConfig: {viewAreaCoveragePercentThreshold: 0},
|
2017-06-13 05:32:58 +00:00
|
|
|
viewableItems: [{isViewable: true, key: 'a'}],
|
|
|
|
});
|
|
|
|
helper.onUpdate(
|
|
|
|
data.length,
|
|
|
|
100,
|
|
|
|
200,
|
|
|
|
getFrameMetrics,
|
|
|
|
createViewToken,
|
|
|
|
onViewableItemsChanged,
|
|
|
|
);
|
|
|
|
expect(onViewableItemsChanged.mock.calls.length).toBe(2);
|
|
|
|
// Both visible with 100px overlap each
|
|
|
|
expect(onViewableItemsChanged.mock.calls[1][0]).toEqual({
|
|
|
|
changed: [{isViewable: true, key: 'b'}],
|
2017-09-06 01:33:44 +00:00
|
|
|
viewabilityConfig: {viewAreaCoveragePercentThreshold: 0},
|
2017-06-13 05:32:58 +00:00
|
|
|
viewableItems: [
|
|
|
|
{isViewable: true, key: 'a'},
|
|
|
|
{isViewable: true, key: 'b'},
|
|
|
|
],
|
|
|
|
});
|
|
|
|
helper.onUpdate(
|
|
|
|
data.length,
|
|
|
|
200,
|
|
|
|
200,
|
|
|
|
getFrameMetrics,
|
|
|
|
createViewToken,
|
|
|
|
onViewableItemsChanged,
|
|
|
|
);
|
|
|
|
expect(onViewableItemsChanged.mock.calls.length).toBe(3);
|
|
|
|
expect(onViewableItemsChanged.mock.calls[2][0]).toEqual({
|
|
|
|
changed: [{isViewable: false, key: 'a'}],
|
2017-09-06 01:33:44 +00:00
|
|
|
viewabilityConfig: {viewAreaCoveragePercentThreshold: 0},
|
2017-06-13 05:32:58 +00:00
|
|
|
viewableItems: [{isViewable: true, key: 'b'}],
|
|
|
|
});
|
|
|
|
});
|
2017-02-28 10:08:53 +00:00
|
|
|
|
2017-06-13 05:32:58 +00:00
|
|
|
it('minimumViewTime delays callback', function() {
|
|
|
|
const helper = new ViewabilityHelper({
|
|
|
|
minimumViewTime: 350,
|
|
|
|
viewAreaCoveragePercentThreshold: 0,
|
|
|
|
});
|
|
|
|
rowFrames = {
|
|
|
|
a: {y: 0, height: 200},
|
|
|
|
b: {y: 200, height: 200},
|
|
|
|
};
|
|
|
|
data = [{key: 'a'}, {key: 'b'}];
|
|
|
|
const onViewableItemsChanged = jest.fn();
|
|
|
|
helper.onUpdate(
|
|
|
|
data.length,
|
|
|
|
0,
|
|
|
|
200,
|
|
|
|
getFrameMetrics,
|
|
|
|
createViewToken,
|
|
|
|
onViewableItemsChanged,
|
|
|
|
);
|
|
|
|
expect(onViewableItemsChanged).not.toBeCalled();
|
2017-02-28 10:08:53 +00:00
|
|
|
|
2017-06-13 05:32:58 +00:00
|
|
|
jest.runAllTimers();
|
2017-02-28 10:08:53 +00:00
|
|
|
|
2017-06-13 05:32:58 +00:00
|
|
|
expect(onViewableItemsChanged.mock.calls.length).toBe(1);
|
|
|
|
expect(onViewableItemsChanged.mock.calls[0][0]).toEqual({
|
|
|
|
changed: [{isViewable: true, key: 'a'}],
|
2017-09-06 01:33:44 +00:00
|
|
|
viewabilityConfig: {
|
|
|
|
minimumViewTime: 350,
|
|
|
|
viewAreaCoveragePercentThreshold: 0,
|
|
|
|
},
|
2017-06-13 05:32:58 +00:00
|
|
|
viewableItems: [{isViewable: true, key: 'a'}],
|
|
|
|
});
|
|
|
|
});
|
2017-02-28 10:08:53 +00:00
|
|
|
|
2017-06-13 05:32:58 +00:00
|
|
|
it('minimumViewTime skips briefly visible items', function() {
|
|
|
|
const helper = new ViewabilityHelper({
|
|
|
|
minimumViewTime: 350,
|
|
|
|
viewAreaCoveragePercentThreshold: 0,
|
|
|
|
});
|
|
|
|
rowFrames = {
|
|
|
|
a: {y: 0, height: 250},
|
|
|
|
b: {y: 250, height: 200},
|
|
|
|
};
|
|
|
|
data = [{key: 'a'}, {key: 'b'}];
|
|
|
|
const onViewableItemsChanged = jest.fn();
|
|
|
|
helper.onUpdate(
|
|
|
|
data.length,
|
|
|
|
0,
|
|
|
|
200,
|
|
|
|
getFrameMetrics,
|
|
|
|
createViewToken,
|
|
|
|
onViewableItemsChanged,
|
|
|
|
);
|
|
|
|
helper.onUpdate(
|
|
|
|
data.length,
|
|
|
|
300, // scroll past item 'a'
|
|
|
|
200,
|
|
|
|
getFrameMetrics,
|
|
|
|
createViewToken,
|
|
|
|
onViewableItemsChanged,
|
|
|
|
);
|
2017-02-28 10:08:53 +00:00
|
|
|
|
2017-06-13 05:32:58 +00:00
|
|
|
jest.runAllTimers();
|
2017-02-28 10:08:53 +00:00
|
|
|
|
2017-06-13 05:32:58 +00:00
|
|
|
expect(onViewableItemsChanged.mock.calls.length).toBe(1);
|
|
|
|
expect(onViewableItemsChanged.mock.calls[0][0]).toEqual({
|
|
|
|
changed: [{isViewable: true, key: 'b'}],
|
2017-09-06 01:33:44 +00:00
|
|
|
viewabilityConfig: {
|
|
|
|
minimumViewTime: 350,
|
|
|
|
viewAreaCoveragePercentThreshold: 0,
|
|
|
|
},
|
2017-06-13 05:32:58 +00:00
|
|
|
viewableItems: [{isViewable: true, key: 'b'}],
|
|
|
|
});
|
|
|
|
});
|
2017-03-07 05:34:32 +00:00
|
|
|
|
2017-06-13 05:32:58 +00:00
|
|
|
it('waitForInteraction blocks callback until interaction', function() {
|
|
|
|
const helper = new ViewabilityHelper({
|
|
|
|
waitForInteraction: true,
|
|
|
|
viewAreaCoveragePercentThreshold: 0,
|
|
|
|
});
|
|
|
|
rowFrames = {
|
|
|
|
a: {y: 0, height: 200},
|
|
|
|
b: {y: 200, height: 200},
|
|
|
|
};
|
|
|
|
data = [{key: 'a'}, {key: 'b'}];
|
|
|
|
const onViewableItemsChanged = jest.fn();
|
|
|
|
helper.onUpdate(
|
|
|
|
data.length,
|
|
|
|
0,
|
|
|
|
100,
|
|
|
|
getFrameMetrics,
|
|
|
|
createViewToken,
|
|
|
|
onViewableItemsChanged,
|
|
|
|
);
|
|
|
|
expect(onViewableItemsChanged).not.toBeCalled();
|
2017-03-07 05:34:32 +00:00
|
|
|
|
2017-06-13 05:32:58 +00:00
|
|
|
helper.recordInteraction();
|
2017-02-28 10:08:53 +00:00
|
|
|
|
2017-06-13 05:32:58 +00:00
|
|
|
helper.onUpdate(
|
|
|
|
data.length,
|
|
|
|
20,
|
|
|
|
100,
|
|
|
|
getFrameMetrics,
|
|
|
|
createViewToken,
|
|
|
|
onViewableItemsChanged,
|
|
|
|
);
|
|
|
|
expect(onViewableItemsChanged.mock.calls.length).toBe(1);
|
|
|
|
expect(onViewableItemsChanged.mock.calls[0][0]).toEqual({
|
|
|
|
changed: [{isViewable: true, key: 'a'}],
|
2017-09-06 01:33:44 +00:00
|
|
|
viewabilityConfig: {
|
|
|
|
waitForInteraction: true,
|
|
|
|
viewAreaCoveragePercentThreshold: 0,
|
|
|
|
},
|
2017-06-13 05:32:58 +00:00
|
|
|
viewableItems: [{isViewable: true, key: 'a'}],
|
|
|
|
});
|
|
|
|
});
|
2017-10-16 11:18:17 +00:00
|
|
|
|
|
|
|
it('returns the right visible row after the underlying data changed', function() {
|
|
|
|
const helper = new ViewabilityHelper();
|
|
|
|
rowFrames = {
|
|
|
|
a: {y: 0, height: 200},
|
|
|
|
b: {y: 200, height: 200},
|
|
|
|
};
|
|
|
|
data = [{key: 'a'}, {key: 'b'}];
|
|
|
|
const onViewableItemsChanged = jest.fn();
|
|
|
|
helper.onUpdate(
|
|
|
|
data.length,
|
|
|
|
0,
|
|
|
|
200,
|
|
|
|
getFrameMetrics,
|
|
|
|
createViewToken,
|
|
|
|
onViewableItemsChanged,
|
|
|
|
);
|
|
|
|
expect(onViewableItemsChanged.mock.calls.length).toBe(1);
|
|
|
|
expect(onViewableItemsChanged.mock.calls[0][0]).toEqual({
|
|
|
|
changed: [{isViewable: true, key: 'a'}],
|
|
|
|
viewabilityConfig: {viewAreaCoveragePercentThreshold: 0},
|
|
|
|
viewableItems: [{isViewable: true, key: 'a'}],
|
|
|
|
});
|
|
|
|
|
|
|
|
// update data
|
|
|
|
rowFrames = {
|
|
|
|
c: {y: 0, height: 200},
|
|
|
|
a: {y: 200, height: 200},
|
|
|
|
b: {y: 400, height: 200},
|
|
|
|
};
|
|
|
|
data = [{key: 'c'}, {key: 'a'}, {key: 'b'}];
|
|
|
|
|
|
|
|
helper.resetViewableIndices();
|
|
|
|
|
|
|
|
helper.onUpdate(
|
|
|
|
data.length,
|
|
|
|
0,
|
|
|
|
200,
|
|
|
|
getFrameMetrics,
|
|
|
|
createViewToken,
|
|
|
|
onViewableItemsChanged,
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(onViewableItemsChanged.mock.calls.length).toBe(2);
|
|
|
|
expect(onViewableItemsChanged.mock.calls[1][0]).toEqual({
|
|
|
|
changed: [{isViewable: true, key: 'c'}, {isViewable: false, key: 'a'}],
|
|
|
|
viewabilityConfig: {viewAreaCoveragePercentThreshold: 0},
|
|
|
|
viewableItems: [{isViewable: true, key: 'c'}],
|
|
|
|
});
|
|
|
|
});
|
Better ListView - FlatList
Summary:
We really need a better list view - so here it is!
Main changes from existing `ListView`:
* Items are "virtualized" to limit memory - that is, items outside of the render window are unmounted and their memory is reclaimed. This means that instance state is not preserved when items scroll out of the render window.
* No `DataSource` - just a simple `data` prop of shape `Array<any>`. By default, they are expected to be of the shape `{key: string}` but a custom `rowExtractor` function can be provided for different shapes, e.g. graphql data where you want to map `id` to `key`. Note the underlying `VirtualizedList` is much more flexible.
* Fancy `scrollTo` functionality: `scrollToEnd`, `scrollToIndex`, and `scrollToItem` in addition to the normal `scrollToOffset`.
* Built-in pull to refresh support - set set the `onRefresh` and `refreshing` props.
* Rendering additional rows is usually done with low priority, after any interactions/animations complete, unless we're about to run out of rendered content. This should help apps feel more responsive.
* Component props replace render functions, e.g. `ItemComponent: ReactClass<{item: Item, index: number}>` replaces `renderRow: (...) => React.Element<*>`
* Supports dynamic items automatically by using `onLayout`, or `getItemLayout` can be provided for a perf boost and smoother `scrollToIndex` and scroll bar behavior.
* Visibility callback replaced with more powerful viewability callback and works in vertical and horizontal mode on at least Android and iOS, but probably other platforms as well. Extra power comes from the `viewablePercentThreshold` that lets the client decide when an item should be considered viewable.
Demo:
https://www.facebook.com/groups/576288835853049/permalink/753923058089625/
Reviewed By: yungsters
Differential Revision: D4412469
fbshipit-source-id: e2d891490bf76fe14df49294ecddf78a58adcf23
2017-02-04 18:25:32 +00:00
|
|
|
});
|