Skip to content

Commit 25a04f3

Browse files
authored
refactor: simplify fabric template (#921)
1 parent 6a50608 commit 25a04f3

File tree

4 files changed

+11
-32
lines changed

4 files changed

+11
-32
lines changed

packages/create-react-native-library/templates/kotlin-view-new/android/src/main/java/com/{%- project.package_dir %}/{%- project.name %}ViewManager.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ class <%- project.name -%>ViewManager : SimpleViewManager<<%- project.name -%>Vi
3131
}
3232

3333
@ReactProp(name = "color")
34-
override fun setColor(view: <%- project.name -%>View?, color: String?) {
35-
view?.setBackgroundColor(Color.parseColor(color))
34+
override fun setColor(view: <%- project.name -%>View?, color: Int?) {
35+
view?.setBackgroundColor(color ?: Color.TRANSPARENT)
3636
}
3737

3838
companion object {
Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1-
import { codegenNativeComponent, type ViewProps } from 'react-native';
1+
import {
2+
codegenNativeComponent,
3+
type ColorValue,
4+
type ViewProps,
5+
} from 'react-native';
26

37
interface NativeProps extends ViewProps {
4-
color?: string;
8+
color?: ColorValue;
59
}
610

711
export default codegenNativeComponent<NativeProps>('<%- project.name -%>View');

packages/create-react-native-library/templates/nitro-view/android/src/main/java/com/margelo/nitro/{%- project.package_dir %}/{%- project.name %}.kt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,8 @@ import androidx.core.graphics.toColorInt
88
@DoNotStrip
99
class Hybrid<%- project.name %>(val context: ThemedReactContext) : Hybrid<%- project.name %>Spec() {
1010

11-
// View
1211
override val view: View = View(context)
1312

14-
// Props
1513
private var _color = "#000"
1614
override var color: String
1715
get() = _color
Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,15 @@
11
#import "<%- project.name -%>View.h"
22

3+
#import <React/RCTConversions.h>
4+
35
#import <react/renderer/components/<%- project.name -%>ViewSpec/ComponentDescriptors.h>
4-
#import <react/renderer/components/<%- project.name -%>ViewSpec/EventEmitters.h>
56
#import <react/renderer/components/<%- project.name -%>ViewSpec/Props.h>
67
#import <react/renderer/components/<%- project.name -%>ViewSpec/RCTComponentViewHelpers.h>
78

89
#import "RCTFabricComponentsPlugins.h"
910

1011
using namespace facebook::react;
1112

12-
@interface <%- project.name -%>View () <RCT<%- project.name -%>ViewViewProtocol>
13-
14-
@end
15-
1613
@implementation <%- project.name -%>View {
1714
UIView * _view;
1815
}
@@ -42,30 +39,10 @@ - (void)updateProps:(Props::Shared const &)props oldProps:(Props::Shared const &
4239
const auto &newViewProps = *std::static_pointer_cast<<%- project.name -%>ViewProps const>(props);
4340

4441
if (oldViewProps.color != newViewProps.color) {
45-
NSString * colorToConvert = [[NSString alloc] initWithUTF8String: newViewProps.color.c_str()];
46-
[_view setBackgroundColor:[self hexStringToColor:colorToConvert]];
42+
[_view setBackgroundColor: RCTUIColorFromSharedColor(newViewProps.color)];
4743
}
4844

4945
[super updateProps:props oldProps:oldProps];
5046
}
5147

52-
Class<RCTComponentViewProtocol> <%- project.name -%>ViewCls(void)
53-
{
54-
return <%- project.name -%>View.class;
55-
}
56-
57-
- hexStringToColor:(NSString *)stringToConvert
58-
{
59-
NSString *noHashString = [stringToConvert stringByReplacingOccurrencesOfString:@"#" withString:@""];
60-
NSScanner *stringScanner = [NSScanner scannerWithString:noHashString];
61-
62-
unsigned hex;
63-
if (![stringScanner scanHexInt:&hex]) return nil;
64-
int r = (hex >> 16) & 0xFF;
65-
int g = (hex >> 8) & 0xFF;
66-
int b = (hex) & 0xFF;
67-
68-
return [UIColor colorWithRed:r / 255.0f green:g / 255.0f blue:b / 255.0f alpha:1.0f];
69-
}
70-
7148
@end

0 commit comments

Comments
 (0)