Skip to content

Commit 2cdd6d7

Browse files
cuvafacebook-github-bot
authored andcommitted
Expose CKCompositeComponent to Swift
Summary: Ditto. Reviewed By: Andrey-Mishanin Differential Revision: D19744254 fbshipit-source-id: 84bf5ffb663db8570d47cf0ea91616fd62d6adeb
1 parent 65e87cd commit 2cdd6d7

7 files changed

+52
-11
lines changed

ComponentKit/Core/CKCompositeComponent.h

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,8 @@
88
*
99
*/
1010

11+
#import <Foundation/Foundation.h>
1112
#import <ComponentKit/CKDefines.h>
12-
13-
#if CK_NOT_SWIFT
14-
1513
#import <ComponentKit/CKComponent.h>
1614

1715
NS_ASSUME_NONNULL_BEGIN
@@ -29,8 +27,17 @@ NS_ASSUME_NONNULL_BEGIN
2927
3028
@warning Overriding -layoutThatFits:parentSize: or -computeLayoutThatFits: is **not allowed** for any subclass.
3129
*/
30+
NS_SWIFT_NAME(CompositeComponent)
3231
@interface CKCompositeComponent : CKComponent
3332

33+
- (instancetype)init NS_UNAVAILABLE;
34+
- (instancetype _Nullable)initWithComponent:(NS_RELEASES_ARGUMENT CKComponent *_Nullable)component CK_OBJC_UNAVAILABLE;
35+
36+
#if CK_NOT_SWIFT
37+
38+
- (instancetype _Nullable)initWithView:(const CKComponentViewConfiguration &)view
39+
component:(NS_RELEASES_ARGUMENT CKComponent * _Nullable)component CK_SWIFT_DESIGNATED_INITIALIZER;
40+
3441
/** Calls the initializer with {} for view. */
3542
+ (instancetype _Nullable)newWithComponent:(NS_RELEASES_ARGUMENT CKComponent * _Nullable)component;
3643

@@ -41,18 +48,20 @@ NS_ASSUME_NONNULL_BEGIN
4148
*/
4249
+ (instancetype _Nullable)newWithView:(const CKComponentViewConfiguration &)view component:(NS_RELEASES_ARGUMENT CKComponent * _Nullable)component;
4350

44-
/** Access the child component. For intenral use only. */
45-
- (CKComponent * _Nullable)child;
51+
CK_COMPONENT_INIT_UNAVAILABLE;
52+
53+
#endif
54+
55+
/** Access the child component. For internal use only. */
56+
@property (nonatomic, strong, readonly, nullable) CKComponent *child;
57+
4658

4759
@end
4860

4961
#define CK_COMPOSITE_COMPONENT_INIT_UNAVAILABLE \
50-
CK_COMPONENT_INIT_UNAVAILABLE; \
5162
+ (instancetype _Nullable)newWithComponent:(NS_RELEASES_ARGUMENT CKComponent * _Nullable)component NS_UNAVAILABLE; \
5263
+ (instancetype _Nullable)newWithView:(const CKComponentViewConfiguration &)view component:(NS_RELEASES_ARGUMENT CKComponent * _Nullable)component NS_UNAVAILABLE
5364

5465
NS_ASSUME_NONNULL_END
5566

5667
#import <ComponentKit/CompositeComponentBuilder.h>
57-
58-
#endif

ComponentKit/Core/CKCompositeComponent.mm

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,21 +41,40 @@ + (void)initialize
4141
}
4242
#endif
4343

44+
- (instancetype _Nullable)initWithView:(const CKComponentViewConfiguration &)view
45+
component:(CKComponent * _Nullable)component
46+
{
47+
if (component == nil) {
48+
return nil;
49+
}
50+
51+
if (self = [super initWithView:view size:{}]) {
52+
_child = component;
53+
}
54+
return self;
55+
}
56+
57+
- (instancetype _Nullable)initWithComponent:(CKComponent * _Nullable)component
58+
{
59+
return [self initWithView:{} component:component];
60+
}
61+
4462
+ (instancetype)newWithComponent:(CKComponent *)component
4563
{
4664
return [self newWithView:{} component:component];
4765
}
4866

4967
+ (instancetype)newWithView:(const CKComponentViewConfiguration &)view component:(CKComponent *)component
5068
{
51-
if (!component) {
69+
if (component == nil) {
5270
return nil;
5371
}
5472

5573
CKCompositeComponent *c = [super newWithView:view size:{}];
56-
if (c) {
74+
if (c != nil) {
5775
c->_child = component;
5876
}
77+
5978
return c;
6079
}
6180

ComponentKitTestHelpers/CKEmbeddedTestComponent.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
This can be used to test what happens when state changes and the LifecycleTestComponent is removed.
99
Call setLifecycleTestComponentIsHidden to trigger the corresponding state change. */
1010
@interface CKEmbeddedTestComponent : CKCompositeComponent
11+
12+
+ (instancetype)newWithView:(const CKComponentViewConfiguration &)view size:(const CKComponentSize &)size;
13+
1114
- (void)setLifecycleTestComponentIsHidden:(BOOL)isHidden;
1215
- (CKLifecycleTestComponent *)lifecycleTestComponent;
16+
1317
@end

ComponentKitTests/CKComponentGeneratorTests.mm

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ @interface CKComponentGeneratorTests : XCTestCase <CKComponentGeneratorDelegate>
2222

2323
@interface CKTestStateComponent : CKCompositeComponent
2424

25+
+ (instancetype)new;
26+
2527
@property (nonatomic, readonly, strong) id state;
2628

2729
@end

ComponentKitTests/CKComponentViewContextTests.mm

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ @interface CKComponentViewContextTests : XCTestCase
2424
@interface CKNestedComponent : CKCompositeComponent
2525
@property (nonatomic, strong) CKComponent *subcomponent;
2626

27+
+ (instancetype)new;
28+
2729
@end
2830

2931
@implementation CKComponentViewContextTests
@@ -40,7 +42,6 @@ @implementation CKComponentViewContextTests
4042
return [CKNestedComponent new];
4143
}
4244

43-
4445
static const CKSizeRange size = {{100, 100}, {100, 100}};
4546

4647
- (void)testMountingComponentWithViewExposesViewContextWithTheCreatedView

ComponentKitTests/TransactionalDataSource/CKDataSourceReloadModificationTests.mm

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@
3030
static u_int32_t lifecycleComponentState = 1;
3131

3232
@interface CKTestGlobalStateComponent : CKCompositeComponent
33+
34+
+ (instancetype)new;
35+
3336
@property (nonatomic, readonly) u_int32_t globalStateAtTimeOfCreation;
3437
@property (nonatomic, readonly) CKLifecycleTestComponent *lifecycleComponent;
3538
@end

ComponentKitTests/TransactionalDataSource/CKDataSourceUpdateStateModificationTests.mm

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@
2929
static NSString *const kTestStateForLifecycleComponent = @"kTestStateForLifecycleComponent";
3030

3131
@interface CKStatefulTestComponent : CKCompositeComponent
32+
33+
+ (instancetype)new;
34+
3235
@property (nonatomic, readonly) NSString *state;
3336
@property (nonatomic, readonly) CKLifecycleTestComponent *lifecycleComponent;
3437
@end

0 commit comments

Comments
 (0)