Skip to content

Commit b127662

Browse files
elicwhitefacebook-github-bot
authored andcommitted
Flow Type ScrollView
Reviewed By: yungsters Differential Revision: D7981073 fbshipit-source-id: 38c100f37e46683da1e34b335d476e706baae238
1 parent 188b118 commit b127662

File tree

4 files changed

+165
-12
lines changed

4 files changed

+165
-12
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* Copyright (c) 2015-present, Facebook, Inc.
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+
* @format
8+
* @flow
9+
*/
10+
11+
const React = require('React');
12+
13+
// This class is purely a facsimile of ScrollView so that we can
14+
// properly type it with Flow before migrating ScrollView off of
15+
// createReactClass. If there are things missing here that are in
16+
// ScrollView, that is unintentional.
17+
class InternalScrollViewType<Props> extends React.Component<Props> {
18+
scrollTo(
19+
y?: number | {x?: number, y?: number, animated?: boolean},
20+
x?: number,
21+
animated?: boolean,
22+
) {}
23+
24+
flashScrollIndicators() {}
25+
scrollToEnd(options?: {animated?: boolean}) {}
26+
scrollWithoutAnimationTo(y: number = 0, x: number = 0) {}
27+
setNativeProps(props: Object) {}
28+
29+
getScrollResponder(): any {}
30+
getScrollableNode(): any {}
31+
getInnerViewNode(): any {}
32+
33+
scrollResponderScrollNativeHandleToKeyboard(
34+
nodeHandle: any,
35+
additionalOffset?: number,
36+
preventNegativeScrollOffset?: boolean,
37+
) {}
38+
39+
scrollResponderScrollTo(
40+
x?: number | {x?: number, y?: number, animated?: boolean},
41+
y?: number,
42+
animated?: boolean,
43+
) {}
44+
}
45+
46+
module.exports = InternalScrollViewType;

Libraries/Components/ScrollView/ScrollView.js

Lines changed: 108 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ const StyleSheetPropType = require('StyleSheetPropType');
2525
const View = require('View');
2626
const ViewPropTypes = require('ViewPropTypes');
2727
const ViewStylePropTypes = require('ViewStylePropTypes');
28+
const InternalScrollViewType = require('InternalScrollViewType');
2829

2930
const createReactClass = require('create-react-class');
3031
const dismissKeyboard = require('dismissKeyboard');
@@ -38,7 +39,104 @@ const requireNativeComponent = require('requireNativeComponent');
3839
const warning = require('fbjs/lib/warning');
3940
const resolveAssetSource = require('resolveAssetSource');
4041

42+
import type {PressEvent} from 'CoreEventTypes';
43+
import type {EdgeInsetsProp} from 'EdgeInsetsPropType';
4144
import type {NativeMethodsMixinType} from 'ReactNativeTypes';
45+
import type {ViewStyleProp} from 'StyleSheet';
46+
import type {ViewProps} from 'ViewPropTypes';
47+
import type {PointProp} from 'PointPropType';
48+
49+
import type {ColorValue} from 'StyleSheetTypes';
50+
51+
type TouchableProps = $ReadOnly<{|
52+
onTouchStart?: (event: PressEvent) => void,
53+
onTouchMove?: (event: PressEvent) => void,
54+
onTouchEnd?: (event: PressEvent) => void,
55+
onTouchCancel?: (event: PressEvent) => void,
56+
onTouchEndCapture?: (event: PressEvent) => void,
57+
|}>;
58+
59+
type IOSProps = $ReadOnly<{|
60+
automaticallyAdjustContentInsets?: ?boolean,
61+
contentInset?: ?EdgeInsetsProp,
62+
contentOffset?: ?PointProp,
63+
bounces?: ?boolean,
64+
bouncesZoom?: ?boolean,
65+
alwaysBounceHorizontal?: ?boolean,
66+
alwaysBounceVertical?: ?boolean,
67+
centerContent?: ?boolean,
68+
decelerationRate?: ?('fast' | 'normal' | number),
69+
indicatorStyle?: ?('default' | 'black' | 'white'),
70+
directionalLockEnabled?: ?boolean,
71+
canCancelContentTouches?: ?boolean,
72+
maintainVisibleContentPosition?: ?$ReadOnly<{|
73+
minIndexForVisible: number,
74+
autoscrollToTopThreshold?: ?number,
75+
|}>,
76+
maximumZoomScale?: ?number,
77+
minimumZoomScale?: ?number,
78+
pinchGestureEnabled?: ?boolean,
79+
scrollEventThrottle?: ?number,
80+
scrollIndicatorInsets?: ?EdgeInsetsProp,
81+
scrollsToTop?: ?boolean,
82+
showsHorizontalScrollIndicator?: ?boolean,
83+
snapToAlignment?: ?('start' | 'center' | 'end'),
84+
zoomScale?: ?number,
85+
contentInsetAdjustmentBehavior?: ?(
86+
| 'automatic'
87+
| 'scrollableAxes'
88+
| 'never'
89+
| 'always'
90+
),
91+
DEPRECATED_sendUpdatedChildFrames?: ?boolean,
92+
|}>;
93+
94+
type AndroidProps = $ReadOnly<{|
95+
nestedScrollEnabled?: ?boolean,
96+
endFillColor?: ?ColorValue,
97+
scrollPerfTag?: ?string,
98+
overScrollMode?: ?('auto' | 'always' | 'never'),
99+
|}>;
100+
101+
type VRProps = $ReadOnly<{|
102+
scrollBarThumbImage?: ?($ReadOnly<{||}> | number),
103+
|}>;
104+
105+
type Props = $ReadOnly<{|
106+
...ViewProps,
107+
...TouchableProps,
108+
...IOSProps,
109+
...AndroidProps,
110+
...VRProps,
111+
112+
contentContainerStyle?: ?ViewStyleProp,
113+
horizontal?: ?boolean,
114+
invertStickyHeaders?: ?boolean,
115+
keyboardDismissMode?: ?(
116+
| 'none' // default
117+
| 'on-drag' // cross-platform
118+
| 'interactive'
119+
), // ios only
120+
// $FlowFixMe Issues found when typing ScrollView
121+
keyboardShouldPersistTaps?: ?('always' | 'never' | 'handled' | false | true),
122+
onMomentumScrollBegin?: ?Function,
123+
onMomentumScrollEnd?: ?Function,
124+
125+
onScroll?: ?Function,
126+
onScrollBeginDrag?: ?Function,
127+
onScrollEndDrag?: ?Function,
128+
onContentSizeChange?: ?Function,
129+
onKeyboardDidShow?: (event: PressEvent) => void,
130+
pagingEnabled?: ?boolean,
131+
scrollEnabled?: ?boolean,
132+
showsVerticalScrollIndicator?: ?boolean,
133+
stickyHeaderIndices?: ?$ReadOnlyArray<number>,
134+
snapToInterval?: ?number,
135+
removeClippedSubviews?: ?boolean,
136+
refreshControl?: ?React.Element<any>,
137+
style?: ?ViewStyleProp,
138+
children?: React.Node,
139+
|}>;
42140

