Permalink
Browse files

Introducing -[RCTShadowView canHaveSubviews]

Summary:
Override `canHaveSubviews` in RCTShadowView subclass to disallow any nested content.
For now, this prop will be checked only in DEV mode for performance reasons.

Reviewed By: javache

Differential Revision: D5189083

fbshipit-source-id: 87087dd806e1fd7320128dab969b13642174f81c
  • Loading branch information...
shergin authored and facebook-github-bot committed Jun 21, 2017
1 parent d0ad6ad commit abfa63c67e22b985edc65a1e856d4733c339480c
Showing with 21 additions and 1 deletion.
  1. +14 −1 React/Views/RCTShadowView.h
  2. +7 −0 React/Views/RCTShadowView.m
@@ -217,9 +217,22 @@ typedef void (^RCTApplierBlock)(NSDictionary<NSNumber *, UIView *> *viewRegistry
absolutePosition:(CGPoint)absolutePosition;
/**
* Return whether or not this node acts as a leaf node in the eyes of Yoga.
* Returns whether or not this view can have any subviews.
* Adding/inserting a child view to leaf view (`canHaveSubviews` equals `NO`)
* will throw an error.
* Return `NO` for components which must not have any descendants
* (like <Image>, for example.)
* Defaults to `YES`. Can be overridden in subclasses.
* Don't confuse this with `isYogaLeafNode`.
*/
- (BOOL)canHaveSubviews;
/**
* Returns whether or not this node acts as a leaf node in the eyes of Yoga.
* For example `RCTShadowText` has children which it does not want Yoga
* to lay out so in the eyes of Yoga it is a leaf node.
* Defaults to `NO`. Can be overridden in subclasses.
* Don't confuse this with `canHaveSubviews`.
*/
- (BOOL)isYogaLeafNode;
@@ -365,6 +365,11 @@ - (void)dealloc
YGNodeFree(_yogaNode);
}
- (BOOL)canHaveSubviews
{
return NO;
}
- (BOOL)isYogaLeafNode
{
return NO;
@@ -403,6 +408,8 @@ - (void)setTextComputed
- (void)insertReactSubview:(RCTShadowView *)subview atIndex:(NSInteger)atIndex
{
RCTAssert(!self.canHaveSubviews, @"Attempt to insert subview inside leaf view.");
[_reactSubviews insertObject:subview atIndex:atIndex];
if (![self isYogaLeafNode]) {
YGNodeInsertChild(_yogaNode, subview.yogaNode, (uint32_t)atIndex);

0 comments on commit abfa63c

Please sign in to comment.