diff --git a/ReactAndroid/src/main/java/com/facebook/react/uimanager/ReactShadowNode.java b/ReactAndroid/src/main/java/com/facebook/react/uimanager/ReactShadowNode.java index 3c8d923699135b..a3ce58bf3b5c6e 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/uimanager/ReactShadowNode.java +++ b/ReactAndroid/src/main/java/com/facebook/react/uimanager/ReactShadowNode.java @@ -452,6 +452,23 @@ public final int getTotalNativeChildren() { return mTotalNativeChildren; } + public boolean isDescendantOf(ReactShadowNode ancestorNode) { + ReactShadowNode parentNode = getParent(); + + boolean isDescendant = false; + + while (parentNode != null) { + if (parentNode == ancestorNode) { + isDescendant = true; + break; + } else { + parentNode = parentNode.getParent(); + } + } + + return isDescendant; + } + /** * Returns the offset within the native children owned by all layout-only nodes in the subtree * rooted at this node for the given child. Put another way, this returns the number of native diff --git a/ReactAndroid/src/main/java/com/facebook/react/uimanager/UIImplementation.java b/ReactAndroid/src/main/java/com/facebook/react/uimanager/UIImplementation.java index ab6e6f2aa998e3..6fc29266feb477 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/uimanager/UIImplementation.java +++ b/ReactAndroid/src/main/java/com/facebook/react/uimanager/UIImplementation.java @@ -469,6 +469,22 @@ public void findSubviewIn(int reactTag, float targetX, float targetY, Callback c mOperationsQueue.enqueueFindTargetForTouch(reactTag, targetX, targetY, callback); } + /** + * Check if the first shadow node is the descendant of the second shadow node + */ + public void viewIsDescendantOf( + final int reactTag, + final int ancestorReactTag, + final Callback callback) { + ReactShadowNode node = mShadowNodeRegistry.getNode(reactTag); + ReactShadowNode ancestorNode = mShadowNodeRegistry.getNode(ancestorReactTag); + if (node == null || ancestorNode == null) { + callback.invoke(false); + return; + } + callback.invoke(node.isDescendantOf(ancestorNode)); + } + /** * Determines the location on screen, width, and height of the given view relative to the root * view and returns the values via an async callback. diff --git a/ReactAndroid/src/main/java/com/facebook/react/uimanager/UIManagerModule.java b/ReactAndroid/src/main/java/com/facebook/react/uimanager/UIManagerModule.java index c9074438f83651..e173b0795c31c4 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/uimanager/UIManagerModule.java +++ b/ReactAndroid/src/main/java/com/facebook/react/uimanager/UIManagerModule.java @@ -412,6 +412,17 @@ public void findSubviewIn( callback); } + /** + * Check if the first shadow node is the descendant of the second shadow node + */ + @ReactMethod + public void viewIsDescendantOf( + final int reactTag, + final int ancestorReactTag, + final Callback callback) { + mUIImplementation.viewIsDescendantOf(reactTag, ancestorReactTag, callback); + } + /** * Registers a new Animation that can then be added to a View using {@link #addAnimation}. */