Add a nativeID prop to allow native code to reference react managed views

Reviewed By: sahrens

Differential Revision: D4786713

fbshipit-source-id: af9cef0737c010b429d52d00181c00bd81f13f5b
This commit is contained in:
Andrew Y. Chen
2017-04-07 11:47:35 -07:00
committed by Facebook Github Bot
parent e154117f37
commit 909af08f24
17 changed files with 218 additions and 4 deletions

View File

@@ -41,6 +41,7 @@ public abstract class BaseViewManager<T extends View, C extends LayoutShadowNode
* Used to locate views in end-to-end (UI) tests.
*/
public static final String PROP_TEST_ID = "testID";
public static final String PROP_NATIVE_ID = "nativeID";
private static MatrixMathHelper.MatrixDecompositionContext sMatrixDecompositionContext =
new MatrixMathHelper.MatrixDecompositionContext();
@@ -92,6 +93,11 @@ public abstract class BaseViewManager<T extends View, C extends LayoutShadowNode
view.setTag(testId);
}
@ReactProp(name = PROP_NATIVE_ID)
public void setNativeId(T view, String nativeId) {
view.setTag(R.id.view_tag_native_id, nativeId);
}
@ReactProp(name = PROP_ACCESSIBILITY_LABEL)
public void setAccessibilityLabel(T view, String accessibilityLabel) {
view.setContentDescription(accessibilityLabel);

View File

@@ -0,0 +1,13 @@
include_defs("//ReactAndroid/DEFS")
android_library(
name = "util",
srcs = glob(["*.java"]),
visibility = [
"PUBLIC",
],
deps = [
react_native_dep("third-party/java/jsr-305:jsr-305"),
react_native_target("res:uimanager"),
],
)

View File

@@ -0,0 +1,38 @@
// Copyright 2004-present Facebook. All Rights Reserved.
package com.facebook.react.uimanager.util;
import javax.annotation.Nullable;
import android.view.View;
import android.view.ViewGroup;
import com.facebook.react.R;
/**
* Finds views in React Native view hierarchies
*/
public class ReactFindViewUtil {
/**
* Finds a view that is tagged with {@param nativeId} as its `nativeID` prop
*/
public static @Nullable View findViewByNativeId(View view, String nativeId) {
Object tag = view.getTag(R.id.view_tag_native_id);
if (tag instanceof String && tag.equals(nativeId)) {
return view;
}
if (view instanceof ViewGroup) {
ViewGroup viewGroup = (ViewGroup) view;
for (int i = 0; i < viewGroup.getChildCount(); i++) {
View v = findViewByNativeId(viewGroup.getChildAt(i), nativeId);
if (v != null) {
return v;
}
}
}
return null;
}
}