mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
1cb0a3342a
Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/43068 This diff adds `react-native-test-library` package. It contains native module and native component example, and targets both the new and the old architecture. It has structure similar to many OSS React Native libraries, and is supposed to be used to test the integration with third-party libraries. It is integrated with RNTester as the **OSS Library Example** screen. {F1457510909} **Change Background** tests native commands. **Set Opacity** tests native props. **Get Random Number** tests native module. Changelog: [Internal] Reviewed By: RSNara Differential Revision: D50793835 fbshipit-source-id: ff6daefab10e6e9f13049e3013f8f63cfa8a929e
71 lines
2.2 KiB
Plaintext
71 lines
2.2 KiB
Plaintext
/*
|
|
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
*
|
|
* This source code is licensed under the MIT license found in the
|
|
* LICENSE file in the root directory of this source tree.
|
|
*/
|
|
|
|
#import <React/RCTLog.h>
|
|
#import <React/RCTUIManager.h>
|
|
#import <React/RCTViewManager.h>
|
|
|
|
static UIColor *UIColorFromHexString(const std::string hexString)
|
|
{
|
|
unsigned rgbValue = 0;
|
|
NSString *colorString = [NSString stringWithCString:hexString.c_str() encoding:[NSString defaultCStringEncoding]];
|
|
NSScanner *scanner = [NSScanner scannerWithString:colorString];
|
|
[scanner setScanLocation:1]; // bypass '#' character
|
|
[scanner scanHexInt:&rgbValue];
|
|
return [UIColor colorWithRed:((rgbValue & 0xFF0000) >> 16) / 255.0
|
|
green:((rgbValue & 0xFF00) >> 8) / 255.0
|
|
blue:(rgbValue & 0xFF) / 255.0
|
|
alpha:1.0];
|
|
}
|
|
|
|
@interface RCTSampleNativeComponentViewManager : RCTViewManager
|
|
@end
|
|
|
|
@implementation RCTSampleNativeComponentViewManager
|
|
|
|
RCT_EXPORT_MODULE(SampleNativeComponent)
|
|
|
|
RCT_REMAP_VIEW_PROPERTY(color, backgroundColor, UIColor)
|
|
|
|
RCT_REMAP_VIEW_PROPERTY(opacity, alpha, CGFloat)
|
|
|
|
RCT_EXPORT_VIEW_PROPERTY(onColorChanged, RCTBubblingEventBlock)
|
|
|
|
RCT_CUSTOM_VIEW_PROPERTY(cornerRadius, CGFloat, UIView)
|
|
{
|
|
view.clipsToBounds = true;
|
|
NSNumber *cornerRadius = (NSNumber *)json;
|
|
view.layer.cornerRadius = [cornerRadius floatValue];
|
|
}
|
|
|
|
RCT_EXPORT_METHOD(changeBackgroundColor : (nonnull NSNumber *)reactTag color : (NSString *)color)
|
|
{
|
|
[self.bridge.uiManager addUIBlock:^(RCTUIManager *uiManager, NSDictionary<NSNumber *, UIView *> *viewRegistry) {
|
|
if (UIView *view = [RCTSampleNativeComponentViewManager getViewByTag:viewRegistry reactTag:reactTag]) {
|
|
view.backgroundColor = UIColorFromHexString(std::string(color.UTF8String));
|
|
}
|
|
}];
|
|
}
|
|
|
|
+ (UIView *)getViewByTag:(NSDictionary<NSNumber *, UIView *> *)viewRegistry reactTag:(nonnull NSNumber *)reactTag
|
|
{
|
|
UIView *view = viewRegistry[reactTag];
|
|
if (view == nil) {
|
|
RCTLogError(@"Cannot find view with tag #%@", reactTag);
|
|
}
|
|
return view;
|
|
}
|
|
|
|
- (UIView *)view
|
|
{
|
|
UIView *view = [UIView new];
|
|
view.backgroundColor = UIColor.redColor;
|
|
return view;
|
|
}
|
|
|
|
@end
|