Skip to content

Commit 5105c09

Browse files
James IdeFacebook Github Bot
authored andcommitted
Fix missing methods in Keyboard module
Summary: They keyboard module is an instance of `NativeEventEmitter` which is an instance of `EventEmitter`. But the exported module only has a small subset of the APIs. This broke existing codebases which are using the methods not exported currently. The PR just reassigns the variable before exporting so that the actual module is exported instead of the dummy object used for documentation. It also fixes a layout issue in the documentation. Closes facebook#10671 Differential Revision: D4110355 fbshipit-source-id: a6757f3ca8c2494970ba221b10a7e6e9a5f2d64d
1 parent 29888b5 commit 5105c09

File tree

1 file changed

+47
-18
lines changed

1 file changed

+47
-18
lines changed

Libraries/Components/Keyboard/Keyboard.js

Lines changed: 47 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,41 @@
1111
*/
1212
'use strict';
1313

14+
const invariant = require('fbjs/lib/invariant');
1415
const NativeEventEmitter = require('NativeEventEmitter');
1516
const KeyboardObserver = require('NativeModules').KeyboardObserver;
1617
const dismissKeyboard = require('dismissKeyboard');
1718
const KeyboardEventEmitter = new NativeEventEmitter(KeyboardObserver);
1819

20+
type KeyboardEventName =
21+
| 'keyboardWillShow'
22+
| 'keyboardDidShow'
23+
| 'keyboardWillHide'
24+
| 'keyboardDidHide'
25+
| 'keyboardWillChangeFrame'
26+
| 'keyboardDidChangeFrame';
27+
28+
type KeyboardEventData = {
29+
endCoordinates: {
30+
width: number,
31+
height: number,
32+
screenX: number,
33+
screenY: number,
34+
},
35+
};
36+
37+
type KeyboardEventListener = (e: KeyboardEventData) => void;
38+
1939
// The following object exists for documentation purposes
2040
// Actual work happens in
2141
// https://github.com/facebook/react-native/blob/master/Libraries/EventEmitter/NativeEventEmitter.js
42+
2243
/**
23-
* `Keyboard` component to control keyboard events.
44+
* `Keyboard` module to control keyboard events.
2445
*
2546
* ### Usage
2647
*
27-
* The Keyboard component allows you to listen for native events and react to them, as
48+
* The Keyboard module allows you to listen for native events and react to them, as
2849
* well as make changes to the keyboard, like dismissing it.
2950
*
3051
*```
@@ -60,52 +81,60 @@ const KeyboardEventEmitter = new NativeEventEmitter(KeyboardObserver);
6081
* }
6182
*```
6283
*/
63-
module.exports = {
6484

85+
let Keyboard = {
6586
/**
6687
* The `addListener` function connects a JavaScript function to an identified native
6788
* keyboard notification event.
6889
*
6990
* This function then returns the reference to the listener.
7091
*
71-
* @param {string} nativeEvent The `nativeEvent` is the string that identifies the event you're listening for. This
92+
* @param {string} eventName The `nativeEvent` is the string that identifies the event you're listening for. This
7293
*can be any of the following:
94+
*
7395
* - `keyboardWillShow`
7496
* - `keyboardDidShow`
7597
* - `keyboardWillHide`
7698
* - `keyboardDidHide`
7799
* - `keyboardWillChangeFrame`
78100
* - `keyboardDidChangeFrame`
79101
*
80-
* @param {function} jsFunction function to be called when the event fires.
102+
* @param {function} callback function to be called when the event fires.
81103
*/
82-
addListener (nativeEvent: string, jsFunction: Function) {
83-
return KeyboardEventEmitter.addListener(nativeEvent, jsFunction);
104+
addListener(eventName: KeyboardEventName, callback: KeyboardEventListener) {
105+
invariant(false, 'Dummy method used for documentation');
84106
},
85107

86108
/**
87-
* Removes all listeners for a specific event type.
109+
* Removes a specific listener.
88110
*
89-
* @param {string} eventType The native event string listeners are watching which will be removed.
111+
* @param {string} eventName The `nativeEvent` is the string that identifies the event you're listening for.
112+
* @param {function} callback function to be called when the event fires.
90113
*/
91-
removeAllListeners (eventType: string) {
92-
KeyboardEventEmitter.removeAllListeners(eventType);
114+
removeListener(eventName: KeyboardEventName, callback: Function) {
115+
invariant(false, 'Dummy method used for documentation');
93116
},
94117

95118
/**
96-
* Removes a specific subscription.
119+
* Removes all listeners for a specific event type.
97120
*
98-
* @param {EmitterSubscription} subscription The subscription emitter to be removed.
121+
* @param {string} eventType The native event string listeners are watching which will be removed.
99122
*/
100-
removeSubscription (subscription: Object) {
101-
KeyboardEventEmitter.removeSubscription(subscription);
123+
removeAllListeners(eventName: KeyboardEventName) {
124+
invariant(false, 'Dummy method used for documentation');
102125
},
103126

104127
/**
105128
* Dismisses the active keyboard and removes focus.
106129
*/
107-
dismiss () {
108-
dismissKeyboard();
130+
dismiss() {
131+
invariant(false, 'Dummy method used for documentation');
109132
}
110-
111133
};
134+
135+
// Throw away the dummy object and reassign it to original module
136+
Keyboard = KeyboardEventEmitter;
137+
Keyboard.dismiss = dismissKeyboard;
138+
139+
module.exports = Keyboard;
140+

0 commit comments

Comments
 (0)