Skip to content

Commit

Permalink
Align multi-line TextInput onSubmitEditing with Android (facebook#463)
Browse files Browse the repository at this point in the history
Setting `blurOnSubmit` to `true` means that pressing return will blur
the field and trigger the `onSubmitEditing` event instead of inserting
a newline into the field.

-- https://reactnative.dev/docs/textinput#bluronsubmit
  • Loading branch information
tido64 authored Jun 19, 2020
1 parent d68e94b commit 2803dc8
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 15 deletions.
13 changes: 3 additions & 10 deletions Libraries/Text/TextInput/RCTBackedTextInputDelegateAdapter.m
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,6 @@

static void *TextFieldSelectionObservingContext = &TextFieldSelectionObservingContext;

#if TARGET_OS_OSX // [TODO(macOS ISS#2323203)
BOOL RCTEventIsCommandEnterEvent(NSEvent *event) {
NSEventModifierFlags modifierFlags = event.modifierFlags & NSEventModifierFlagDeviceIndependentFlagsMask;
return (modifierFlags & NSEventModifierFlagCommand) == NSEventModifierFlagCommand && event.keyCode == 0x24;
}
#endif // ]TODO(macOS ISS#2323203)

@interface RCTBackedTextFieldDelegateAdapter ()
#if !TARGET_OS_OSX // [TODO(macOS ISS#2323203)
<UITextFieldDelegate>
Expand Down Expand Up @@ -360,12 +353,12 @@ - (BOOL)textView:(NSTextView *)textView doCommandBySelector:(SEL)commandSelector
{
BOOL commandHandled = NO;
id<RCTBackedTextInputDelegate> textInputDelegate = [_backedTextInputView textInputDelegate];
// cmd + enter/return
if (commandSelector == @selector(noop:) && RCTEventIsCommandEnterEvent(NSApp.currentEvent)) {
// enter/return
if ((commandSelector == @selector(insertNewline:) || commandSelector == @selector(insertNewlineIgnoringFieldEditor:))) {
if (textInputDelegate.textInputShouldReturn) {
[_backedTextInputView.window makeFirstResponder:nil];
commandHandled = YES;
}
commandHandled = YES;
//backspace
} else if (commandSelector == @selector(deleteBackward:)) {
commandHandled = textInputDelegate != nil && ![textInputDelegate textInputShouldHandleDeleteBackward:_backedTextInputView];
Expand Down
16 changes: 11 additions & 5 deletions Libraries/Text/TextInput/RCTBaseTextInputView.m
Original file line number Diff line number Diff line change
Expand Up @@ -407,11 +407,17 @@ - (BOOL)textInputShouldReturn
// `onSubmitEditing` is called when "Submit" button
// (the blue key on onscreen keyboard) did pressed
// (no connection to any specific "submitting" process).
[_eventDispatcher sendTextEventWithType:RCTTextEventTypeSubmit
reactTag:self.reactTag
text:self.backedTextInputView.attributedText.string
key:nil
eventCount:_nativeEventCount];
#if TARGET_OS_OSX // [TODO(macOS Candidate ISS#2710739)
if (_blurOnSubmit) {
#endif // ]TODO(macOS Candidate ISS#2710739)
[_eventDispatcher sendTextEventWithType:RCTTextEventTypeSubmit
reactTag:self.reactTag
text:self.backedTextInputView.attributedText.string
key:nil
eventCount:_nativeEventCount];
#if TARGET_OS_OSX // [TODO(macOS Candidate ISS#2710739)
}
#endif // ]TODO(macOS Candidate ISS#2710739)

return _blurOnSubmit;
}
Expand Down

0 comments on commit 2803dc8

Please sign in to comment.