Skip to content
Merged
Prev Previous commit
Next Next commit
Add hideAndroidSoftKeyboard to RN bridge
  • Loading branch information
fluiddot committed Dec 14, 2023
commit 63913e823a0abd0aca15f8e2a44cf06cb6f21e75
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package org.wordpress.mobile.ReactNativeGutenbergBridge;

import android.app.Activity;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.os.VibrationEffect;
import android.os.Vibrator;
import android.provider.Settings;
import android.view.View;
import android.view.inputmethod.InputMethodManager;

import androidx.annotation.Nullable;

Expand Down Expand Up @@ -550,4 +553,17 @@ private ConnectionStatusCallback requestConnectionStatusCallback(final Callback
}
};
}

@ReactMethod
public void hideAndroidSoftKeyboard() {
Activity currentActivity = mReactContext.getCurrentActivity();
if (currentActivity != null) {
View currentFocusedView = currentActivity.getCurrentFocus();
if (currentFocusedView != null) {
InputMethodManager imm =
(InputMethodManager) mReactContext.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(currentFocusedView.getWindowToken(), 0);
}
}
}
}
15 changes: 15 additions & 0 deletions packages/react-native-bridge/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,21 @@ export function sendEventToHost( eventName, properties ) {
);
}

/**
* Hides Android's soft keyboard.
*
* @return {void}
*/
export function hideAndroidSoftKeyboard() {
if ( isIOS ) {
/* eslint-disable-next-line no-console */
console.warn( 'hideAndroidSoftKeyboard is not supported on iOS' );
return;
}

RNReactNativeGutenbergBridge.hideAndroidSoftKeyboard();
}

/**
* Generate haptic feedback.
*/
Expand Down
1 change: 1 addition & 0 deletions test/native/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ jest.mock( '@wordpress/react-native-bridge', () => {
subscribeOnRedoPressed: jest.fn(),
useIsConnected: jest.fn( () => ( { isConnected: true } ) ),
editorDidMount: jest.fn(),
hideAndroidSoftKeyboard: jest.fn(),
editorDidAutosave: jest.fn(),
subscribeMediaUpload: jest.fn(),
subscribeMediaSave: jest.fn(),
Expand Down