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
This commit is contained in:
Valentin Shergin 2017-06-20 17:09:52 -07:00 committed by Facebook Github Bot
parent d0ad6ad413
commit abfa63c67e
2 changed files with 21 additions and 1 deletions

View File

@ -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;

View File

@ -365,6 +365,11 @@ static void RCTProcessMetaPropsBorder(const YGValue metaProps[META_PROP_COUNT],
YGNodeFree(_yogaNode);
}
- (BOOL)canHaveSubviews
{
return NO;
}
- (BOOL)isYogaLeafNode
{
return NO;
@ -403,6 +408,8 @@ static void RCTProcessMetaPropsBorder(const YGValue metaProps[META_PROP_COUNT],
- (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);