8
8
StatusBar ,
9
9
} from 'react-native' ;
10
10
import { KeyboardContext } from '../context' ;
11
+ import PropTypes from 'prop-types' ;
11
12
12
13
/**
13
14
* KeyboardCompatibleView is HOC component similar to [KeyboardAvoidingView](https://facebook.github.io/react-native/docs/keyboardavoidingview),
@@ -25,13 +26,40 @@ import { KeyboardContext } from '../context';
25
26
* ```
26
27
*/
27
28
export class KeyboardCompatibleView extends React . PureComponent {
29
+ static propTypes = {
30
+ keyboardDismissAnimationDuration : PropTypes . number ,
31
+ keyboardOpenAnimationDuration : PropTypes . number ,
32
+ enabled : PropTypes . bool ,
33
+ } ;
34
+ static defaultProps = {
35
+ keyboardDismissAnimationDuration : 500 ,
36
+ keyboardOpenAnimationDuration : 500 ,
37
+ enabled : true ,
38
+ } ;
39
+
28
40
constructor ( props ) {
29
41
super ( props ) ;
30
42
31
43
this . state = {
32
44
channelHeight : new Animated . Value ( '100%' ) ,
45
+ // For some reason UI doesn't update sometimes, when state is updated using setValue.
46
+ // So to force update the component, I am using following key, which will be increamented
47
+ // for every keyboard slide up and down.
48
+ key : 0 ,
33
49
} ;
34
50
51
+ this . setupListeners ( ) ;
52
+
53
+ this . _keyboardOpen = false ;
54
+ // Following variable takes care of race condition between keyboardDidHide and keyboardDidShow.
55
+ this . _hidingKeyboardInProgress = false ;
56
+ this . rootChannelView = false ;
57
+ this . initialHeight = undefined ;
58
+ }
59
+
60
+ setupListeners = ( ) => {
61
+ if ( ! this . props . enabled ) return ;
62
+
35
63
if ( Platform . OS === 'ios' ) {
36
64
this . keyboardDidShowListener = Keyboard . addListener (
37
65
'keyboardWillShow' ,
@@ -51,21 +79,15 @@ export class KeyboardCompatibleView extends React.PureComponent {
51
79
'keyboardDidHide' ,
52
80
this . keyboardDidHide ,
53
81
) ;
54
-
55
- this . _keyboardOpen = false ;
56
- // Following variable takes care of race condition between keyboardDidHide and keyboardDidShow.
57
- this . _hidingKeyboardInProgress = false ;
58
- this . rootChannelView = false ;
59
- this . initialHeight = undefined ;
60
- }
61
-
82
+ } ;
62
83
componentWillUnmount ( ) {
63
84
this . keyboardDidShowListener . remove ( ) ;
64
85
this . keyboardDidHideListener . remove ( ) ;
65
86
}
66
87
67
88
// TODO: Better to extract following functions to different HOC.
68
89
keyboardDidShow = ( e ) => {
90
+ if ( ! this . props . enabled ) return ;
69
91
const keyboardHidingInProgressBeforeMeasure = this
70
92
. _hidingKeyboardInProgress ;
71
93
const keyboardHeight = e . endCoordinates . height ;
@@ -79,9 +101,6 @@ export class KeyboardCompatibleView extends React.PureComponent {
79
101
! keyboardHidingInProgressBeforeMeasure &&
80
102
this . _hidingKeyboardInProgress
81
103
) {
82
- console . log (
83
- 'Aborting keyboardDidShow operation since hide is in progress!' ,
84
- ) ;
85
104
return ;
86
105
}
87
106
@@ -97,10 +116,13 @@ export class KeyboardCompatibleView extends React.PureComponent {
97
116
98
117
Animated . timing ( this . state . channelHeight , {
99
118
toValue : finalHeight ,
100
- duration : 500 ,
119
+ duration : this . props . keyboardOpenAnimationDuration ,
101
120
} ) . start ( ( ) => {
102
121
// Force the final value, in case animation halted in between.
103
122
this . state . channelHeight . setValue ( finalHeight ) ;
123
+ this . setState ( {
124
+ key : this . state . key + 1 ,
125
+ } ) ;
104
126
} ) ;
105
127
} ) ;
106
128
this . _keyboardOpen = true ;
@@ -110,12 +132,15 @@ export class KeyboardCompatibleView extends React.PureComponent {
110
132
this . _hidingKeyboardInProgress = true ;
111
133
Animated . timing ( this . state . channelHeight , {
112
134
toValue : this . initialHeight ,
113
- duration : 500 ,
135
+ duration : this . props . keyboardDismissAnimationDuration ,
114
136
} ) . start ( ( ) => {
115
137
// Force the final value, in case animation halted in between.
116
138
this . state . channelHeight . setValue ( this . initialHeight ) ;
117
139
this . _hidingKeyboardInProgress = false ;
118
140
this . _keyboardOpen = false ;
141
+ this . setState ( {
142
+ key : this . state . key + 1 ,
143
+ } ) ;
119
144
} ) ;
120
145
} ;
121
146
@@ -128,7 +153,7 @@ export class KeyboardCompatibleView extends React.PureComponent {
128
153
129
154
Animated . timing ( this . state . channelHeight , {
130
155
toValue : this . initialHeight ,
131
- duration : 500 ,
156
+ duration : this . props . keyboardDismissAnimationDuration ,
132
157
} ) . start ( ( response ) => {
133
158
this . state . channelHeight . setValue ( this . initialHeight ) ;
134
159
if ( response && ! response . finished ) {
@@ -138,7 +163,7 @@ export class KeyboardCompatibleView extends React.PureComponent {
138
163
// during keyboard dismissal.
139
164
setTimeout ( ( ) => {
140
165
resolve ( ) ;
141
- } , 500 ) ;
166
+ } , this . props . keyboardDismissAnimationDuration ) ;
142
167
return ;
143
168
}
144
169
@@ -164,7 +189,7 @@ export class KeyboardCompatibleView extends React.PureComponent {
164
189
165
190
dismissKeyboard = async ( ) => {
166
191
Keyboard . dismiss ( ) ;
167
- await this . keyboardWillDismiss ( ) ;
192
+ if ( this . props . enabled ) await this . keyboardWillDismiss ( ) ;
168
193
} ;
169
194
170
195
getContext = ( ) => ( {
0 commit comments