Skip to content

Commit

Permalink
RN: Fix ScrollView Type Errors
Browse files Browse the repository at this point in the history
Summary:
Fixes some of the type errors in `ScrollView` that were previously being suppressed by comments. This also slightly simplifies the implementation of `ScrollView`.

Changelog:
[Internal]

Reviewed By: kacieb

Differential Revision: D25097225

fbshipit-source-id: a444db8179c91e264671d8f32431b93c4da8dfc4
  • Loading branch information
yungsters authored and facebook-github-bot committed Nov 19, 2020
1 parent 54a067e commit d4e29ec
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 67 deletions.
95 changes: 37 additions & 58 deletions Libraries/Components/ScrollView/ScrollView.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,20 +48,25 @@ import ScrollContentViewNativeComponent from './ScrollContentViewNativeComponent
import AndroidHorizontalScrollViewNativeComponent from './AndroidHorizontalScrollViewNativeComponent';
import AndroidHorizontalScrollContentViewNativeComponent from './AndroidHorizontalScrollContentViewNativeComponent';

let AndroidScrollView;
let AndroidHorizontalScrollContentView;
let AndroidHorizontalScrollView;
let RCTScrollView;
let RCTScrollContentView;

if (Platform.OS === 'android') {
AndroidScrollView = ScrollViewNativeComponent;
AndroidHorizontalScrollView = AndroidHorizontalScrollViewNativeComponent;
AndroidHorizontalScrollContentView = AndroidHorizontalScrollContentViewNativeComponent;
} else {
RCTScrollView = ScrollViewNativeComponent;
RCTScrollContentView = ScrollContentViewNativeComponent;
}
const {NativeHorizontalScrollViewTuple, NativeVerticalScrollViewTuple} =
Platform.OS === 'android'
? {
NativeHorizontalScrollViewTuple: [
AndroidHorizontalScrollViewNativeComponent,
AndroidHorizontalScrollContentViewNativeComponent,
],
NativeVerticalScrollViewTuple: [ScrollViewNativeComponent, View],
}
: {
NativeHorizontalScrollViewTuple: [
ScrollViewNativeComponent,
ScrollContentViewNativeComponent,
],
NativeVerticalScrollViewTuple: [
ScrollViewNativeComponent,
ScrollContentViewNativeComponent,
],
};

// Public methods for ScrollView
export type ScrollViewImperativeMethods = $ReadOnly<{|
Expand Down Expand Up @@ -1008,30 +1013,10 @@ class ScrollView extends React.Component<Props, State> {
});

