Files
react-native/ReactAndroid/src/androidTest/js/ScrollViewTestModule.js
T
Pieter VanderwerffandFacebook GitHub Bot e7a4dbcefc Add LTI annotations to function params in xplat/js [1/2]
Summary: Add annotations to function parameters required for Flow's Local Type Inference project. This codemod prepares the codebase to match Flow's new typechecking algorithm. The new algorithm will make Flow more reliable and predicatable.

Reviewed By: evanyeung

Differential Revision: D37353648

fbshipit-source-id: e5a0c685ced85a8ff353d578b373f836b376bb28
2022-06-22 21:36:52 -07:00

187 lines
4.1 KiB
JavaScript

/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
* @flow
*/
'use strict';
import type {
PressEvent,
ScrollEvent,
} from 'react-native/Libraries/Types/CoreEventTypes';
const BatchedBridge = require('react-native/Libraries/BatchedBridge/BatchedBridge');
const React = require('react');
const {
NativeModules,
ScrollView,
StyleSheet,
Text,
TouchableWithoutFeedback,
View,
} = require('react-native');
const {ScrollListener} = NativeModules;
const NUM_ITEMS = 100;
// Shared by integration tests for ScrollView and HorizontalScrollView
let scrollViewApp: ScrollViewTestApp | HorizontalScrollViewTestApp;
type ItemProps = $ReadOnly<{|
onPress: (event: PressEvent) => void,
text: string,
|}>;
type ItemState = {||};
class Item extends React.Component<ItemProps, ItemState> {
render() {
return (
<TouchableWithoutFeedback onPress={this.props.onPress}>
<View style={styles.item_container}>
<Text style={styles.item_text}>{this.props.text}</Text>
</View>
</TouchableWithoutFeedback>
);
}
}
const getInitialState = function () {
const data = [];
for (let i = 0; i < NUM_ITEMS; i++) {
data[i] = {text: 'Item ' + i + '!'};
}
return {
data: data,
};
};
const onScroll = function (e: ScrollEvent) {
ScrollListener.onScroll(
e.nativeEvent.contentOffset.x,
e.nativeEvent.contentOffset.y,
);
};
const onScrollBeginDrag = function (e: ScrollEvent) {
ScrollListener.onScrollBeginDrag(
e.nativeEvent.contentOffset.x,
e.nativeEvent.contentOffset.y,
);
};
const onScrollEndDrag = function (e: ScrollEvent) {
ScrollListener.onScrollEndDrag(
e.nativeEvent.contentOffset.x,
e.nativeEvent.contentOffset.y,
);
};
const onItemPress = function (itemNumber: number) {
ScrollListener.onItemPress(itemNumber);
};
type Props = $ReadOnly<{||}>;
type State = {|
data: $ReadOnlyArray<{|text: string|}>,
|};
class ScrollViewTestApp extends React.Component<Props, State> {
scrollView: {|current: any | null|} = React.createRef();
state: State = getInitialState();
scrollTo(destX: number, destY: number) {
const scrollView = this.scrollView.current;
if (scrollView == null) {
return;
}
scrollView.scrollTo(destY, destX);
}
render(): React.Node {
scrollViewApp = this; // eslint-disable-line consistent-this
const children = this.state.data.map((item, index) => (
<Item
key={index}
text={item.text}
onPress={onItemPress.bind(this, index)}
/>
));
return (
<ScrollView
onScroll={onScroll}
onScrollBeginDrag={onScrollBeginDrag}
onScrollEndDrag={onScrollEndDrag}
ref={this.scrollView}>
{children}
</ScrollView>
);
}
}
class HorizontalScrollViewTestApp extends React.Component<Props, State> {
scrollView: {|current: any | null|} = React.createRef();
state: State = getInitialState();
scrollTo(destX: number, destY: number) {
const scrollView = this.scrollView.current;
if (scrollView == null) {
return;
}
scrollView.scrollTo(destY, destX);
}
render(): React.Node {
scrollViewApp = this; // eslint-disable-line consistent-this
const children = this.state.data.map((item, index) => (
<Item
key={index}
text={item.text}
onPress={onItemPress.bind(this, index)}
/>
));
return (
<ScrollView horizontal={true} onScroll={onScroll} ref={this.scrollView}>
{children}
</ScrollView>
);
}
}
const styles = StyleSheet.create({
item_container: {
padding: 30,
backgroundColor: '#ffffff',
},
item_text: {
flex: 1,
fontSize: 18,
alignSelf: 'center',
},
});
const ScrollViewTestModule = {
ScrollViewTestApp: ScrollViewTestApp,
HorizontalScrollViewTestApp: HorizontalScrollViewTestApp,
scrollTo(destX: number, destY: number) {
scrollViewApp.scrollTo(destX, destY);
},
};
BatchedBridge.registerCallableModule(
'ScrollViewTestModule',
ScrollViewTestModule,
);
module.exports = ScrollViewTestModule;