43141
/**
44142
* Component that wraps platform ScrollView while providing
@@ -75,7 +173,6 @@ import type {NativeMethodsMixinType} from 'ReactNativeTypes';
75173
* multiple columns, infinite scroll loading, or any number of other features it
76174
* supports out of the box.
77175
*/
78-
// $FlowFixMe(>=0.41.0)
79176
const ScrollView = createReactClass({
80177
displayName: 'ScrollView',
81178
propTypes: {
@@ -806,8 +903,8 @@ const ScrollView = createReactClass({
806903
// $FlowFixMe Invalid prop usage
807904
hasStickyHeaders && React.Children.toArray(this.props.children);
808905
const children = hasStickyHeaders
809-
// $FlowFixMe Invalid prop usage
810-
? childArray.map((child, index) => {
906+
? // $FlowFixMe Invalid prop usage
907+
childArray.map((child, index) => {
811908
// $FlowFixMe Invalid prop usage
812909
const indexOfIndex = child ? stickyHeaderIndices.indexOf(index) : -1;
813910
if (indexOfIndex > -1) {
@@ -834,8 +931,8 @@ const ScrollView = createReactClass({
834931
return child;
835932
}
836933
})
837-
// $FlowFixMe Invalid prop usage
838-
: this.props.children;
934+
: // $FlowFixMe Invalid prop usage
935+
this.props.children;
839936
const contentContainer = (
840937
<ScrollContentContainerViewClass
841938
{...contentSizeChangeProps}
@@ -884,6 +981,7 @@ const ScrollView = createReactClass({
884981
onResponderGrant: this.scrollResponderHandleResponderGrant,
885982
onResponderReject: this.scrollResponderHandleResponderReject,
886983
onResponderRelease: this.scrollResponderHandleResponderRelease,
984+
// $FlowFixMe
887985
onResponderTerminate: this.scrollResponderHandleTerminate,
888986
onResponderTerminationRequest: this
889987
.scrollResponderHandleTerminationRequest,
@@ -958,6 +1056,10 @@ const ScrollView = createReactClass({
9581056
},
9591057
});
9601058

1059+
const TypedScrollView = ((ScrollView: any): Class<
1060+
InternalScrollViewType<Props>,
1061+
>);
1062+
9611063
const styles = StyleSheet.create({
9621064
baseVertical: {
9631065
flexGrow: 1,
@@ -1028,4 +1130,4 @@ if (Platform.OS === 'android') {
10281130
RCTScrollContentView = requireNativeComponent('RCTScrollContentView', View);
10291131
}
10301132

1031-
module.exports = ScrollView;
1133+
module.exports = TypedScrollView;

Libraries/StyleSheet/EdgeInsetsPropType.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ const EdgeInsetsPropType = PropTypes.shape({
1919
right: PropTypes.number,
2020
});
2121

22-
export type EdgeInsetsProp = {|
23-
+top: number,
24-
+left: number,
25-
+bottom: number,
26-
+right: number,
27-
|};
22+
export type EdgeInsetsProp = $ReadOnly<{|
23+
top?: ?number,
24+
left?: ?number,
25+
bottom?: ?number,
26+
right?: ?number,
27+
|}>;
2828

2929
module.exports = EdgeInsetsPropType;

Libraries/StyleSheet/PointPropType.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,9 @@ const PointPropType = PropTypes.shape({
1717
y: PropTypes.number,
1818
});
1919

20+
export type PointProp = $ReadOnly<{
21+
x: number,
22+
y: number,
23+
}>;
24+
2025
module.exports = PointPropType;

0 commit comments

Comments
 (0)