From 441744a0eb46ca732ed170eeb49b563cfe43a8dd Mon Sep 17 00:00:00 2001 From: Joe Vilches Date: Fri, 21 Feb 2025 14:10:01 -0800 Subject: [PATCH] Implement view finding by native id (#49581) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/49581 I need to be able to find a View with a specific nativeId as part of my implementation of accessibility ordered children. This already exists in Android in `ReactFindViewUtil.kt`. Not much to this implementation. Recursive tree searching. I do not think perf is a big deal here but if we want to optimize this we could implement some nativeId registry and try and get the UIView * from that at the expense of storing that map somewhere. Changelog: [Internal] Reviewed By: vincentriemer Differential Revision: D69868430 fbshipit-source-id: b3648a8dca351bed50534cac2144d7e8ea0a207f --- .../React/Fabric/Utils/RCTViewFinder.h | 18 ++++++++++ .../React/Fabric/Utils/RCTViewFinder.mm | 34 +++++++++++++++++++ 2 files changed, 52 insertions(+) create mode 100644 packages/react-native/React/Fabric/Utils/RCTViewFinder.h create mode 100644 packages/react-native/React/Fabric/Utils/RCTViewFinder.mm diff --git a/packages/react-native/React/Fabric/Utils/RCTViewFinder.h b/packages/react-native/React/Fabric/Utils/RCTViewFinder.h new file mode 100644 index 00000000000..cbeac5b9fbe --- /dev/null +++ b/packages/react-native/React/Fabric/Utils/RCTViewFinder.h @@ -0,0 +1,18 @@ +/* + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +#import + +NS_ASSUME_NONNULL_BEGIN + +@interface RCTViewFinder : NSObject + ++ (UIView *)findView:(UIView *)root withNativeId:(NSString *)nativeId; + +@end + +NS_ASSUME_NONNULL_END diff --git a/packages/react-native/React/Fabric/Utils/RCTViewFinder.mm b/packages/react-native/React/Fabric/Utils/RCTViewFinder.mm new file mode 100644 index 00000000000..e6406846ce7 --- /dev/null +++ b/packages/react-native/React/Fabric/Utils/RCTViewFinder.mm @@ -0,0 +1,34 @@ +/* + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +#import "RCTViewFinder.h" +#include + +@implementation RCTViewFinder + ++ (UIView *)findView:(UIView *)root withNativeId:(NSString *)nativeId +{ + if (!nativeId) { + return nil; + } + + if ([root isKindOfClass:[RCTViewComponentView class]] && + [nativeId isEqualToString:((RCTViewComponentView *)root).nativeId]) { + return root; + } + + for (UIView *subview in root.subviews) { + UIView *result = [RCTViewFinder findView:subview withNativeId:nativeId]; + if (result) { + return result; + } + } + + return nil; +} + +@end