From 6681274f432db8453301beab4f27dc71fbf273d6 Mon Sep 17 00:00:00 2001 From: Kevin Gozali Date: Fri, 3 Apr 2015 11:13:20 -0700 Subject: [PATCH] [madman] prevent pulling the content down inconsistently when the keyboard shows up --- Libraries/Components/ScrollResponder.js | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/Libraries/Components/ScrollResponder.js b/Libraries/Components/ScrollResponder.js index 009bbebb45..1f3d493f3c 100644 --- a/Libraries/Components/ScrollResponder.js +++ b/Libraries/Components/ScrollResponder.js @@ -364,10 +364,16 @@ var ScrollResponderMixin = { /** * This method should be used as the callback to onFocus in a TextInputs' * parent view. Note that any module using this mixin needs to return - * the parent view's ref in getScrollViewRef() in order to use this method + * the parent view's ref in getScrollViewRef() in order to use this method. + * @param {any} nodeHandle The TextInput node handle + * @param {number} additionalOffset The scroll view's top "contentInset". + * Default is 0. + * @param {bool} preventNegativeScrolling Whether to allow pulling the content + * down to make it meet the keyboard's top. Default is false. */ - scrollResponderScrollNativeHandleToKeyboard: function(nodeHandle: any, additionalOffset?: number) { + scrollResponderScrollNativeHandleToKeyboard: function(nodeHandle: any, additionalOffset?: number, preventNegativeScrollOffset?: bool) { this.additionalScrollOffset = additionalOffset || 0; + this.preventNegativeScrollOffset = !!preventNegativeScrollOffset; RCTUIManager.measureLayout( nodeHandle, this.getNodeHandle(), @@ -386,14 +392,23 @@ var ScrollResponderMixin = { * @param {number} width Width 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) { if (this.keyboardWillOpenTo) { var scrollOffsetY = top - this.keyboardWillOpenTo.endCoordinates.screenY + height + this.additionalScrollOffset; + + // By default, this can scroll with negative offset, pulling the content + // down so that the target component's bottom meets the keyboard's top. + // If requested otherwise, cap the offset at 0 minimum to avoid content + // shifting down. + if (this.preventNegativeScrollOffset) { + scrollOffsetY = Math.max(0, scrollOffsetY); + } this.scrollResponderScrollTo(0, scrollOffsetY); } this.additionalOffset = 0; + this.preventNegativeScrollOffset = false; }, scrollResponderTextInputFocusError: function(e: Event) {