Skip to content

Commit 3eb78be

Browse files
authored
fix: allow to pass additional params to .dismiss() unintentionally (#685)
## 📜 Description Allow to pass additional params to `KeyboardController.dismiss()` method unintentionally. ## 💡 Motivation and Context When we pass params unintentionally like `onPress={KeyboardController.dismiss}` then we will try to serialize big JS object to the native side, which eventually crash the application. To avoid this I decided to wrap `dismiss` in additional function and always send empty params to a native function. Closes #684 ## 📢 Changelog <!-- High level overview of important changes --> <!-- For example: fixed status bar manipulation; added new types declarations; --> <!-- If your changes don't affect one of platform/language below - then remove this platform/language --> ### JS - removed undocumented `addListener`/`removeListener` methods from `KeyboardController`; - allow to pass any params to `dismiss`; - removed unnecessary functions nesting in `KeyboardToolbar`; ## 🤔 How Has This Been Tested? Tested on CI. ## 📝 Checklist - [x] CI successfully passed - [x] I added new mocks and corresponding unit-tests if library API was changed
1 parent 82628d5 commit 3eb78be

File tree

8 files changed

+21
-17
lines changed

8 files changed

+21
-17
lines changed

FabricExample/src/components/KeyboardAnimation/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ export default function KeyboardAnimation({
6262
<TouchableOpacity
6363
activeOpacity={1}
6464
style={styles.container}
65-
onPress={() => KeyboardController.dismiss()}
65+
onPress={KeyboardController.dismiss}
6666
>
6767
<>
6868
<View>

FabricExample/src/screens/Examples/Close/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ function CloseScreen() {
77
<Button
88
testID="close_keyboard_button"
99
title="Close keyboard"
10-
onPress={() => KeyboardController.dismiss()}
10+
onPress={KeyboardController.dismiss}
1111
/>
1212
<TextInput
1313
placeholder="Touch to open the keyboard..."

example/src/components/KeyboardAnimation/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ export default function KeyboardAnimation({
6262
<TouchableOpacity
6363
activeOpacity={1}
6464
style={styles.container}
65-
onPress={() => KeyboardController.dismiss()}
65+
onPress={KeyboardController.dismiss}
6666
>
6767
<>
6868
<View>

example/src/screens/Examples/Close/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ function CloseScreen() {
77
<Button
88
testID="close_keyboard_button"
99
title="Close keyboard"
10-
onPress={() => KeyboardController.dismiss()}
10+
onPress={KeyboardController.dismiss}
1111
/>
1212
<TextInput
1313
placeholder="Touch to open the keyboard..."

src/bindings.native.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { NativeEventEmitter, Platform } from "react-native";
33
import type {
44
FocusedInputEventsModule,
55
KeyboardControllerModule,
6+
KeyboardControllerNativeModule,
67
KeyboardControllerProps,
78
KeyboardEventsModule,
89
KeyboardGestureAreaProps,
@@ -19,7 +20,7 @@ const LINKING_ERROR =
1920
const RCTKeyboardController =
2021
require("./specs/NativeKeyboardController").default;
2122

22-
export const KeyboardController = (
23+
export const KeyboardControllerNative = (
2324
RCTKeyboardController
2425
? RCTKeyboardController
2526
: new Proxy(
@@ -30,15 +31,22 @@ export const KeyboardController = (
3031
},
3132
},
3233
)
33-
) as KeyboardControllerModule;
34+
) as KeyboardControllerNativeModule;
3435

3536
const KEYBOARD_CONTROLLER_NAMESPACE = "KeyboardController::";
36-
const eventEmitter = new NativeEventEmitter(KeyboardController);
37+
const eventEmitter = new NativeEventEmitter(KeyboardControllerNative);
3738

3839
export const KeyboardEvents: KeyboardEventsModule = {
3940
addListener: (name, cb) =>
4041
eventEmitter.addListener(KEYBOARD_CONTROLLER_NAMESPACE + name, cb),
4142
};
43+
export const KeyboardController: KeyboardControllerModule = {
44+
setDefaultMode: KeyboardControllerNative.setDefaultMode,
45+
setInputMode: KeyboardControllerNative.setInputMode,
46+
setFocusTo: KeyboardControllerNative.setFocusTo,
47+
// additional function is needed because of this https://github.com/kirillzyusko/react-native-keyboard-controller/issues/684
48+
dismiss: () => KeyboardControllerNative.dismiss(),
49+
};
4250
/**
4351
* This API is not documented, it's for internal usage only (for now), and is a subject to potential breaking changes in future.
4452
* Use it with cautious.

src/bindings.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ export const KeyboardController: KeyboardControllerModule = {
1818
setInputMode: NOOP,
1919
dismiss: NOOP,
2020
setFocusTo: NOOP,
21-
addListener: NOOP,
22-
removeListeners: NOOP,
2321
};
2422
export const KeyboardEvents: KeyboardEventsModule = {
2523
addListener: () => ({ remove: NOOP } as EmitterSubscription),

src/components/KeyboardToolbar/index.tsx

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,6 @@ const KEYBOARD_TOOLBAR_HEIGHT = 42;
6464
const DEFAULT_OPACITY: HEX = "FF";
6565
const offset = { closed: KEYBOARD_TOOLBAR_HEIGHT };
6666

67-
const dismissKeyboard = () => KeyboardController.dismiss();
68-
const goToNextField = () => KeyboardController.setFocusTo("next");
69-
const goToPrevField = () => KeyboardController.setFocusTo("prev");
70-
7167
/**
7268
* `KeyboardToolbar` is a component that is shown above the keyboard with `Prev`/`Next` and
7369
* `Done` buttons.
@@ -122,7 +118,7 @@ const KeyboardToolbar: React.FC<KeyboardToolbarProps> = ({
122118
onNextCallback?.(event);
123119

124120
if (!event.isDefaultPrevented()) {
125-
goToNextField();
121+
KeyboardController.setFocusTo("next");
126122
}
127123
},
128124
[onNextCallback],
@@ -132,7 +128,7 @@ const KeyboardToolbar: React.FC<KeyboardToolbarProps> = ({
132128
onPrevCallback?.(event);
133129

134130
if (!event.isDefaultPrevented()) {
135-
goToPrevField();
131+
KeyboardController.setFocusTo("prev");
136132
}
137133
},
138134
[onPrevCallback],
@@ -142,7 +138,7 @@ const KeyboardToolbar: React.FC<KeyboardToolbarProps> = ({
142138
onDoneCallback?.(event);
143139

144140
if (!event.isDefaultPrevented()) {
145-
dismissKeyboard();
141+
KeyboardController.dismiss();
146142
}
147143
},
148144
[onDoneCallback],

src/types.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,10 +121,12 @@ export type KeyboardControllerModule = {
121121
// all platforms
122122
dismiss: () => void;
123123
setFocusTo: (direction: Direction) => void;
124+
};
125+
export type KeyboardControllerNativeModule = {
124126
// native event module stuff
125127
addListener: (eventName: string) => void;
126128
removeListeners: (count: number) => void;
127-
};
129+
} & KeyboardControllerModule;
128130

129131
// Event module declarations
130132
export type KeyboardControllerEvents =

0 commit comments

Comments
 (0)