Skip to content

Commit 5fd1983

Browse files
Luna Weifacebook-github-bot
Luna Wei
authored andcommitted
Move onViewableItemsChanged under FLatListExampleIndex
Summary: Changelog: [Internal] Refactor FlatList examples, move onViewableItemsChanged under FlatList index Reviewed By: charlesbdudley Differential Revision: D30816718 fbshipit-source-id: 87c0500f0852fa6608a133cca008347f62b7010b
1 parent 4603e76 commit 5fd1983

File tree

5 files changed

+66
-75
lines changed

5 files changed

+66
-75
lines changed

packages/rn-tester/js/examples/FlatList/BaseFlatListExample.js

-46
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,6 @@ import {
1818
} from 'react-native';
1919

2020
import * as React from 'react';
21-
type FlatListProps = React.ElementProps<typeof FlatList>;
22-
23-
type ViewabilityConfig = $PropertyType<FlatListProps, 'viewabilityConfig'>;
2421

2522
const DATA = [
2623
'Pizza',
@@ -91,49 +88,6 @@ export function FlatList_withSeparators(): React.Node {
9188
return <BaseFlatListExample ref={ref} exampleProps={exampleProps} />;
9289
}
9390

94-
export function FlatList_onViewableItemsChanged(props: {
95-
viewabilityConfig: ViewabilityConfig,
96-
offScreen?: ?boolean,
97-
horizontal?: ?boolean,
98-
useScrollRefScroll?: ?boolean,
99-
}): React.Node {
100-
const {viewabilityConfig, offScreen, horizontal, useScrollRefScroll} = props;
101-
const [output, setOutput] = React.useState('');
102-
const onViewableItemsChanged = React.useCallback(
103-
info =>
104-
setOutput(
105-
info.viewableItems
106-
.filter(viewToken => viewToken.index != null && viewToken.isViewable)
107-
.map(viewToken => viewToken.item)
108-
.join(', '),
109-
),
110-
[setOutput],
111-
);
112-
const exampleProps = {
113-
onViewableItemsChanged,
114-
viewabilityConfig,
115-
horizontal,
116-
};
117-
118-
const ref = React.useRef(null);
119-
const onTest =
120-
useScrollRefScroll === true
121-
? () => {
122-
ref?.current?.getScrollResponder()?.scrollToEnd();
123-
}
124-
: null;
125-
126-
return (
127-
<BaseFlatListExample
128-
ref={ref}
129-
exampleProps={exampleProps}
130-
onTest={onTest}
131-
testOutput={output}>
132-
{offScreen === true ? <View style={styles.offScreen} /> : null}
133-
</BaseFlatListExample>
134-
);
135-
}
136-
13791
type Props = {
13892
exampleProps: $Shape<React.ElementConfig<typeof FlatList>>,
13993
onTest?: ?() => void,

packages/rn-tester/js/examples/FlatList/FlatList-onViewableItemsChanged.js

+64-19
Original file line numberDiff line numberDiff line change
@@ -9,30 +9,75 @@
99
*/
1010

1111
'use strict';
12-
import {FlatList_onViewableItemsChanged} from './BaseFlatListExample';
13-
const React = require('react');
12+
import type {RNTesterModuleExample} from '../../types/RNTesterTypes';
13+
import BaseFlatListExample from './BaseFlatListExample';
14+
import {StyleSheet, View, FlatList} from 'react-native';
15+
import * as React from 'react';
16+
17+
type FlatListProps = React.ElementProps<typeof FlatList>;
18+
type ViewabilityConfig = $PropertyType<FlatListProps, 'viewabilityConfig'>;
1419

1520
const VIEWABILITY_CONFIG = {
1621
minimumViewTime: 1000,
1722
viewAreaCoveragePercentThreshold: 100,
1823
waitForInteraction: true,
1924
};
2025

21-
exports.title = 'FlatList onViewableItemsChanged';
22-
exports.testTitle = 'Test onViewableItemsChanged callback';
23-
exports.category = 'ListView';
24-
exports.documentationURL = 'https://reactnative.dev/docs/sectionlist';
25-
exports.description =
26-
'Scroll list to see what items are returned in `onViewableItemsChanged` callback.';
27-
exports.examples = [
28-
{
29-
title: 'FlatList onViewableItemsChanged',
30-
render: function(): React.Element<typeof FlatList_onViewableItemsChanged> {
31-
return (
32-
<FlatList_onViewableItemsChanged
33-
viewabilityConfig={VIEWABILITY_CONFIG}
34-
/>
35-
);
36-
},
26+
export function FlatList_onViewableItemsChanged(props: {
27+
viewabilityConfig: ViewabilityConfig,
28+
offScreen?: ?boolean,
29+
horizontal?: ?boolean,
30+
useScrollRefScroll?: ?boolean,
31+
}): React.Node {
32+
const {viewabilityConfig, offScreen, horizontal, useScrollRefScroll} = props;
33+
const [output, setOutput] = React.useState('');
34+
const onViewableItemsChanged = React.useCallback(
35+
info =>
36+
setOutput(
37+
info.viewableItems
38+
.filter(viewToken => viewToken.index != null && viewToken.isViewable)
39+
.map(viewToken => viewToken.item)
40+
.join(', '),
41+
),
42+
[setOutput],
43+
);
44+
const exampleProps = {
45+
onViewableItemsChanged,
46+
viewabilityConfig,
47+
horizontal,
48+
};
49+
50+
const ref = React.useRef(null);
51+
const onTest =
52+
useScrollRefScroll === true
53+
? () => {
54+
ref?.current?.getScrollResponder()?.scrollToEnd();
55+
}
56+
: null;
57+
58+
return (
59+
<BaseFlatListExample
60+
ref={ref}
61+
exampleProps={exampleProps}
62+
onTest={onTest}
63+
testOutput={output}>
64+
{offScreen === true ? <View style={styles.offScreen} /> : null}
65+
</BaseFlatListExample>
66+
);
67+
}
68+
69+
const styles = StyleSheet.create({
70+
offScreen: {
71+
height: 1000,
3772
},
38-
];
73+
});
74+
75+
export default ({
76+
title: 'onViewableItemsChanged',
77+
name: 'onViewableItemsChanged',
78+
description:
79+
'Scroll list to see what items are returned in `onViewableItemsChanged` callback.',
80+
render: () => (
81+
<FlatList_onViewableItemsChanged viewabilityConfig={VIEWABILITY_CONFIG} />
82+
),
83+
}: RNTesterModuleExample);

packages/rn-tester/js/examples/FlatList/FlatListExampleIndex.js

+2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import BasicExample from './FlatList-basic';
1313
import OnEndReachedExample from './FlatList-onEndReached';
1414
import ContentInsetExample from './FlatList-contentInset';
1515
import InvertedExample from './FlatList-inverted';
16+
import onViewableItemsChangedExample from './FlatList-onViewableItemsChanged';
1617

1718
export default ({
1819
framework: 'React',
@@ -26,5 +27,6 @@ export default ({
2627
OnEndReachedExample,
2728
ContentInsetExample,
2829
InvertedExample,
30+
onViewableItemsChangedExample,
2931
],
3032
}: RNTesterModule);

packages/rn-tester/js/utils/RNTesterList.android.js

-5
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,6 @@ const Components: Array<RNTesterModuleInfo> = [
3434
module: require('../examples/FlatList/FlatList-withSeparators'),
3535
category: 'ListView',
3636
},
37-
{
38-
key: 'FlatList-onViewableItemsChanged',
39-
module: require('../examples/FlatList/FlatList-onViewableItemsChanged'),
40-
category: 'ListView',
41-
},
4237
{
4338
key: 'ImageExample',
4439
category: 'Basic',

packages/rn-tester/js/utils/RNTesterList.ios.js

-5
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,6 @@ const Components: Array<RNTesterModuleInfo> = [
3636
module: require('../examples/FlatList/FlatList-withSeparators'),
3737
category: 'ListView',
3838
},
39-
{
40-
key: 'FlatList-onViewableItemsChanged',
41-
module: require('../examples/FlatList/FlatList-onViewableItemsChanged'),
42-
category: 'ListView',
43-
},
4439
{
4540
key: 'ImageExample',
4641
module: require('../examples/Image/ImageExample'),

0 commit comments

Comments
 (0)