RN: Simplify `Keyboard`
Summary: Simplifies `Keyboard` by removing redundant methods and changing `addEventListener` to return an `EventSubscription`. Changelog: [General][Changed] - `Keyboard.addListener` now returns an `EventSubscription` object. [General][Removed] - Removed `Keyboard.removeListener`. Instead, use the `remove()` method on the object returned by `Keyboard.addListener`. [General][Removed] - `Keyboard` no longer inherits from `NativeEventEmitter`, so it no longer implements `removeAllListeners`, and `removeSubscription`. Reviewed By: milroc Differential Revision: D26163536 fbshipit-source-id: b4bd91627cd027a13fcba269a253823913eb7589
This commit is contained in:
Родитель
88a41f180c
Коммит
1049835b50
|
@ -12,7 +12,7 @@ import NativeEventEmitter from '../../EventEmitter/NativeEventEmitter';
|
|||
import LayoutAnimation from '../../LayoutAnimation/LayoutAnimation';
|
||||
import dismissKeyboard from '../../Utilities/dismissKeyboard';
|
||||
import NativeKeyboardObserver from './NativeKeyboardObserver';
|
||||
import type EmitterSubscription from '../../vendor/emitter/_EmitterSubscription';
|
||||
import {type EventSubscription} from '../../vendor/emitter/EventEmitter';
|
||||
|
||||
export type KeyboardEventName = $Keys<KeyboardEventDefinitions>;
|
||||
|
||||
|
@ -101,10 +101,10 @@ type KeyboardEventDefinitions = {
|
|||
*```
|
||||
*/
|
||||
|
||||
class Keyboard extends NativeEventEmitter<KeyboardEventDefinitions> {
|
||||
constructor() {
|
||||
super(NativeKeyboardObserver);
|
||||
}
|
||||
class Keyboard {
|
||||
_emitter: NativeEventEmitter<KeyboardEventDefinitions> = new NativeEventEmitter(
|
||||
NativeKeyboardObserver,
|
||||
);
|
||||
|
||||
/**
|
||||
* The `addListener` function connects a JavaScript function to an identified native
|
||||
|
@ -132,22 +132,9 @@ class Keyboard extends NativeEventEmitter<KeyboardEventDefinitions> {
|
|||
addListener<K: $Keys<KeyboardEventDefinitions>>(
|
||||
eventType: K,
|
||||
listener: (...$ElementType<KeyboardEventDefinitions, K>) => mixed,
|
||||
context: $FlowFixMe,
|
||||
): EmitterSubscription<KeyboardEventDefinitions, K> {
|
||||
return super.addListener(eventType, listener);
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes a specific listener.
|
||||
*
|
||||
* @param {string} eventName The `nativeEvent` is the string that identifies the event you're listening for.
|
||||
* @param {function} callback function to be called when the event fires.
|
||||
*/
|
||||
removeListener<K: $Keys<KeyboardEventDefinitions>>(
|
||||
eventType: K,
|
||||
listener: (...$ElementType<KeyboardEventDefinitions, K>) => mixed,
|
||||
): void {
|
||||
super.removeListener(eventType, listener);
|
||||
context?: mixed,
|
||||
): EventSubscription {
|
||||
return this._emitter.addListener(eventType, listener);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -156,7 +143,7 @@ class Keyboard extends NativeEventEmitter<KeyboardEventDefinitions> {
|
|||
* @param {string} eventType The native event string listeners are watching which will be removed.
|
||||
*/
|
||||
removeAllListeners<K: $Keys<KeyboardEventDefinitions>>(eventType: ?K): void {
|
||||
super.removeAllListeners(eventType);
|
||||
this._emitter.removeAllListeners(eventType);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -9,13 +9,10 @@
|
|||
* @emails oncall+react_native
|
||||
*/
|
||||
|
||||
const NativeModules = require('../../../BatchedBridge/NativeModules');
|
||||
const LayoutAnimation = require('../../../LayoutAnimation/LayoutAnimation');
|
||||
const dismissKeyboard = require('../../../Utilities/dismissKeyboard');
|
||||
const Keyboard = require('../Keyboard');
|
||||
|
||||
import NativeEventEmitter from '../../../EventEmitter/NativeEventEmitter';
|
||||
|
||||
jest.mock('../../../LayoutAnimation/LayoutAnimation');
|
||||
jest.mock('../../../Utilities/dismissKeyboard');
|
||||
|
||||
|
@ -24,16 +21,6 @@ describe('Keyboard', () => {
|
|||
jest.resetAllMocks();
|
||||
});
|
||||
|
||||
it('exposes KeyboardEventEmitter methods', () => {
|
||||
const KeyboardObserver = NativeModules.KeyboardObserver;
|
||||
const KeyboardEventEmitter = new NativeEventEmitter(KeyboardObserver);
|
||||
|
||||
// $FlowFixMe
|
||||
expect(Keyboard._subscriber).toBe(KeyboardEventEmitter._subscriber);
|
||||
// $FlowFixMe Cannot access private property
|
||||
expect(Keyboard._nativeModule).toBe(KeyboardEventEmitter._nativeModule);
|
||||
});
|
||||
|
||||
it('uses dismissKeyboard utility', () => {
|
||||
Keyboard.dismiss();
|
||||
expect(dismissKeyboard).toHaveBeenCalled();
|
||||
|
|
Загрузка…
Ссылка в новой задаче