11/* @flow */
22
33import PropTypes from 'prop-types'
4- import ReactNative , { TextInput , Keyboard , UIManager } from 'react-native'
4+ import ReactNative , { TextInput , Keyboard , UIManager , Platform } from 'react-native'
55import TimerMixin from 'react-timer-mixin'
66
77import type { Event } from 'react-native'
@@ -72,8 +72,28 @@ const KeyboardAwareMixin = {
7272 if ( isAncestor ) {
7373 // Check if the TextInput will be hidden by the keyboard
7474 UIManager . measureInWindow ( currentlyFocusedField , ( x , y , width , height ) => {
75- if ( y + height > frames . endCoordinates . screenY - this . props . extraScrollHeight - this . props . extraHeight ) {
76- this . scrollToFocusedInputWithNodeHandle ( currentlyFocusedField )
75+ const textInputBottomPosition = y + height
76+ const keyboardPosition = frames . endCoordinates . screenY
77+ const totalExtraHeight = this . props . extraScrollHeight + this . props . extraHeight
78+
79+ if ( Platform . OS === 'ios' ) {
80+ if ( textInputBottomPosition > keyboardPosition - totalExtraHeight ) {
81+ this . scrollToFocusedInputWithNodeHandle ( currentlyFocusedField )
82+ }
83+ } else {
84+
85+ // on android, the system would scroll the text input just above the keyboard
86+ // so we just neet to scroll the extra height part
87+ if ( textInputBottomPosition > keyboardPosition ) {
88+ // since the system already scrolled the whole view up
89+ // we should reduce that amount
90+ keyboardSpace = keyboardSpace - ( textInputBottomPosition - keyboardPosition )
91+ this . setState ( { keyboardSpace} )
92+
93+ this . scrollForExtraHeightOnAndroid ( totalExtraHeight )
94+ } else if ( textInputBottomPosition > keyboardPosition - totalExtraHeight ) {
95+ this . scrollForExtraHeightOnAndroid ( totalExtraHeight - ( keyboardPosition - textInputBottomPosition ) )
96+ }
7797 }
7898 } )
7999 }
@@ -108,8 +128,13 @@ const KeyboardAwareMixin = {
108128
109129 componentDidMount : function ( ) {
110130 // Keyboard events
111- this . keyboardWillShowEvent = Keyboard . addListener ( 'keyboardWillShow' , this . updateKeyboardSpace )
112- this . keyboardWillHideEvent = Keyboard . addListener ( 'keyboardWillHide' , this . resetKeyboardSpace )
131+ if ( Platform . OS === 'ios' ) {
132+ this . keyboardWillShowEvent = Keyboard . addListener ( 'keyboardWillShow' , this . updateKeyboardSpace )
133+ this . keyboardWillHideEvent = Keyboard . addListener ( 'keyboardWillHide' , this . resetKeyboardSpace )
134+ } else if ( Platform . OS === 'android' && this . props . enableOnAndroid ) {
135+ this . keyboardWillShowEvent = Keyboard . addListener ( 'keyboardDidShow' , this . updateKeyboardSpace )
136+ this . keyboardWillHideEvent = Keyboard . addListener ( 'keyboardDidHide' , this . resetKeyboardSpace )
137+ }
113138 } ,
114139
115140 componentWillUnmount : function ( ) {
@@ -131,6 +156,10 @@ const KeyboardAwareMixin = {
131156 responder && responder . scrollResponderScrollToEnd ( { animated : animated } )
132157 } ,
133158
159+ scrollForExtraHeightOnAndroid ( extraHeight : number ) {
160+ this . scrollToPosition ( 0 , this . position . y + extraHeight , true )
161+ } ,
162+
134163 /**
135164 * @param keyboardOpeningTime: takes a different keyboardOpeningTime in consideration.
136165 * @param extraHeight: takes an extra height in consideration.
0 commit comments