Skip to content

Commit

Permalink
Modernize ScrollResponder.js
Browse files Browse the repository at this point in the history
Reviewed By: sahrens

Differential Revision: D6661084

fbshipit-source-id: 46cef96dc86842b379728d8465ce4feb408338c7
  • Loading branch information
TheSavior authored and facebook-github-bot committed Jan 5, 2018
1 parent c49d249 commit 45e6fcd
Showing 1 changed file with 34 additions and 42 deletions.
76 changes: 34 additions & 42 deletions Libraries/Components/ScrollResponder.js
Expand Up @@ -11,27 +11,21 @@
*/ */
'use strict'; 'use strict';


var Dimensions = require('Dimensions'); const Dimensions = require('Dimensions');
var FrameRateLogger = require('FrameRateLogger'); const FrameRateLogger = require('FrameRateLogger');
var Keyboard = require('Keyboard'); const Keyboard = require('Keyboard');
var ReactNative = require('ReactNative'); const ReactNative = require('ReactNative');
var Subscribable = require('Subscribable'); const Subscribable = require('Subscribable');
var TextInputState = require('TextInputState'); const TextInputState = require('TextInputState');
var UIManager = require('UIManager'); const UIManager = require('UIManager');


var invariant = require('fbjs/lib/invariant'); const invariant = require('fbjs/lib/invariant');
var nullthrows = require('fbjs/lib/nullthrows'); const nullthrows = require('fbjs/lib/nullthrows');
/* $FlowFixMe(>=0.54.0 site=react_native_oss) This comment suppresses an error const performanceNow = require('fbjs/lib/performanceNow');
* found when Flow v0.54 was deployed. To see the error delete this comment and const warning = require('fbjs/lib/warning');
* run Flow. */
var performanceNow = require('fbjs/lib/performanceNow'); const { ScrollViewManager } = require('NativeModules');
/* $FlowFixMe(>=0.54.0 site=react_native_oss) This comment suppresses an error const { getInstanceFromNode } = require('ReactNativeComponentTree');
* found when Flow v0.54 was deployed. To see the error delete this comment and
* run Flow. */
var warning = require('fbjs/lib/warning');

var { ScrollViewManager } = require('NativeModules');
var { getInstanceFromNode } = require('ReactNativeComponentTree');


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


var IS_ANIMATING_TOUCH_START_THRESHOLD_MS = 16; const IS_ANIMATING_TOUCH_START_THRESHOLD_MS = 16;


