|
11 | 11 | */ |
12 | 12 | 'use strict'; |
13 | 13 |
|
| 14 | +const invariant = require('fbjs/lib/invariant'); |
14 | 15 | const NativeEventEmitter = require('NativeEventEmitter'); |
15 | 16 | const KeyboardObserver = require('NativeModules').KeyboardObserver; |
16 | 17 | const dismissKeyboard = require('dismissKeyboard'); |
17 | 18 | const KeyboardEventEmitter = new NativeEventEmitter(KeyboardObserver); |
18 | 19 |
|
| 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 | + |
19 | 39 | // The following object exists for documentation purposes |
20 | 40 | // Actual work happens in |
21 | 41 | // https://github.com/facebook/react-native/blob/master/Libraries/EventEmitter/NativeEventEmitter.js |
| 42 | + |
22 | 43 | /** |
23 | | - * `Keyboard` component to control keyboard events. |
| 44 | + * `Keyboard` module to control keyboard events. |
24 | 45 | * |
25 | 46 | * ### Usage |
26 | 47 | * |
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 |
28 | 49 | * well as make changes to the keyboard, like dismissing it. |
29 | 50 | * |
30 | 51 | *``` |
@@ -60,52 +81,60 @@ const KeyboardEventEmitter = new NativeEventEmitter(KeyboardObserver); |
60 | 81 | * } |
61 | 82 | *``` |
62 | 83 | */ |
63 | | -module.exports = { |
64 | 84 |
|
| 85 | +let Keyboard = { |
65 | 86 | /** |
66 | 87 | * The `addListener` function connects a JavaScript function to an identified native |
67 | 88 | * keyboard notification event. |
68 | 89 | * |
69 | 90 | * This function then returns the reference to the listener. |
70 | 91 | * |
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 |
72 | 93 | *can be any of the following: |
| 94 | + * |
73 | 95 | * - `keyboardWillShow` |
74 | 96 | * - `keyboardDidShow` |
75 | 97 | * - `keyboardWillHide` |
76 | 98 | * - `keyboardDidHide` |
77 | 99 | * - `keyboardWillChangeFrame` |
78 | 100 | * - `keyboardDidChangeFrame` |
79 | 101 | * |
80 | | - * @param {function} jsFunction function to be called when the event fires. |
| 102 | + * @param {function} callback function to be called when the event fires. |
81 | 103 | */ |
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'); |
84 | 106 | }, |
85 | 107 |
|
86 | 108 | /** |
87 | | - * Removes all listeners for a specific event type. |
| 109 | + * Removes a specific listener. |
88 | 110 | * |
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. |
90 | 113 | */ |
91 | | - removeAllListeners (eventType: string) { |
92 | | - KeyboardEventEmitter.removeAllListeners(eventType); |
| 114 | + removeListener(eventName: KeyboardEventName, callback: Function) { |
| 115 | + invariant(false, 'Dummy method used for documentation'); |
93 | 116 | }, |
94 | 117 |
|
95 | 118 | /** |
96 | | - * Removes a specific subscription. |
| 119 | + * Removes all listeners for a specific event type. |
97 | 120 | * |
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. |
99 | 122 | */ |
100 | | - removeSubscription (subscription: Object) { |
101 | | - KeyboardEventEmitter.removeSubscription(subscription); |
| 123 | + removeAllListeners(eventName: KeyboardEventName) { |
| 124 | + invariant(false, 'Dummy method used for documentation'); |
102 | 125 | }, |
103 | 126 |
|
104 | 127 | /** |
105 | 128 | * Dismisses the active keyboard and removes focus. |
106 | 129 | */ |
107 | | - dismiss () { |
108 | | - dismissKeyboard(); |
| 130 | + dismiss() { |
| 131 | + invariant(false, 'Dummy method used for documentation'); |
109 | 132 | } |
110 | | - |
111 | 133 | }; |
| 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