Files
react-native/ReactAndroid/src/androidTest/js/ScrollViewTestModule.js
T
Mike Vitousek 8f5f3d159b Add Flow annotations to escaped generics in xplat
Summary:
Flow will soon stop allowing generic types to "escape" out of the scope in which they were defined. The fix will be to add annotations to currently-unannotated variables, parameters, and function returns, so that generics don't become inputs to type inference for those positions. This diff adds new type annotations to xplat where possible to minimize the impact of this change.

This diff was generated by running
```
buck run //flow/src/facebook/komodo/binaries:annotate_escaped_generics -- --write ../../xplat/js
```
from within the flow directory, and then reverting changes that led to new errors. Most changes were reverted by running:
```
facebook/flowd check --json --json-version=2 ../../xplat/js &> post-json
jq -f j.jq < post-json | xargs hg revert
```
where `j.jq` is
```
def locs: [.primaryLoc.source, (select(.rootLoc.source != null) | .rootLoc.source), .referenceLocs[].source ] | unique;
[.errors[] | locs[]] | unique | .[]
```

Changelog: [Internal]

Reviewed By: panagosg7

Differential Revision: D24006427

fbshipit-source-id: 0cd6ec8a9611d8b1e9b14c54f9fffd2d7de2fd9e
2020-10-01 06:19:39 -07:00

184 lines
4.0 KiB
JavaScript

/**
* Copyright (c) Facebook, Inc. and its 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';
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;
import type {PressEvent} from 'react-native/Libraries/Types/CoreEventTypes';
// 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) {
ScrollListener.onScroll(
e.nativeEvent.contentOffset.x,
e.nativeEvent.contentOffset.y,
);
};
const onScrollBeginDrag = function(e) {
ScrollListener.onScrollBeginDrag(
e.nativeEvent.contentOffset.x,
e.nativeEvent.contentOffset.y,
);
};
const onScrollEndDrag = function(e) {
ScrollListener.onScrollEndDrag(
e.nativeEvent.contentOffset.x,
e.nativeEvent.contentOffset.y,
);
};
const onItemPress = function(itemNumber) {
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;
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;
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;