type State = { type State = {
isTouching: boolean, isTouching: boolean,
Expand All @@ -123,15 +117,15 @@ type State = {
type Event = Object; type Event = Object;


function isTagInstanceOfTextInput(tag) { function isTagInstanceOfTextInput(tag) {
var instance = getInstanceFromNode(tag); const instance = getInstanceFromNode(tag);
return instance && instance.viewConfig && ( return instance && instance.viewConfig && (
instance.viewConfig.uiViewClassName === 'AndroidTextInput' || instance.viewConfig.uiViewClassName === 'AndroidTextInput' ||
instance.viewConfig.uiViewClassName === 'RCTMultilineTextInputView' || instance.viewConfig.uiViewClassName === 'RCTMultilineTextInputView' ||
instance.viewConfig.uiViewClassName === 'RCTSinglelineTextInputView' instance.viewConfig.uiViewClassName === 'RCTSinglelineTextInputView'
); );
} }


var ScrollResponderMixin = { const ScrollResponderMixin = {
mixins: [Subscribable.Mixin], mixins: [Subscribable.Mixin],
scrollResponderMixinGetInitialState: function(): State { scrollResponderMixinGetInitialState: function(): State {
return { return {
Expand Down Expand Up @@ -182,7 +176,7 @@ var ScrollResponderMixin = {
* *
*/ */
scrollResponderHandleStartShouldSetResponder: function(e: Event): boolean { scrollResponderHandleStartShouldSetResponder: function(e: Event): boolean {
var currentlyFocusedTextInput = TextInputState.currentlyFocusedField(); const currentlyFocusedTextInput = TextInputState.currentlyFocusedField();


if (this.props.keyboardShouldPersistTaps === 'handled' && if (this.props.keyboardShouldPersistTaps === 'handled' &&
currentlyFocusedTextInput != null && currentlyFocusedTextInput != null &&
Expand All @@ -205,9 +199,9 @@ var ScrollResponderMixin = {
*/ */
scrollResponderHandleStartShouldSetResponderCapture: function(e: Event): boolean { scrollResponderHandleStartShouldSetResponderCapture: function(e: Event): boolean {
// First see if we want to eat taps while the keyboard is up // First see if we want to eat taps while the keyboard is up
var currentlyFocusedTextInput = TextInputState.currentlyFocusedField(); const currentlyFocusedTextInput = TextInputState.currentlyFocusedField();
var {keyboardShouldPersistTaps} = this.props; const {keyboardShouldPersistTaps} = this.props;
var keyboardNeverPersistTaps = !keyboardShouldPersistTaps || const keyboardNeverPersistTaps = !keyboardShouldPersistTaps ||
keyboardShouldPersistTaps === 'never'; keyboardShouldPersistTaps === 'never';
if (keyboardNeverPersistTaps && if (keyboardNeverPersistTaps &&
currentlyFocusedTextInput != null && currentlyFocusedTextInput != null &&
Expand Down Expand Up @@ -255,7 +249,7 @@ var ScrollResponderMixin = {
* @param {SyntheticEvent} e Event. * @param {SyntheticEvent} e Event.
*/ */
scrollResponderHandleTouchEnd: function(e: Event) { scrollResponderHandleTouchEnd: function(e: Event) {
var nativeEvent = e.nativeEvent; const nativeEvent = e.nativeEvent;
this.state.isTouching = nativeEvent.touches.length !== 0; this.state.isTouching = nativeEvent.touches.length !== 0;
this.props.onTouchEnd && this.props.onTouchEnd(e); this.props.onTouchEnd && this.props.onTouchEnd(e);
}, },
Expand All @@ -278,7 +272,7 @@ var ScrollResponderMixin = {


// By default scroll views will unfocus a textField // By default scroll views will unfocus a textField
// if another touch occurs outside of it // if another touch occurs outside of it
var currentlyFocusedTextInput = TextInputState.currentlyFocusedField(); const currentlyFocusedTextInput = TextInputState.currentlyFocusedField();
if (this.props.keyboardShouldPersistTaps !== true && if (this.props.keyboardShouldPersistTaps !== true &&
this.props.keyboardShouldPersistTaps !== 'always' && this.props.keyboardShouldPersistTaps !== 'always' &&
currentlyFocusedTextInput != null && currentlyFocusedTextInput != null &&
Expand Down Expand Up @@ -388,9 +382,9 @@ var ScrollResponderMixin = {
* a touch has just started or ended. * a touch has just started or ended.
*/ */
scrollResponderIsAnimating: function(): boolean { scrollResponderIsAnimating: function(): boolean {
var now = performanceNow(); const now = performanceNow();
var timeSinceLastMomentumScrollEnd = now - this.state.lastMomentumScrollEndTime; const timeSinceLastMomentumScrollEnd = now - this.state.lastMomentumScrollEndTime;
var isAnimating = timeSinceLastMomentumScrollEnd < IS_ANIMATING_TOUCH_START_THRESHOLD_MS || const isAnimating = timeSinceLastMomentumScrollEnd < IS_ANIMATING_TOUCH_START_THRESHOLD_MS ||
this.state.lastMomentumScrollEndTime < this.state.lastMomentumScrollBeginTime; this.state.lastMomentumScrollEndTime < this.state.lastMomentumScrollBeginTime;
return isAnimating; return isAnimating;
}, },
Expand Down Expand Up @@ -469,15 +463,13 @@ var ScrollResponderMixin = {
* @platform ios * @platform ios
*/ */
scrollResponderZoomTo: function( scrollResponderZoomTo: function(
rect: { x: number, y: number, width: number, height: number, animated?: boolean }, rect: {| x: number, y: number, width: number, height: number, animated?: boolean |},
animated?: boolean // deprecated, put this inside the rect argument instead animated?: boolean // deprecated, put this inside the rect argument instead
) { ) {
invariant(ScrollViewManager && ScrollViewManager.zoomToRect, 'zoomToRect is not implemented'); invariant(ScrollViewManager && ScrollViewManager.zoomToRect, 'zoomToRect is not implemented');
if ('animated' in rect) { if ('animated' in rect) {
/* $FlowFixMe(>=0.60.0 site=react_native_fb) This comment suppresses an animated = rect.animated;
* error found when Flow v0.60 was deployed. To see the error delete this delete rect.animated;
* comment and run Flow. */
var { animated, ...rect } = rect;
} else if (typeof animated !== 'undefined') { } else if (typeof animated !== 'undefined') {
console.warn('`scrollResponderZoomTo` `animated` argument is deprecated. Use `options.animated` instead'); console.warn('`scrollResponderZoomTo` `animated` argument is deprecated. Use `options.animated` instead');
} }
Expand Down Expand Up @@ -527,11 +519,11 @@ var ScrollResponderMixin = {
* @param {number} height Height of the text input. * @param {number} height Height of the text input.
*/ */
scrollResponderInputMeasureAndScrollToKeyboard: function(left: number, top: number, width: number, height: number) { scrollResponderInputMeasureAndScrollToKeyboard: function(left: number, top: number, width: number, height: number) {
var keyboardScreenY = Dimensions.get('window').height; let keyboardScreenY = Dimensions.get('window').height;
if (this.keyboardWillOpenTo) { if (this.keyboardWillOpenTo) {
keyboardScreenY = this.keyboardWillOpenTo.endCoordinates.screenY; keyboardScreenY = this.keyboardWillOpenTo.endCoordinates.screenY;
} }
var scrollOffsetY = top - keyboardScreenY + height + this.additionalScrollOffset; let scrollOffsetY = top - keyboardScreenY + height + this.additionalScrollOffset;


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


}; };


var ScrollResponder = { const ScrollResponder = {
Mixin: ScrollResponderMixin, Mixin: ScrollResponderMixin,
}; };


Expand Down

0 comments on commit 45e6fcd

Please sign in to comment.