Skip to content

Commit 3e8ad32

Browse files
committed
Handle "Escape" key for TextInput (microsoft#1044)
* wip * onKeyPress works * Minor cleanup * spaces not tabs
1 parent b758848 commit 3e8ad32

File tree

4 files changed

+28
-5
lines changed

4 files changed

+28
-5
lines changed

Libraries/Text/TextInput/RCTBackedTextInputDelegate.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ NS_ASSUME_NONNULL_BEGIN
3636
- (BOOL)textInputShouldHandleDeleteBackward:(id<RCTBackedTextInputViewProtocol>)sender; // Return `YES` to have the deleteBackward event handled normally. Return `NO` to disallow it and handle it yourself. TODO(OSS Candidate ISS#2710739)
3737
#if TARGET_OS_OSX // [TODO(macOS GH#774)
3838
- (BOOL)textInputShouldHandleDeleteForward:(id<RCTBackedTextInputViewProtocol>)sender; // Return `YES` to have the deleteForward event handled normally. Return `NO` to disallow it and handle it yourself.
39+
40+
- (void)textInputDidCancel; // Handle `Escape` key press.
3941
#endif // ]TODO(macOS GH#774)
4042

4143
@optional

Libraries/Text/TextInput/RCTBackedTextInputDelegateAdapter.m

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -187,17 +187,17 @@ - (BOOL)control:(NSControl *)control textShouldEndEditing:(NSText *)fieldEditor
187187

188188
- (BOOL)control:(NSControl *)control textView:(NSTextView *)fieldEditor doCommandBySelector:(SEL)commandSelector
189189
{
190+
id<RCTBackedTextInputDelegate> textInputDelegate = [_backedTextInputView textInputDelegate];
190191
BOOL commandHandled = NO;
191192
// enter/return
192193
if (commandSelector == @selector(insertNewline:) || commandSelector == @selector(insertNewlineIgnoringFieldEditor:)) {
193194
[self textFieldDidEndEditingOnExit];
194-
if ([[_backedTextInputView textInputDelegate] textInputShouldReturn]) {
195+
if ([textInputDelegate textInputShouldReturn]) {
195196
[[_backedTextInputView window] makeFirstResponder:nil];
196197
}
197198
commandHandled = YES;
198199
//backspace
199200
} else if (commandSelector == @selector(deleteBackward:)) {
200-
id<RCTBackedTextInputDelegate> textInputDelegate = [_backedTextInputView textInputDelegate];
201201
if (textInputDelegate != nil && ![textInputDelegate textInputShouldHandleDeleteBackward:_backedTextInputView]) {
202202
commandHandled = YES;
203203
} else {
@@ -214,7 +214,13 @@ - (BOOL)control:(NSControl *)control textView:(NSTextView *)fieldEditor doComman
214214
//paste
215215
} else if (commandSelector == @selector(paste:)) {
216216
_backedTextInputView.textWasPasted = YES;
217-
}
217+
//escape
218+
} else if (commandSelector == @selector(cancelOperation:)) {
219+
[textInputDelegate textInputDidCancel];
220+
[[_backedTextInputView window] makeFirstResponder:nil];
221+
commandHandled = YES;
222+
}
223+
218224
return commandHandled;
219225
}
220226

@@ -411,6 +417,12 @@ - (BOOL)textView:(NSTextView *)textView doCommandBySelector:(SEL)commandSelector
411417
//deleteForward
412418
} else if (commandSelector == @selector(deleteForward:)) {
413419
commandHandled = textInputDelegate != nil && ![textInputDelegate textInputShouldHandleDeleteForward:_backedTextInputView];
420+
//escape
421+
} else if (commandSelector == @selector(cancelOperation:)) {
422+
[textInputDelegate textInputDidCancel];
423+
[_backedTextInputView.window makeFirstResponder:nil];
424+
commandHandled = YES;
425+
414426
}
415427

416428
return commandHandled;

Libraries/Text/TextInput/RCTBaseTextInputView.m

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,15 @@ - (BOOL)textInputShouldHandleDeleteBackward:(__unused id)sender {
569569
- (BOOL)textInputShouldHandleDeleteForward:(__unused id)sender {
570570
return YES;
571571
}
572+
573+
- (void)textInputDidCancel {
574+
[_eventDispatcher sendTextEventWithType:RCTTextEventTypeKeyPress
575+
reactTag:self.reactTag
576+
text:nil
577+
key:@"Escape"
578+
eventCount:_nativeEventCount];
579+
[self textInputDidEndEditing];
580+
}
572581
#endif // ]TODO(macOS GH#774)
573582

574583
- (void)updateLocalData

packages/rn-tester/RNTesterPods.xcodeproj/project.pbxproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1869,7 +1869,7 @@
18691869
ENABLE_BITCODE = NO;
18701870
ENABLE_STRICT_OBJC_MSGSEND = YES;
18711871
ENABLE_TESTABILITY = YES;
1872-
"EXCLUDED_ARCHS[sdk=iphonesimulator*]" = "arm64 ";
1872+
"EXCLUDED_ARCHS[sdk=iphonesimulator*]" = "";
18731873
GCC_C_LANGUAGE_STANDARD = gnu11;
18741874
GCC_DYNAMIC_NO_PIC = NO;
18751875
GCC_NO_COMMON_BLOCKS = YES;
@@ -1953,7 +1953,7 @@
19531953
ENABLE_BITCODE = NO;
19541954
ENABLE_NS_ASSERTIONS = NO;
19551955
ENABLE_STRICT_OBJC_MSGSEND = YES;
1956-
"EXCLUDED_ARCHS[sdk=iphonesimulator*]" = "arm64 ";
1956+
"EXCLUDED_ARCHS[sdk=iphonesimulator*]" = "";
19571957
GCC_C_LANGUAGE_STANDARD = gnu11;
19581958
GCC_NO_COMMON_BLOCKS = YES;
19591959
GCC_TREAT_INCOMPATIBLE_POINTER_TYPE_WARNINGS_AS_ERRORS = YES;

0 commit comments

Comments
 (0)