Skip to content

Commit 2dd2529

Browse files
amhinsonfacebook-github-bot
authored andcommitted
Add option to hide context menu for TextInput facebook#17335
Summary: <!-- Thank you for sending the PR! We appreciate you spending the time to work on these changes. Help us understand your motivation by explaining why you decided to make this change. You can learn more about contributing to React Native here: http://facebook.github.io/react-native/docs/contributing.html Happy contributing! --> There is currently no way to disable to context menu that automatically appears over a TextInput. This is especially troublesome if you would like to disable the user from pasting text into certain fields. This PR adds a `contextMenuHidden` property to TextInput that will hide it. I'm not sure if testing is necessary here. I would be happy to investigate further on how this would be tested, if deemed necessary! facebook/react-native-website#95 <!-- Help reviewers and the release process by writing your own release notes **INTERNAL and MINOR tagged notes will not be included in the next version's final release notes.** CATEGORY [----------] TYPE [ CLI ] [-------------] LOCATION [ DOCS ] [ BREAKING ] [-------------] [ GENERAL ] [ BUGFIX ] [-{Component}-] [ INTERNAL ] [ ENHANCEMENT ] [ {File} ] [ IOS ] [ FEATURE ] [ {Directory} ] |-----------| [ ANDROID ] [ MINOR ] [ {Framework} ] - | {Message} | [----------] [-------------] [-------------] |-----------| [CATEGORY] [TYPE] [LOCATION] - MESSAGE EXAMPLES: [IOS] [BREAKING] [FlatList] - Change a thing that breaks other things [ANDROID] [BUGFIX] [TextInput] - Did a thing to TextInput [CLI] [FEATURE] [local-cli/info/info.js] - CLI easier to do things with [DOCS] [BUGFIX] [GettingStarted.md] - Accidentally a thing/word [GENERAL] [ENHANCEMENT] [Yoga] - Added new yoga thing/position [INTERNAL] [FEATURE] [./scripts] - Added thing to script that nobody will see --> [FEATURE][TextInput] - Added `contextMenuHidden` property Closes facebook#18125 Differential Revision: D7101888 Pulled By: hramos fbshipit-source-id: fe36603a3fbdcefbd644251a7ea894ac7e23e5b8
1 parent 247f32f commit 2dd2529

File tree

7 files changed

+42
-3
lines changed

7 files changed

+42
-3
lines changed

