diff --git a/packages/react-native/Libraries/Components/Keyboard/Keyboard.js b/packages/react-native/Libraries/Components/Keyboard/Keyboard.js index 40bfa1e121ac..1a37901c13a2 100644 --- a/packages/react-native/Libraries/Components/Keyboard/Keyboard.js +++ b/packages/react-native/Libraries/Components/Keyboard/Keyboard.js @@ -104,7 +104,7 @@ class KeyboardImpl { listener: (...KeyboardEventDefinitions[K]) => unknown, context?: unknown, ): EventSubscription { - return this._emitter.addListener(eventType, listener); + return this._emitter.addListener(eventType, listener, context); } /** diff --git a/packages/react-native/Libraries/Components/Keyboard/__tests__/Keyboard-test.js b/packages/react-native/Libraries/Components/Keyboard/__tests__/Keyboard-test.js index a84c0becf891..1153673b2635 100644 --- a/packages/react-native/Libraries/Components/Keyboard/__tests__/Keyboard-test.js +++ b/packages/react-native/Libraries/Components/Keyboard/__tests__/Keyboard-test.js @@ -8,6 +8,8 @@ * @format */ +const RCTDeviceEventEmitter = + require('../../../EventEmitter/RCTDeviceEventEmitter').default; const LayoutAnimation = require('../../../LayoutAnimation/LayoutAnimation').default; const dismissKeyboard = require('../../../Utilities/dismissKeyboard').default; @@ -26,6 +28,25 @@ describe('Keyboard', () => { expect(dismissKeyboard).toHaveBeenCalled(); }); + it('invokes listeners with the specified context', () => { + const context = {}; + let that; + const listener = jest.fn(function (this: unknown, _event: unknown) { + that = this; + }); + const subscription = Keyboard.addListener( + 'keyboardDidShow', + listener, + context, + ); + + RCTDeviceEventEmitter.emit('keyboardDidShow', {}); + + expect(listener).toHaveBeenCalled(); + expect(that).toBe(context); + subscription.remove(); + }); + describe('scheduling layout animation', () => { const scheduleLayoutAnimation = ( duration: null | number,