Skip to content

Commit

Permalink
Introducing -[RCTShadowView canHaveSubviews]
Browse files Browse the repository at this point in the history
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 abfa63c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
15 changes: 14 additions & 1 deletion React/Views/RCTShadowView.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
7 changes: 7 additions & 0 deletions React/Views/RCTShadowView.m
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,11 @@ - (void)dealloc
YGNodeFree(_yogaNode);
}

- (BOOL)canHaveSubviews
{
return NO;
}

- (BOOL)isYogaLeafNode
{
return NO;
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit abfa63c

Please sign in to comment.