Skip to content

Commit 1f83f4e

Browse files
amgleitmanAdam Gleitman
andauthored
Enable searching through a view's parents for a React tag to call blur/focus on (#2325)
Co-authored-by: Adam Gleitman <[email protected]>
1 parent 14f6ccc commit 1f83f4e

File tree

1 file changed

+21
-4
lines changed

1 file changed

+21
-4
lines changed

packages/react-native/React/Base/RCTRootView.m

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -455,19 +455,36 @@ - (void)viewDidMoveToWindow {
455455
object:window];
456456
}
457457

458+
+ (NSNumber *)reactTagClosestToView:(RCTPlatformView *)view {
459+
// The first responder may not necessarily have a React tag. For example, if we're focused on
460+
// a multiline <TextInput>, the RN component whose blur/focus events we need to touch is an
461+
// RCTMultilineTextInputView, but the first responder will be the underlying RCTUITextView.
462+
for (RCTPlatformView *testView = view; testView != nil; testView = [testView superview]) {
463+
NSNumber *reactTag = [testView reactTag];
464+
if (reactTag != nil) {
465+
return reactTag;
466+
}
467+
}
468+
return nil;
469+
}
470+
458471
- (void)containingWindowDidBecomeKey {
459472
NSResponder *firstResponder = [[self window] firstResponder];
460473
if ([firstResponder isKindOfClass:[RCTPlatformView class]]) {
461-
NSNumber *reactTag = [(RCTPlatformView *)firstResponder reactTag];
462-
[[[self bridge] eventDispatcher] sendEvent:[RCTFocusChangeEvent focusEventWithReactTag:reactTag]];
474+
NSNumber *reactTag = [RCTRootView reactTagClosestToView:(RCTPlatformView *)firstResponder];
475+
if (reactTag != nil) {
476+
[[[self bridge] eventDispatcher] sendEvent:[RCTFocusChangeEvent focusEventWithReactTag:reactTag]];
477+
}
463478
}
464479
}
465480

466481
- (void)containingWindowDidResignKey {
467482
NSResponder *firstResponder = [[self window] firstResponder];
468483
if ([firstResponder isKindOfClass:[RCTPlatformView class]]) {
469-
NSNumber *reactTag = [(RCTPlatformView *)firstResponder reactTag];
470-
[[[self bridge] eventDispatcher] sendEvent:[RCTFocusChangeEvent blurEventWithReactTag:reactTag]];
484+
NSNumber *reactTag = [RCTRootView reactTagClosestToView:(RCTPlatformView *)firstResponder];
485+
if (reactTag != nil) {
486+
[[[self bridge] eventDispatcher] sendEvent:[RCTFocusChangeEvent blurEventWithReactTag:reactTag]];
487+
}
471488
}
472489
}
473490
#endif // macOS]

0 commit comments

Comments
 (0)