From 08eed9f093d759fadbaef86dcf54c801917b4602 Mon Sep 17 00:00:00 2001 From: Alan Lee Date: Thu, 20 Jun 2024 10:38:58 -0700 Subject: [PATCH] fix TextInput 'contextMenuHidden' prop (#45014) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/45014 `TextInput`'s `contextMenuHidden` prop isn't working working as expected. It should hide the context menu (copy/paste/...) that pops up from input text. Reference: [Android doc](https://developer.android.com/reference/android/widget/TextView#setCustomSelectionActionModeCallback(android.view.ActionMode.Callback)) > Returning false from `ActionMode.Callback.onCreateActionMode(ActionMode, android.view.Menu)` will prevent the action mode from being started. **Changelog:** [Android][Fixed] - TextInput's `contextMenuHidden` prop bug fix Reviewed By: javache Differential Revision: D58684366 fbshipit-source-id: 328c267ed0e896a78e114578e3a00adf41f2e095 --- .../react/views/textinput/ReactEditText.java | 8 +++ .../textinput/ReactTextInputManager.java | 51 ++++++++++--------- 2 files changed, 35 insertions(+), 24 deletions(-) diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactEditText.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactEditText.java index 081f2b80160..f47cd37d571 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactEditText.java +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactEditText.java @@ -119,6 +119,7 @@ public class ReactEditText extends AppCompatEditText { private int mFontWeight = UNSET; private int mFontStyle = UNSET; private boolean mAutoFocus = false; + private boolean mContextMenuHidden = false; private boolean mDidAttachToWindow = false; private @Nullable String mPlaceholder = null; @@ -192,6 +193,9 @@ public class ReactEditText extends AppCompatEditText { */ @Override public boolean onCreateActionMode(ActionMode mode, Menu menu) { + if (mContextMenuHidden) { + return false; + } menu.removeItem(android.R.id.pasteAsPlainText); return true; } @@ -1139,6 +1143,10 @@ public class ReactEditText extends AppCompatEditText { mAutoFocus = autoFocus; } + public void setContextMenuHidden(boolean contextMenuHidden) { + mContextMenuHidden = contextMenuHidden; + } + protected void applyTextAttributes() { // In general, the `getEffective*` functions return `Float.NaN` if the // property hasn't been set. diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactTextInputManager.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactTextInputManager.java index afa1030b3eb..616afd709ea 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactTextInputManager.java +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactTextInputManager.java @@ -904,14 +904,15 @@ public class ReactTextInputManager extends BaseViewManager