|
9 | 9 | */
|
10 | 10 |
|
11 | 11 | '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'>; |
14 | 19 |
|
15 | 20 | const VIEWABILITY_CONFIG = {
|
16 | 21 | minimumViewTime: 1000,
|
17 | 22 | viewAreaCoveragePercentThreshold: 100,
|
18 | 23 | waitForInteraction: true,
|
19 | 24 | };
|
20 | 25 |
|
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, |
37 | 72 | },
|
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); |
0 commit comments