1
1
/* @flow */
2
2
3
3
import PropTypes from 'prop-types'
4
- import ReactNative , { TextInput , Keyboard , UIManager } from 'react-native'
4
+ import ReactNative , { TextInput , Keyboard , UIManager , Platform } from 'react-native'
5
5
import TimerMixin from 'react-timer-mixin'
6
6
7
7
import type { Event } from 'react-native'
@@ -72,8 +72,28 @@ const KeyboardAwareMixin = {
72
72
if ( isAncestor ) {
73
73
// Check if the TextInput will be hidden by the keyboard
74
74
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
+ }
77
97
}
78
98
} )
79
99
}
@@ -108,8 +128,13 @@ const KeyboardAwareMixin = {
108
128
109
129
componentDidMount : function ( ) {
110
130
// 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
+ }
113
138
} ,
114
139
115
140
componentWillUnmount : function ( ) {
@@ -131,6 +156,10 @@ const KeyboardAwareMixin = {
131
156
responder && responder . scrollResponderScrollToEnd ( { animated : animated } )
132
157
} ,
133
158
159
+ scrollForExtraHeightOnAndroid ( extraHeight : number ) {
160
+ this . scrollToPosition ( 0 , this . position . y + extraHeight , true )
161
+ } ,
162
+
134
163
/**
135
164
* @param keyboardOpeningTime: takes a different keyboardOpeningTime in consideration.
136
165
* @param extraHeight: takes an extra height in consideration.
0 commit comments