Skip to content

Commit 0000b9c

Browse files
committed
PR feedback + std::any_of
1 parent 84f6f50 commit 0000b9c

File tree

1 file changed

+24
-19
lines changed

1 file changed

+24
-19
lines changed

packages/react-native/React/Fabric/Mounting/ComponentViews/TextInput/RCTTextInputComponentView.mm

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@
3939
static const CGFloat kSingleLineKeyboardBottomOffset = 15.0;
4040
#endif // [macOS]
4141

42+
#if TARGET_OS_OSX // [macOS
43+
static const NSString *kEscapeKeyCode = @"\x1B";
44+
#endif // macOS]
45+
46+
4247
using namespace facebook::react;
4348

4449
@interface RCTTextInputComponentView () <RCTBackedTextInputDelegate, RCTTextInputViewProtocol>
@@ -144,9 +149,7 @@ - (void)didMoveToWindow
144149
[_backedTextInputView becomeFirstResponder];
145150
#else // [macOS
146151
NSWindow *window = [_backedTextInputView window];
147-
if (window) {
148-
[window makeFirstResponder:_backedTextInputView.responder];
149-
}
152+
[window makeFirstResponder:_backedTextInputView.responder];
150153
#endif // macOS]
151154
[self scrollCursorIntoView];
152155
}
@@ -615,20 +618,23 @@ - (void)submitOnKeyDownIfNeeded:(nonnull NSEvent *)event
615618
&& ![keyEvent[@"functionKey"] boolValue]; // Default clearTextOnSubmit key
616619
} else {
617620
NSString *keyValue = keyEvent[@"key"];
618-
NSUInteger keyValueLength = [keyValue lengthOfBytesUsingEncoding:NSUTF8StringEncoding];
619-
std::string key = std::string([keyValue UTF8String], keyValueLength);
620-
for (auto const &submitKeyEvent : props.traits.submitKeyEvents) {
621-
if (
622-
submitKeyEvent.key == key &&
623-
submitKeyEvent.altKey == [keyEvent[@"altKey"] boolValue] &&
624-
submitKeyEvent.shiftKey == [keyEvent[@"shiftKey"] boolValue] &&
625-
submitKeyEvent.ctrlKey == [keyEvent[@"ctrlKey"] boolValue] &&
626-
submitKeyEvent.metaKey == [keyEvent[@"metaKey"] boolValue] &&
627-
submitKeyEvent.functionKey == [keyEvent[@"functionKey"] boolValue]
628-
) {
629-
shouldSubmit = YES;
630-
break;
631-
}
621+
const char *keyCString = [keyValue UTF8String];
622+
if (keyCString != nullptr) {
623+
std::string_view key(keyCString);
624+
const bool altKey = [keyEvent[@"altKey"] boolValue];
625+
const bool shiftKey = [keyEvent[@"shiftKey"] boolValue];
626+
const bool ctrlKey = [keyEvent[@"ctrlKey"] boolValue];
627+
const bool metaKey = [keyEvent[@"metaKey"] boolValue];
628+
const bool functionKey = [keyEvent[@"functionKey"] boolValue];
629+
630+
shouldSubmit = std::any_of(
631+
props.traits.submitKeyEvents.begin(),
632+
props.traits.submitKeyEvents.end(),
633+
[&](auto const &submitKeyEvent) {
634+
return submitKeyEvent.key == key && submitKeyEvent.altKey == altKey &&
635+
submitKeyEvent.shiftKey == shiftKey && submitKeyEvent.ctrlKey == ctrlKey &&
636+
submitKeyEvent.metaKey == metaKey && submitKeyEvent.functionKey == functionKey;
637+
});
632638
}
633639
}
634640

@@ -648,10 +654,9 @@ - (void)submitOnKeyDownIfNeeded:(nonnull NSEvent *)event
648654
- (void)textInputDidCancel
649655
{
650656
if (_eventEmitter) {
651-
652657
auto const &textInputEventEmitter = *std::static_pointer_cast<TextInputEventEmitter const>(_eventEmitter);
653658
textInputEventEmitter.onKeyPress({
654-
.text = RCTStringFromNSString(@"\x1B"), // Escape key
659+
.text = RCTStringFromNSString(kEscapeKeyCode),
655660
.eventCount = static_cast<int>(_mostRecentEventCount),
656661
});
657662
}

0 commit comments

Comments
 (0)