Libraries/Components/TextInput/TextInput.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -590,6 +590,10 @@ const TextInput = createReactClass({
590590
* This property is supported only for single-line TextInput component on iOS.
591591
*/
592592
caretHidden: PropTypes.bool,
593+
/*
594+
* If `true`, contextMenuHidden is hidden. The default value is `false`.
595+
*/
596+
contextMenuHidden: PropTypes.bool,
593597
/**
594598
* An optional identifier which links a custom InputAccessoryView to
595599
* this text input. The InputAccessoryView is rendered above the

Libraries/Text/TextInput/Multiline/RCTUITextView.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ NS_ASSUME_NONNULL_BEGIN
2323

2424
@property (nonatomic, weak) id<RCTBackedTextInputDelegate> textInputDelegate;
2525

26+
@property (nonatomic, assign) BOOL contextMenuHidden;
2627
@property (nonatomic, assign, readonly) BOOL textWasPasted;
2728
@property (nonatomic, copy, nullable) NSString *placeholder;
2829
@property (nonatomic, strong, nullable) UIColor *placeholderColor;

Libraries/Text/TextInput/Multiline/RCTUITextView.m

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,19 +58,19 @@ - (void)dealloc
5858
- (NSString *)accessibilityLabel
5959
{
6060
NSMutableString *accessibilityLabel = [NSMutableString new];
61-
61+
6262
NSString *superAccessibilityLabel = [super accessibilityLabel];
6363
if (superAccessibilityLabel.length > 0) {
6464
[accessibilityLabel appendString:superAccessibilityLabel];
6565
}
66-
66+
6767
if (self.placeholder.length > 0 && self.attributedText.string.length == 0) {
6868
if (accessibilityLabel.length > 0) {
6969
[accessibilityLabel appendString:@" "];
7070
}
7171
[accessibilityLabel appendString:self.placeholder];
7272
}
73-
73+
7474
return accessibilityLabel;
7575
}
7676

@@ -228,6 +228,17 @@ - (CGSize)fixedSizeThatFits:(CGSize)size
228228
return [_detachedTextView sizeThatFits:size];
229229
}
230230

231+
#pragma mark - Context Menu
232+
233+
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
234+
{
235+
if (_contextMenuHidden) {
236+
return NO;
237+
}
238+
239+
return [super canPerformAction:action withSender:sender];
240+
}
241+
231242
#pragma mark - Placeholder
232243

233244
- (void)invalidatePlaceholderVisibility

Libraries/Text/TextInput/RCTBaseTextInputViewManager.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ @implementation RCTBaseTextInputViewManager
3636

3737
RCT_REMAP_VIEW_PROPERTY(autoCapitalize, backedTextInputView.autocapitalizationType, UITextAutocapitalizationType)
3838
RCT_REMAP_VIEW_PROPERTY(autoCorrect, backedTextInputView.autocorrectionType, UITextAutocorrectionType)
39+
RCT_REMAP_VIEW_PROPERTY(contextMenuHidden, backedTextInputView.contextMenuHidden, BOOL)
3940
RCT_REMAP_VIEW_PROPERTY(editable, backedTextInputView.editable, BOOL)
4041
RCT_REMAP_VIEW_PROPERTY(enablesReturnKeyAutomatically, backedTextInputView.enablesReturnKeyAutomatically, BOOL)
4142
RCT_REMAP_VIEW_PROPERTY(keyboardAppearance, backedTextInputView.keyboardAppearance, UIKeyboardAppearance)

Libraries/Text/TextInput/Singleline/RCTUITextField.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ NS_ASSUME_NONNULL_BEGIN
2121
@property (nonatomic, weak) id<RCTBackedTextInputDelegate> textInputDelegate;
2222

2323
@property (nonatomic, assign) BOOL caretHidden;
24+
@property (nonatomic, assign) BOOL contextMenuHidden;
2425
@property (nonatomic, assign, readonly) BOOL textWasPasted;
2526
@property (nonatomic, strong, nullable) UIColor *placeholderColor;
2627
@property (nonatomic, assign) UIEdgeInsets textContainerInset;

Libraries/Text/TextInput/Singleline/RCTUITextField.m

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,17 @@ - (void)setEditable:(BOOL)editable
8585
self.enabled = editable;
8686
}
8787

88+
#pragma mark - Context Menu
89+
90+
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
91+
{
92+
if (_contextMenuHidden) {
93+
return NO;
94+
}
95+
96+
return [super canPerformAction:action withSender:sender];
97+
}
98+
8899
#pragma mark - Caret Manipulation
89100

90101
- (CGRect)caretRectForPosition:(UITextPosition *)position

ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactTextInputManager.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,16 @@ public void setCaretHidden(ReactEditText view, boolean caretHidden) {
378378
view.setCursorVisible(!caretHidden);
379379
}
380380

381+
@ReactProp(name = "contextMenuHidden", defaultBoolean = false)
382+
public void setContextMenuHidden(ReactEditText view, boolean contextMenuHidden) {
383+
final boolean _contextMenuHidden = contextMenuHidden;
384+
view.setOnLongClickListener(new View.OnLongClickListener() {
385+
public boolean onLongClick(View v) {
386+
return _contextMenuHidden;
387+
};
388+
});
389+
}
390+
381391
@ReactProp(name = "selectTextOnFocus", defaultBoolean = false)
382392
public void setSelectTextOnFocus(ReactEditText view, boolean selectTextOnFocus) {
383393
view.setSelectAllOnFocus(selectTextOnFocus);

0 commit comments

Comments
 (0)