Skip to content

Commit 441744a

Browse files
joevilchesfacebook-github-bot
authored andcommitted
Implement view finding by native id (facebook#49581)
Summary: Pull Request resolved: facebook#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
1 parent 0aa8af9 commit 441744a

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
#import <UIKit/UIKit.h>
9+
10+
NS_ASSUME_NONNULL_BEGIN
11+
12+
@interface RCTViewFinder : NSObject
13+
14+
+ (UIView *)findView:(UIView *)root withNativeId:(NSString *)nativeId;
15+
16+
@end
17+
18+
NS_ASSUME_NONNULL_END
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
#import "RCTViewFinder.h"
9+
#include <React/RCTViewComponentView.h>
10+
11+
@implementation RCTViewFinder
12+
13+
+ (UIView *)findView:(UIView *)root withNativeId:(NSString *)nativeId
14+
{
15+
if (!nativeId) {
16+
return nil;
17+
}
18+
19+
if ([root isKindOfClass:[RCTViewComponentView class]] &&
20+
[nativeId isEqualToString:((RCTViewComponentView *)root).nativeId]) {
21+
return root;
22+
}
23+
24+
for (UIView *subview in root.subviews) {
25+
UIView *result = [RCTViewFinder findView:subview withNativeId:nativeId];
26+
if (result) {
27+
return result;
28+
}
29+
}
30+
31+
return nil;
32+
}
33+
34+
@end

0 commit comments

Comments
 (0)