[rn] Pass props when creating a view in RCTViewManager

This commit is contained in:
Matt Mahoney 2015-08-25 07:45:07 -07:00
parent 27a67c2434
commit 36c1064033
5 changed files with 19 additions and 5 deletions

View File

@ -758,7 +758,7 @@ RCT_EXPORT_METHOD(createView:(nonnull NSNumber *)reactTag
UIColor *backgroundColor = shadowView.backgroundColor;
[self addUIBlock:^(RCTUIManager *uiManager, RCTSparseArray *viewRegistry){
id<RCTComponent> view = [componentData createViewWithTag:reactTag];
id<RCTComponent> view = [componentData createViewWithTag:reactTag props:props];
if ([view respondsToSelector:@selector(setBackgroundColor:)]) {
((UIView *)view).backgroundColor = backgroundColor;
}

View File

@ -22,7 +22,7 @@
- (instancetype)initWithManager:(RCTViewManager *)manager NS_DESIGNATED_INITIALIZER;
- (id<RCTComponent>)createViewWithTag:(NSNumber *)tag;
- (id<RCTComponent>)createViewWithTag:(NSNumber *)tag props:(NSDictionary *)props;
- (RCTShadowView *)createShadowViewWithTag:(NSNumber *)tag;
- (void)setProps:(NSDictionary *)props forView:(id<RCTComponent>)view;
- (void)setProps:(NSDictionary *)props forShadowView:(RCTShadowView *)shadowView;

View File

@ -62,11 +62,11 @@ typedef void (^RCTPropBlock)(id<RCTComponent> view, id json);
RCT_NOT_IMPLEMENTED(- (instancetype)init)
- (id<RCTComponent>)createViewWithTag:(NSNumber *)tag
- (id<RCTComponent>)createViewWithTag:(NSNumber *)tag props:(NSDictionary *)props
{
RCTAssertMainThread();
id<RCTComponent> view = (id<RCTComponent>)[_manager view];
id<RCTComponent> view = (id<RCTComponent>)(props ? [_manager viewWithProps:props] : [_manager view]);
view.reactTag = tag;
if ([view isKindOfClass:[UIView class]]) {
((UIView *)view).multipleTouchEnabled = YES;
@ -264,7 +264,7 @@ RCT_NOT_IMPLEMENTED(- (instancetype)init)
}
if (!_defaultView) {
_defaultView = [self createViewWithTag:nil];
_defaultView = [self createViewWithTag:nil props:nil];
}
[props enumerateKeysAndObjectsUsingBlock:^(NSString *key, id json, __unused BOOL *stop) {

View File

@ -39,6 +39,15 @@ typedef void (^RCTViewManagerUIBlock)(RCTUIManager *uiManager, RCTSparseArray *v
*/
- (UIView *)view;
/**
* This method instantiates a native view using the props passed into the component.
* This method should be used when you need to know about specific props in order to
* initialize a view. By default, this just calls the -view method. Each prop will
* still be set individually, after the view is created. Like the -view method,
* -viewWithProps: should return a fresh instance each time it is called.
*/
- (UIView *)viewWithProps:(NSDictionary *)props;
/**
* This method instantiates a shadow view to be managed by the module. If omitted,
* an ordinary RCTShadowView instance will be created, which is typically fine for

View File

@ -54,6 +54,11 @@ RCT_EXPORT_MODULE()
return _bridge.uiManager.methodQueue;
}
- (UIView *)viewWithProps:(NSDictionary *)props
{
return [self view];
}
- (UIView *)view
{
return [RCTView new];