render(): React.Node | React.Element<string> {
let ScrollViewClass;
let ScrollContentContainerViewClass;
if (Platform.OS === 'android') {
if (this.props.horizontal === true) {
ScrollViewClass = AndroidHorizontalScrollView;
ScrollContentContainerViewClass = AndroidHorizontalScrollContentView;
} else {
ScrollViewClass = AndroidScrollView;
ScrollContentContainerViewClass = View;
}
} else {
ScrollViewClass = RCTScrollView;
ScrollContentContainerViewClass = RCTScrollContentView;
}

invariant(
ScrollViewClass !== undefined,
'ScrollViewClass must not be undefined',
);

invariant(
ScrollContentContainerViewClass !== undefined,
'ScrollContentContainerViewClass must not be undefined',
);
const [NativeDirectionalScrollView, NativeDirectionalScrollContentView] =
this.props.horizontal === true
? NativeHorizontalScrollViewTuple
: NativeVerticalScrollViewTuple;

const contentContainerStyle = [
this.props.horizontal === true && styles.contentContainerHorizontal,
Expand All @@ -1050,12 +1035,12 @@ class ScrollView extends React.Component<Props, State> {
);
}

let contentSizeChangeProps = {};
if (this.props.onContentSizeChange) {
contentSizeChangeProps = {
onLayout: this._handleContentOnLayout,
};
}
const contentSizeChangeProps =
this.props.onContentSizeChange == null
? null
: {
onLayout: this._handleContentOnLayout,
};

const {stickyHeaderIndices} = this.props;
let children = this.props.children;
Expand Down Expand Up @@ -1101,10 +1086,7 @@ class ScrollView extends React.Component<Props, State> {
Array.isArray(stickyHeaderIndices) && stickyHeaderIndices.length > 0;

const contentContainer = (
/* $FlowFixMe(>=0.112.0 site=react_native_fb) This comment suppresses an
* error found when Flow v0.112 was deployed. To see the error, delete
* this comment and run Flow. */
<ScrollContentContainerViewClass
<NativeDirectionalScrollContentView
{...contentSizeChangeProps}
ref={this._setInnerViewRef}
style={contentContainerStyle}
Expand All @@ -1117,7 +1099,7 @@ class ScrollView extends React.Component<Props, State> {
}
collapsable={false}>
{children}
</ScrollContentContainerViewClass>
</NativeDirectionalScrollContentView>
);

const alwaysBounceHorizontal =
Expand Down Expand Up @@ -1207,13 +1189,10 @@ class ScrollView extends React.Component<Props, State> {
if (Platform.OS === 'ios') {
// On iOS the RefreshControl is a child of the ScrollView.
return (
/* $FlowFixMe(>=0.117.0 site=react_native_fb) This comment suppresses
* an error found when Flow v0.117 was deployed. To see the error,
* delete this comment and run Flow. */
<ScrollViewClass {...props} ref={this._setNativeRef}>
<NativeDirectionalScrollView {...props} ref={this._setNativeRef}>
{refreshControl}
{contentContainer}
</ScrollViewClass>
</NativeDirectionalScrollView>
);
} else if (Platform.OS === 'android') {
// On Android wrap the ScrollView with a AndroidSwipeRefreshLayout.
Expand All @@ -1225,19 +1204,19 @@ class ScrollView extends React.Component<Props, State> {
return React.cloneElement(
refreshControl,
{style: StyleSheet.compose(baseStyle, outer)},
<ScrollViewClass
<NativeDirectionalScrollView
{...props}
style={StyleSheet.compose(baseStyle, inner)}
ref={this._setNativeRef}>
{contentContainer}
</ScrollViewClass>,
</NativeDirectionalScrollView>,
);
}
}
return (
<ScrollViewClass {...props} ref={this._setNativeRef}>
<NativeDirectionalScrollView {...props} ref={this._setNativeRef}>
{contentContainer}
</ScrollViewClass>
</NativeDirectionalScrollView>
);
}
}
Expand Down
13 changes: 4 additions & 9 deletions Libraries/Components/ScrollView/ScrollViewNativeComponentType.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,14 @@
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow strict-local
* @format
* @flow
*/

'use strict';

import type {HostComponent} from '../../Renderer/shims/ReactNativeTypes';
import type {ViewProps} from '../View/ViewPropTypes';
import type {
ViewStyleProp,
DangerouslyImpreciseStyle,
} from '../../StyleSheet/StyleSheet';
import type {ColorValue} from '../../StyleSheet/StyleSheet';
import type {EdgeInsetsProp} from '../../StyleSheet/EdgeInsetsPropType';
import type {ScrollEvent} from '../../Types/CoreEventTypes';
Expand Down Expand Up @@ -45,10 +41,10 @@ export type ScrollViewNativeProps = $ReadOnly<{
fadingEdgeLength?: ?number,
indicatorStyle?: ?('default' | 'black' | 'white'),
keyboardDismissMode?: ?('none' | 'on-drag' | 'interactive'),
maintainVisibleContentPosition?: ?$ReadOnly<{|
maintainVisibleContentPosition?: ?$ReadOnly<{
minIndexForVisible: number,
autoscrollToTopThreshold?: ?number,
|}>,
}>,
maximumZoomScale?: ?number,
minimumZoomScale?: ?number,
nestedScrollEnabled?: ?boolean,
Expand Down Expand Up @@ -78,8 +74,7 @@ export type ScrollViewNativeProps = $ReadOnly<{
snapToStart?: ?boolean,
zoomScale?: ?number,
// Overrides
style?: {...ViewStyleProp, ...} | DangerouslyImpreciseStyle,
onResponderGrant?: ?(e: any) => void | boolean,
onResponderGrant?: ?(e: $FlowFixMe) => void | boolean,
...
}>;

Expand Down

0 comments on commit d4e29ec

Please sign in to comment.