Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: allow to pass additional params to .dismiss() unintentionally #685

Merged
merged 3 commits into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion FabricExample/src/components/KeyboardAnimation/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export default function KeyboardAnimation({
<TouchableOpacity
activeOpacity={1}
style={styles.container}
onPress={() => KeyboardController.dismiss()}
onPress={KeyboardController.dismiss}
>
<>
<View>
Expand Down
2 changes: 1 addition & 1 deletion FabricExample/src/screens/Examples/Close/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ function CloseScreen() {
<Button
testID="close_keyboard_button"
title="Close keyboard"
onPress={() => KeyboardController.dismiss()}
onPress={KeyboardController.dismiss}
/>
<TextInput
placeholder="Touch to open the keyboard..."
Expand Down
2 changes: 1 addition & 1 deletion example/src/components/KeyboardAnimation/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export default function KeyboardAnimation({
<TouchableOpacity
activeOpacity={1}
style={styles.container}
onPress={() => KeyboardController.dismiss()}
onPress={KeyboardController.dismiss}
>
<>
<View>
Expand Down
2 changes: 1 addition & 1 deletion example/src/screens/Examples/Close/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ function CloseScreen() {
<Button
testID="close_keyboard_button"
title="Close keyboard"
onPress={() => KeyboardController.dismiss()}
onPress={KeyboardController.dismiss}
/>
<TextInput
placeholder="Touch to open the keyboard..."
Expand Down
14 changes: 11 additions & 3 deletions src/bindings.native.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { NativeEventEmitter, Platform } from "react-native";
import type {
FocusedInputEventsModule,
KeyboardControllerModule,
KeyboardControllerNativeModule,
KeyboardControllerProps,
KeyboardEventsModule,
KeyboardGestureAreaProps,
Expand All @@ -19,7 +20,7 @@ const LINKING_ERROR =
const RCTKeyboardController =
require("./specs/NativeKeyboardController").default;

export const KeyboardController = (
export const KeyboardControllerNative = (
RCTKeyboardController
? RCTKeyboardController
: new Proxy(
Expand All @@ -30,15 +31,22 @@ export const KeyboardController = (
},
},
)
) as KeyboardControllerModule;
) as KeyboardControllerNativeModule;

const KEYBOARD_CONTROLLER_NAMESPACE = "KeyboardController::";
const eventEmitter = new NativeEventEmitter(KeyboardController);
const eventEmitter = new NativeEventEmitter(KeyboardControllerNative);

export const KeyboardEvents: KeyboardEventsModule = {
addListener: (name, cb) =>
eventEmitter.addListener(KEYBOARD_CONTROLLER_NAMESPACE + name, cb),
};
export const KeyboardController: KeyboardControllerModule = {
setDefaultMode: KeyboardControllerNative.setDefaultMode,
setInputMode: KeyboardControllerNative.setInputMode,
setFocusTo: KeyboardControllerNative.setFocusTo,
// additional function is needed because of this https://github.com/kirillzyusko/react-native-keyboard-controller/issues/684
dismiss: () => KeyboardControllerNative.dismiss(),
};
/**
* This API is not documented, it's for internal usage only (for now), and is a subject to potential breaking changes in future.
* Use it with cautious.
Expand Down
2 changes: 0 additions & 2 deletions src/bindings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ export const KeyboardController: KeyboardControllerModule = {
setInputMode: NOOP,
dismiss: NOOP,
setFocusTo: NOOP,
addListener: NOOP,
removeListeners: NOOP,
};
export const KeyboardEvents: KeyboardEventsModule = {
addListener: () => ({ remove: NOOP } as EmitterSubscription),
Expand Down
10 changes: 3 additions & 7 deletions src/components/KeyboardToolbar/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,6 @@ const KEYBOARD_TOOLBAR_HEIGHT = 42;
const DEFAULT_OPACITY: HEX = "FF";
const offset = { closed: KEYBOARD_TOOLBAR_HEIGHT };

const dismissKeyboard = () => KeyboardController.dismiss();
const goToNextField = () => KeyboardController.setFocusTo("next");
const goToPrevField = () => KeyboardController.setFocusTo("prev");

/**
* `KeyboardToolbar` is a component that is shown above the keyboard with `Prev`/`Next` and
* `Done` buttons.
Expand Down Expand Up @@ -122,7 +118,7 @@ const KeyboardToolbar: React.FC<KeyboardToolbarProps> = ({
onNextCallback?.(event);

if (!event.isDefaultPrevented()) {
goToNextField();
KeyboardController.setFocusTo("next");
}
},
[onNextCallback],
Expand All @@ -132,7 +128,7 @@ const KeyboardToolbar: React.FC<KeyboardToolbarProps> = ({
onPrevCallback?.(event);

if (!event.isDefaultPrevented()) {
goToPrevField();
KeyboardController.setFocusTo("prev");
}
},
[onPrevCallback],
Expand All @@ -142,7 +138,7 @@ const KeyboardToolbar: React.FC<KeyboardToolbarProps> = ({
onDoneCallback?.(event);

if (!event.isDefaultPrevented()) {
dismissKeyboard();
KeyboardController.dismiss();
}
},
[onDoneCallback],
Expand Down
4 changes: 3 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,12 @@ export type KeyboardControllerModule = {
// all platforms
dismiss: () => void;
setFocusTo: (direction: Direction) => void;
};
export type KeyboardControllerNativeModule = {
// native event module stuff
addListener: (eventName: string) => void;
removeListeners: (count: number) => void;
};
} & KeyboardControllerModule;

// Event module declarations
export type KeyboardControllerEvents =
Expand Down