Skip to content

Commit abfa63c

Browse files
sherginfacebook-github-bot
authored andcommitted
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
1 parent d0ad6ad commit abfa63c

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

React/Views/RCTShadowView.h

+14-1
Original file line numberDiff line numberDiff line change
@@ -217,9 +217,22 @@ typedef void (^RCTApplierBlock)(NSDictionary<NSNumber *, UIView *> *viewRegistry
217217
absolutePosition:(CGPoint)absolutePosition;
218218

219219
/**
220-
* Return whether or not this node acts as a leaf node in the eyes of Yoga.
220+
* Returns whether or not this view can have any subviews.
221+
* Adding/inserting a child view to leaf view (`canHaveSubviews` equals `NO`)
222+
* will throw an error.
223+
* Return `NO` for components which must not have any descendants
224+
* (like <Image>, for example.)
225+
* Defaults to `YES`. Can be overridden in subclasses.
226+
* Don't confuse this with `isYogaLeafNode`.
227+
*/
228+
- (BOOL)canHaveSubviews;
229+
230+
/**
231+
* Returns whether or not this node acts as a leaf node in the eyes of Yoga.
221232
* For example `RCTShadowText` has children which it does not want Yoga
222233
* to lay out so in the eyes of Yoga it is a leaf node.
234+
* Defaults to `NO`. Can be overridden in subclasses.
235+
* Don't confuse this with `canHaveSubviews`.
223236
*/
224237
- (BOOL)isYogaLeafNode;
225238

React/Views/RCTShadowView.m

+7
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,11 @@ - (void)dealloc
365365
YGNodeFree(_yogaNode);
366366
}
367367

368+
- (BOOL)canHaveSubviews
369+
{
370+
return NO;
371+
}
372+
368373
- (BOOL)isYogaLeafNode
369374
{
370375
return NO;
@@ -403,6 +408,8 @@ - (void)setTextComputed
403408

404409
- (void)insertReactSubview:(RCTShadowView *)subview atIndex:(NSInteger)atIndex
405410
{
411+
RCTAssert(!self.canHaveSubviews, @"Attempt to insert subview inside leaf view.");
412+
406413
[_reactSubviews insertObject:subview atIndex:atIndex];
407414
if (![self isYogaLeafNode]) {
408415
YGNodeInsertChild(_yogaNode, subview.yogaNode, (uint32_t)atIndex);

0 commit comments

Comments
 (0)