Skip to content

Commit ff36e02

Browse files
authored
Add momentum scroll event counters to RNTester examples (#59940)
Summary: Make the momentum scroll RNTester examples testable end to end. `<ScrollView>` OnMomentumScroll now shows the last event, begin and end counts, and an error count that increments when `onMomentumScrollBegin` arrives while momentum is already active or `onMomentumScrollEnd` arrives without a matching begin. It adds buttons for animated `scrollToEnd`, animated and non-animated `scrollTo`, and resetting the counts, and is addressable as `onMomentumScroll`. A new `<FlatList>` OnMomentumScroll example counts begin and end events for animated and non-animated `scrollToOffset`. Changelog: [Internal] Differential Revision: D131232441 Pull Request resolved: #59940
1 parent cf90a81 commit ff36e02

3 files changed

Lines changed: 181 additions & 4 deletions

File tree

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @flow strict-local
8+
* @format
9+
*/
10+
11+
'use strict';
12+
13+
import type {RNTesterModuleExample} from '../../types/RNTesterTypes';
14+
15+
import * as React from 'react';
16+
import {useCallback, useRef, useState} from 'react';
17+
import {Button, FlatList, StyleSheet, Text, View} from 'react-native';
18+
19+
const DATA = [...Array(30)].map((_, i) => `Item ${i}`);
20+
21+
component FlatList_onMomentumScroll() {
22+
const ref = useRef<?FlatList<string>>(null);
23+
const [beginCount, setBeginCount] = useState(0);
24+
const [endCount, setEndCount] = useState(0);
25+
26+
const renderItem = useCallback(
27+
({item}: {item: string, ...}) => (
28+
<View style={styles.item}>
29+
<Text>{item}</Text>
30+
</View>
31+
),
32+
[],
33+
);
34+
35+
const resetCounts = useCallback(() => {
36+
setBeginCount(0);
37+
setEndCount(0);
38+
}, []);
39+
40+
return (
41+
<View style={styles.root}>
42+
<Text testID="flatlist_momentum_begin_count">begin:{beginCount}</Text>
43+
<Text testID="flatlist_momentum_end_count">end:{endCount}</Text>
44+
<FlatList
45+
ref={ref}
46+
data={DATA}
47+
keyExtractor={(_item, index) => index.toString()}
48+
renderItem={renderItem}
49+
getItemLayout={(_data: unknown, index: number) => ({
50+
length: 40,
51+
offset: 40 * index,
52+
index,
53+
})}
54+
style={styles.list}
55+
testID="flatlist_momentum_list"
56+
onMomentumScrollBegin={() => setBeginCount(count => count + 1)}
57+
onMomentumScrollEnd={() => setEndCount(count => count + 1)}
58+
/>
59+
<Button
60+
title="scrollToOffset 600 (animated)"
61+
testID="flatlist_momentum_scroll_to_offset_600_animated"
62+
onPress={() =>
63+
ref.current?.scrollToOffset({offset: 600, animated: true})
64+
}
65+
/>
66+
<Button
67+
title="scrollToOffset 0 (animated)"
68+
testID="flatlist_momentum_scroll_to_offset_0_animated"
69+
onPress={() => ref.current?.scrollToOffset({offset: 0, animated: true})}
70+
/>
71+
<Button
72+
title="scrollToOffset 0 (not animated)"
73+
testID="flatlist_momentum_scroll_to_offset_0_not_animated"
74+
onPress={() =>
75+
ref.current?.scrollToOffset({offset: 0, animated: false})
76+
}
77+
/>
78+
<Button
79+
title="Reset counts"
80+
testID="flatlist_momentum_reset"
81+
onPress={resetCounts}
82+
/>
83+
</View>
84+
);
85+
}
86+
87+
const styles = StyleSheet.create({
88+
root: {
89+
flex: 1,
90+
padding: 8,
91+
},
92+
list: {
93+
height: 200,
94+
},
95+
item: {
96+
height: 40,
97+
justifyContent: 'center',
98+
paddingHorizontal: 8,
99+
},
100+
});
101+
102+
export default {
103+
title: 'OnMomentumScroll',
104+
name: 'onMomentumScroll',
105+
description:
106+
'Counts `onMomentumScrollBegin`/`onMomentumScrollEnd` for animated and non-animated `scrollToOffset` commands.',
107+
render: function () {
108+
return <FlatList_onMomentumScroll />;
109+
},
110+
} as RNTesterModuleExample;

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import MaintainVisibleContentPosition from './FlatList-maintainVisibleContentPos
1717
import MultiColumnExample from './FlatList-multiColumn';
1818
import NestedExample from './FlatList-nested';
1919
import OnEndReachedExample from './FlatList-onEndReached';
20+
import OnMomentumScrollExample from './FlatList-onMomentumScroll';
2021
import OnStartReachedExample from './FlatList-onStartReached';
2122
import onViewableItemsChangedExample from './FlatList-onViewableItemsChanged';
2223
import onViewableItemsChanged_horizontal_noWaitForInteraction from './FlatList-onViewableItemsChanged-horizontal-noWaitForInteraction';
@@ -40,6 +41,7 @@ export default {
4041
MaintainVisibleContentPosition,
4142
OnStartReachedExample,
4243
OnEndReachedExample,
44+
OnMomentumScrollExample,
4345
ContentInsetExample,
4446
InvertedExample,
4547
onViewableItemsChangedExample,

‎packages/rn-tester/js/examples/ScrollView/ScrollViewExample.js‎

Lines changed: 69 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,7 @@ const examples: Array<RNTesterModuleExample> = [
431431
},
432432
},
433433
{
434+
name: 'onMomentumScroll',
434435
title: '<ScrollView> OnMomentumScroll\n',
435436
description:
436437
'An alert will be called when the momentum scroll starts or ends.',
@@ -1128,17 +1129,81 @@ const OnScrollOptions = () => {
11281129
};
11291130

11301131
const OnMomentumScroll = () => {
1131-
const [scroll, setScroll] = useState('none');
1132+
const ref = useRef<?React.ElementRef<typeof ScrollView>>(null);
1133+
const [beginCount, setBeginCount] = useState(0);
1134+
const [endCount, setEndCount] = useState(0);
1135+
const [lastEvent, setLastEvent] = useState('none');
1136+
// Counts a begin while momentum is already active, or an end without a
1137+
// matching begin.
1138+
const [errorCount, setErrorCount] = useState(0);
1139+
const isMomentumActiveRef = useRef(false);
1140+
1141+
const handleMomentumScrollBegin = useCallback(() => {
1142+
if (isMomentumActiveRef.current) {
1143+
setErrorCount(count => count + 1);
1144+
}
1145+
isMomentumActiveRef.current = true;
1146+
setBeginCount(count => count + 1);
1147+
setLastEvent('onMomentumScrollBegin');
1148+
}, []);
1149+
1150+
const handleMomentumScrollEnd = useCallback(() => {
1151+
if (!isMomentumActiveRef.current) {
1152+
setErrorCount(count => count + 1);
1153+
}
1154+
isMomentumActiveRef.current = false;
1155+
setEndCount(count => count + 1);
1156+
setLastEvent('onMomentumScrollEnd');
1157+
}, []);
1158+
1159+
// Leaves isMomentumActiveRef alone: it mirrors the native momentum state, so an
1160+
// end event still pending after a reset pairs with its begin.
1161+
const resetCounts = useCallback(() => {
1162+
setBeginCount(0);
1163+
setEndCount(0);
1164+
setErrorCount(0);
1165+
setLastEvent('none');
1166+
}, []);
1167+
11321168
return (
11331169
<View>
1134-
<RNTesterText>Scroll State: {scroll}</RNTesterText>
1170+
<RNTesterText testID="momentum_last_event">
1171+
Last event: {lastEvent}
1172+
</RNTesterText>
1173+
<RNTesterText testID="momentum_begin_count">
1174+
begin:{beginCount}
1175+
</RNTesterText>
1176+
<RNTesterText testID="momentum_end_count">end:{endCount}</RNTesterText>
1177+
<RNTesterText testID="momentum_errors">errors:{errorCount}</RNTesterText>
11351178
<ScrollView
1179+
ref={ref}
11361180
style={[styles.scrollView, {height: 200}]}
1137-
onMomentumScrollBegin={() => setScroll('onMomentumScrollBegin')}
1138-
onMomentumScrollEnd={() => setScroll('onMomentumScrollEnd')}
1181+
testID="momentum_scroll_view"
1182+
onMomentumScrollBegin={handleMomentumScrollBegin}
1183+
onMomentumScrollEnd={handleMomentumScrollEnd}
11391184
nestedScrollEnabled>
11401185
{ITEMS.map(createItemRow)}
11411186
</ScrollView>
1187+
<Button
1188+
label="scrollTo bottom (animated)"
1189+
testID="momentum_scroll_to_bottom_animated"
1190+
onPress={() => ref.current?.scrollToEnd({animated: true})}
1191+
/>
1192+
<Button
1193+
label="scrollTo top (animated)"
1194+
testID="momentum_scroll_to_top_animated"
1195+
onPress={() => ref.current?.scrollTo({x: 0, y: 0, animated: true})}
1196+
/>
1197+
<Button
1198+
label="scrollTo top (not animated)"
1199+
testID="momentum_scroll_to_top_not_animated"
1200+
onPress={() => ref.current?.scrollTo({x: 0, y: 0, animated: false})}
1201+
/>
1202+
<Button
1203+
label="Reset counts"
1204+
testID="momentum_reset"
1205+
onPress={resetCounts}
1206+
/>
11421207
</View>
11431208
);
11441209
};

0 commit comments

Comments
 (0)