-
Notifications
You must be signed in to change notification settings - Fork 24.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow iOS PlatformColor strings to be ObjC or Swift UIColor selectors #28703
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -604,7 +604,7 @@ +(type)type : (id)json \ | |
{ | ||
static NSDictionary<NSString *, NSDictionary *> *colorMap = nil; | ||
if (colorMap == nil) { | ||
colorMap = @{ | ||
NSMutableDictionary<NSString *, NSDictionary *> *map = [@{ | ||
// https://developer.apple.com/documentation/uikit/uicolor/ui_element_colors | ||
// Label Colors | ||
@"labelColor" : @{ | ||
|
@@ -729,7 +729,22 @@ +(type)type : (id)json \ | |
// iOS 13.0 | ||
RCTFallbackARGB : @(0xFFf2f2f7) | ||
}, | ||
} mutableCopy]; | ||
// The color names are the Objective-C UIColor selector names, | ||
// but Swift selector names are valid as well, so make aliases. | ||
static NSString *const RCTColorSuffix = @"Color"; | ||
NSMutableDictionary<NSString *, NSDictionary *> *aliases = [NSMutableDictionary new]; | ||
for (NSString *objcSelector in map) { | ||
RCTAssert([objcSelector hasSuffix:RCTColorSuffix], @"A selector in the color map did not end with the suffix Color."); | ||
NSMutableDictionary *entry = [map[objcSelector] mutableCopy]; | ||
RCTAssert([entry objectForKey:RCTSelector] == nil, @"Entry should not already have an RCTSelector"); | ||
NSString *swiftSelector = [objcSelector substringToIndex:[objcSelector length] - [RCTColorSuffix length]]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Seems like this code could have been slightly simpler/faster if it had been reversed, i.e. manually add them all without the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If reversed, it would mean having to add an explicit RCTSelector for each entry -- which is more error prone for maintenance. I like it this way because the table is more truthy to ObjC which is the native language of the RN implementation itself. |
||
entry[RCTSelector] = objcSelector; | ||
aliases[swiftSelector] = entry; | ||
} | ||
[map addEntriesFromDictionary:aliases]; | ||
#if DEBUG | ||
[map addEntriesFromDictionary:@{ | ||
// The follow exist for Unit Tests | ||
@"unitTestFallbackColor" : @{RCTFallback : @"gridColor"}, | ||
@"unitTestFallbackColorIOS" : @{RCTFallback : @"blueColor"}, | ||
|
@@ -743,9 +758,11 @@ +(type)type : (id)json \ | |
RCTIndex : @1, | ||
RCTFallback : @"controlAlternatingRowBackgroundColors" | ||
}, | ||
}]; | ||
#endif | ||
}; | ||
colorMap = [map copy]; | ||
} | ||
|
||
return colorMap; | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you want to keep any examples that include the Color suffix? And / or maybe have an automated test that verifies both ways work?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As I said in the Test notes, there still are ObjC examples later in this file and in unit tests
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, whoops. I should read better :D