Skip to content

Commit

Permalink
Enable searching through a view's parents for a React tag to call blu…
Browse files Browse the repository at this point in the history
…r/focus on (#2325)

Co-authored-by: Adam Gleitman <[email protected]>
  • Loading branch information
amgleitman and Adam Gleitman authored Jan 14, 2025
1 parent 14f6ccc commit 1f83f4e
Showing 1 changed file with 21 additions and 4 deletions.
25 changes: 21 additions & 4 deletions packages/react-native/React/Base/RCTRootView.m
Original file line number Diff line number Diff line change
Expand Up @@ -455,19 +455,36 @@ - (void)viewDidMoveToWindow {
object:window];
}

+ (NSNumber *)reactTagClosestToView:(RCTPlatformView *)view {
// The first responder may not necessarily have a React tag. For example, if we're focused on
// a multiline <TextInput>, the RN component whose blur/focus events we need to touch is an
// RCTMultilineTextInputView, but the first responder will be the underlying RCTUITextView.
for (RCTPlatformView *testView = view; testView != nil; testView = [testView superview]) {
NSNumber *reactTag = [testView reactTag];
if (reactTag != nil) {
return reactTag;
}
}
return nil;
}

- (void)containingWindowDidBecomeKey {
NSResponder *firstResponder = [[self window] firstResponder];
if ([firstResponder isKindOfClass:[RCTPlatformView class]]) {
NSNumber *reactTag = [(RCTPlatformView *)firstResponder reactTag];
[[[self bridge] eventDispatcher] sendEvent:[RCTFocusChangeEvent focusEventWithReactTag:reactTag]];
NSNumber *reactTag = [RCTRootView reactTagClosestToView:(RCTPlatformView *)firstResponder];
if (reactTag != nil) {
[[[self bridge] eventDispatcher] sendEvent:[RCTFocusChangeEvent focusEventWithReactTag:reactTag]];
}
}
}

- (void)containingWindowDidResignKey {
NSResponder *firstResponder = [[self window] firstResponder];
if ([firstResponder isKindOfClass:[RCTPlatformView class]]) {
NSNumber *reactTag = [(RCTPlatformView *)firstResponder reactTag];
[[[self bridge] eventDispatcher] sendEvent:[RCTFocusChangeEvent blurEventWithReactTag:reactTag]];
NSNumber *reactTag = [RCTRootView reactTagClosestToView:(RCTPlatformView *)firstResponder];
if (reactTag != nil) {
[[[self bridge] eventDispatcher] sendEvent:[RCTFocusChangeEvent blurEventWithReactTag:reactTag]];
}
}
}
#endif // macOS]
Expand Down

0 comments on commit 1f83f4e

Please sign in to comment.