Skip to content

Commit 45e6fcd

Browse files
elicwhitefacebook-github-bot
authored andcommitted
Modernize ScrollResponder.js
Reviewed By: sahrens Differential Revision: D6661084 fbshipit-source-id: 46cef96dc86842b379728d8465ce4feb408338c7
1 parent c49d249 commit 45e6fcd

File tree

1 file changed

+34
-42
lines changed

1 file changed

+34
-42
lines changed

Diff for: Libraries/Components/ScrollResponder.js

+34-42
Original file line numberDiff line numberDiff line change
@@ -11,27 +11,21 @@
1111
*/
1212
'use strict';
1313

14-
var Dimensions = require('Dimensions');
15-
var FrameRateLogger = require('FrameRateLogger');
16-
var Keyboard = require('Keyboard');
17-
var ReactNative = require('ReactNative');
18-
var Subscribable = require('Subscribable');
19-
var TextInputState = require('TextInputState');
20-
var UIManager = require('UIManager');
21-
22-
var invariant = require('fbjs/lib/invariant');
23-
var nullthrows = require('fbjs/lib/nullthrows');
24-
/* $FlowFixMe(>=0.54.0 site=react_native_oss) This comment suppresses an error
25-
* found when Flow v0.54 was deployed. To see the error delete this comment and
26-
* run Flow. */
27-
var performanceNow = require('fbjs/lib/performanceNow');
28-
/* $FlowFixMe(>=0.54.0 site=react_native_oss) This comment suppresses an error
29-
* found when Flow v0.54 was deployed. To see the error delete this comment and
30-
* run Flow. */
31-
var warning = require('fbjs/lib/warning');
32-
33-
var { ScrollViewManager } = require('NativeModules');
34-
var { getInstanceFromNode } = require('ReactNativeComponentTree');
14+
const Dimensions = require('Dimensions');
15+
const FrameRateLogger = require('FrameRateLogger');
16+
const Keyboard = require('Keyboard');
17+
const ReactNative = require('ReactNative');
18+
const Subscribable = require('Subscribable');
19+
const TextInputState = require('TextInputState');
20+
const UIManager = require('UIManager');
21+
22+
const invariant = require('fbjs/lib/invariant');
23+
const nullthrows = require('fbjs/lib/nullthrows');
24+
const performanceNow = require('fbjs/lib/performanceNow');
25+
const warning = require('fbjs/lib/warning');
26+
27+
const { ScrollViewManager } = require('NativeModules');
28+
const { getInstanceFromNode } = require('ReactNativeComponentTree');
3529

3630
/**
3731
* Mixin that can be integrated in order to handle scrolling that plays well
@@ -111,7 +105,7 @@ var { getInstanceFromNode } = require('ReactNativeComponentTree');
111105
* this.props.onKeyboardDidHide
112106
*/
113107

114-
var IS_ANIMATING_TOUCH_START_THRESHOLD_MS = 16;
108+
const IS_ANIMATING_TOUCH_START_THRESHOLD_MS = 16;
115109

