1
1
import React from 'react' ;
2
- import KeyboardEvents from 'react-native-keyboardevents' ;
3
- var KeyboardEventEmitter = KeyboardEvents . Emitter ;
2
+
3
+ import {
4
+ Keyboard ,
5
+ } from 'react-native' ;
6
+
4
7
5
8
var KeyboardListener = {
6
9
getInitialState : function ( ) {
@@ -13,9 +16,13 @@ var KeyboardListener = {
13
16
return this . state . keyboardSpace > 0 ;
14
17
} ,
15
18
16
- updateKeyboardSpace : function ( frames ) {
17
- if ( this . isMounted ( ) && frames && frames . end ) {
18
- this . setState ( { keyboardSpace : frames . end . height } ) ;
19
+ isKeyboardVisible : function ( ) {
20
+ return this . state . keyboardSpace > 0 ;
21
+ } ,
22
+
23
+ updateKeyboardSpace : function ( e ) {
24
+ if ( this . isMounted ( ) && e && e . endCoordinates ) {
25
+ this . setState ( { keyboardSpace : e . endCoordinates . height } ) ;
19
26
}
20
27
} ,
21
28
@@ -25,15 +32,32 @@ var KeyboardListener = {
25
32
}
26
33
} ,
27
34
35
+ // onKeyboardHideCallback: function() {
36
+ // // TODO handle case when the keyboard never showed up in the first place
37
+ // // Might want to use state to check that it did showed up
38
+ // if (this.params.onKeyboardHide) {
39
+ // this.params.onKeyboardHide.call(this.originalComponent());
40
+ // }
41
+ // },
42
+
43
+
28
44
componentDidMount : function ( ) {
29
- KeyboardEventEmitter . on ( KeyboardEvents . KeyboardDidShowEvent , this . updateKeyboardSpace ) ;
30
- KeyboardEventEmitter . on ( KeyboardEvents . KeyboardWillHideEvent , this . resetKeyboardSpace ) ;
45
+ this . _keyboardSubscriptions = [ ] ;
46
+ this . _keyboardSubscriptions . push ( Keyboard . addListener ( 'keyboardWillShow' , this . updateKeyboardSpace ) ) ;
47
+ this . _keyboardSubscriptions . push ( Keyboard . addListener ( 'keyboardDidShow' , this . updateKeyboardSpace ) ) ;
48
+ this . _keyboardSubscriptions . push ( Keyboard . addListener ( 'keyboardWillHide' , this . resetKeyboardSpace ) ) ;
49
+ this . _keyboardSubscriptions . push ( Keyboard . addListener ( 'keyboardDidHide' , this . resetKeyboardSpace ) ) ;
50
+ //if (this.params.onKeyboardHide) {
51
+ // this._keyboardSubscriptions.push(Keyboard.addListener('keyboardDidHide', this.onKeyboardHideCallback));
52
+ //}
31
53
} ,
32
54
33
55
componentWillUnmount : function ( ) {
34
- KeyboardEventEmitter . off ( KeyboardEvents . KeyboardDidShowEvent , this . updateKeyboardSpace ) ;
35
- KeyboardEventEmitter . off ( KeyboardEvents . KeyboardWillHideEvent , this . resetKeyboardSpace ) ;
56
+ this . _keyboardSubscriptions . forEach ( ( subscription ) => {
57
+ subscription . remove ( ) ;
58
+ } ) ;
36
59
} ,
60
+
37
61
} ;
38
62
39
63
export default KeyboardListener ;
0 commit comments