116110
type State = {
117111
isTouching: boolean,
@@ -123,15 +117,15 @@ type State = {
123117
type Event = Object;
124118

125119
function isTagInstanceOfTextInput(tag) {
126-
var instance = getInstanceFromNode(tag);
120+
const instance = getInstanceFromNode(tag);
127121
return instance && instance.viewConfig && (
128122
instance.viewConfig.uiViewClassName === 'AndroidTextInput' ||
129123
instance.viewConfig.uiViewClassName === 'RCTMultilineTextInputView' ||
130124
instance.viewConfig.uiViewClassName === 'RCTSinglelineTextInputView'
131125
);
132126
}
133127

134-
var ScrollResponderMixin = {
128+
const ScrollResponderMixin = {
135129
mixins: [Subscribable.Mixin],
136130
scrollResponderMixinGetInitialState: function(): State {
137131
return {
@@ -182,7 +176,7 @@ var ScrollResponderMixin = {
182176
*
183177
*/
184178
scrollResponderHandleStartShouldSetResponder: function(e: Event): boolean {
185-
var currentlyFocusedTextInput = TextInputState.currentlyFocusedField();
179+
const currentlyFocusedTextInput = TextInputState.currentlyFocusedField();
186180

187181
if (this.props.keyboardShouldPersistTaps === 'handled' &&
188182
currentlyFocusedTextInput != null &&
@@ -205,9 +199,9 @@ var ScrollResponderMixin = {
205199
*/
206200
scrollResponderHandleStartShouldSetResponderCapture: function(e: Event): boolean {
207201
// First see if we want to eat taps while the keyboard is up
208-
var currentlyFocusedTextInput = TextInputState.currentlyFocusedField();
209-
var {keyboardShouldPersistTaps} = this.props;
210-
var keyboardNeverPersistTaps = !keyboardShouldPersistTaps ||
202+
const currentlyFocusedTextInput = TextInputState.currentlyFocusedField();
203+
const {keyboardShouldPersistTaps} = this.props;
204+
const keyboardNeverPersistTaps = !keyboardShouldPersistTaps ||
211205
keyboardShouldPersistTaps === 'never';
212206
if (keyboardNeverPersistTaps &&
213207
currentlyFocusedTextInput != null &&
@@ -255,7 +249,7 @@ var ScrollResponderMixin = {
255249
* @param {SyntheticEvent} e Event.
256250
*/
257251
scrollResponderHandleTouchEnd: function(e: Event) {
258-
var nativeEvent = e.nativeEvent;
252+
const nativeEvent = e.nativeEvent;
259253
this.state.isTouching = nativeEvent.touches.length !== 0;
260254
this.props.onTouchEnd && this.props.onTouchEnd(e);
261255
},
@@ -278,7 +272,7 @@ var ScrollResponderMixin = {
278272

279273
// By default scroll views will unfocus a textField
280274
// if another touch occurs outside of it
281-
var currentlyFocusedTextInput = TextInputState.currentlyFocusedField();
275+
const currentlyFocusedTextInput = TextInputState.currentlyFocusedField();
282276
if (this.props.keyboardShouldPersistTaps !== true &&
283277
this.props.keyboardShouldPersistTaps !== 'always' &&
284278
currentlyFocusedTextInput != null &&
@@ -388,9 +382,9 @@ var ScrollResponderMixin = {
388382
* a touch has just started or ended.
389383
*/
390384
scrollResponderIsAnimating: function(): boolean {
391-
var now = performanceNow();
392-
var timeSinceLastMomentumScrollEnd = now - this.state.lastMomentumScrollEndTime;
393-
var isAnimating = timeSinceLastMomentumScrollEnd < IS_ANIMATING_TOUCH_START_THRESHOLD_MS ||
385+
const now = performanceNow();
386+
const timeSinceLastMomentumScrollEnd = now - this.state.lastMomentumScrollEndTime;
387+
const isAnimating = timeSinceLastMomentumScrollEnd < IS_ANIMATING_TOUCH_START_THRESHOLD_MS ||
394388
this.state.lastMomentumScrollEndTime < this.state.lastMomentumScrollBeginTime;
395389
return isAnimating;
396390
},
@@ -469,15 +463,13 @@ var ScrollResponderMixin = {
469463
* @platform ios
470464
*/
471465
scrollResponderZoomTo: function(
472-
rect: { x: number, y: number, width: number, height: number, animated?: boolean },
466+
rect: {| x: number, y: number, width: number, height: number, animated?: boolean |},
473467
animated?: boolean // deprecated, put this inside the rect argument instead
474468
) {
475469
invariant(ScrollViewManager && ScrollViewManager.zoomToRect, 'zoomToRect is not implemented');
476470
if ('animated' in rect) {
477-
/* $FlowFixMe(>=0.60.0 site=react_native_fb) This comment suppresses an
478-
* error found when Flow v0.60 was deployed. To see the error delete this
479-
* comment and run Flow. */
480-
var { animated, ...rect } = rect;
471+
animated = rect.animated;
472+
delete rect.animated;
481473
} else if (typeof animated !== 'undefined') {
482474
console.warn('`scrollResponderZoomTo` `animated` argument is deprecated. Use `options.animated` instead');
483475
}
@@ -527,11 +519,11 @@ var ScrollResponderMixin = {
527519
* @param {number} height Height of the text input.
528520
*/
529521
scrollResponderInputMeasureAndScrollToKeyboard: function(left: number, top: number, width: number, height: number) {
530-
var keyboardScreenY = Dimensions.get('window').height;
522+
let keyboardScreenY = Dimensions.get('window').height;
531523
if (this.keyboardWillOpenTo) {
532524
keyboardScreenY = this.keyboardWillOpenTo.endCoordinates.screenY;
533525
}
534-
var scrollOffsetY = top - keyboardScreenY + height + this.additionalScrollOffset;
526+
let scrollOffsetY = top - keyboardScreenY + height + this.additionalScrollOffset;
535527

536528
// By default, this can scroll with negative offset, pulling the content
537529
// down so that the target component's bottom meets the keyboard's top.
@@ -557,7 +549,7 @@ var ScrollResponderMixin = {
557549
* The `keyboardWillShow` is called before input focus.
558550
*/
559551
componentWillMount: function() {
560-
var {keyboardShouldPersistTaps} = this.props;
552+
const {keyboardShouldPersistTaps} = this.props;
561553
warning(
562554
typeof keyboardShouldPersistTaps !== 'boolean',
563555
`'keyboardShouldPersistTaps={${keyboardShouldPersistTaps}}' is deprecated. `
@@ -626,7 +618,7 @@ var ScrollResponderMixin = {
626618

627619
};
628620

629-
var ScrollResponder = {
621+
const ScrollResponder = {
630622
Mixin: ScrollResponderMixin,
631623
};
632624

0 commit comments

Comments
 (0)