Skip to content

Commit c005830

Browse files
cipolleschifacebook-github-bot
authored andcommittedMar 10, 2023
Add Fabric Interop event example
Summary: This changes adds an example to RNTester to verify that the Interop Layer can process bubbling events in Fabric as it used to do in Paper. ## Changelog: [iOS][Added] - Add example in the Interop Layer to use events Reviewed By: sammy-SC Differential Revision: D43911390 fbshipit-source-id: ae75db25078669676e5a609e090f1e9674026391
·
v0.80.1latest
1 parent e26092a commit c005830

File tree

5 files changed

+104
-2
lines changed

5 files changed

+104
-2
lines changed
 
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
#import <React/RCTComponent.h>
9+
#import <UIKit/UIKit.h>
10+
11+
NS_ASSUME_NONNULL_BEGIN
12+
13+
@interface RNTLegacyView : UIView
14+
15+
@property (nonatomic, copy) RCTBubblingEventBlock onColorChanged;
16+
17+
@end
18+
19+
NS_ASSUME_NONNULL_END
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
#import "RNTLegacyView.h"
9+
10+
@implementation RNTLegacyView
11+
12+
- (void)setBackgroundColor:(UIColor *)backgroundColor
13+
{
14+
super.backgroundColor = backgroundColor;
15+
[self emitEvent];
16+
}
17+
18+
- (void)emitEvent
19+
{
20+
if (!self.onColorChanged) {
21+
return;
22+
}
23+
CGFloat hue = 0.0;
24+
CGFloat saturation = 0.0;
25+
CGFloat brightness = 0.0;
26+
CGFloat alpha = 0.0;
27+
[self.backgroundColor getHue:&hue saturation:&saturation brightness:&brightness alpha:&alpha];
28+
self.onColorChanged(@{
29+
@"backgroundColor" :
30+
@{@"hue" : @(hue), @"saturation" : @(saturation), @"brightness" : @(brightness), @"alpha" : @(alpha)}
31+
});
32+
}
33+
34+
@end

‎packages/rn-tester/NativeComponentExample/ios/RNTMyLegacyNativeViewManager.mm

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#import <React/RCTLog.h>
99
#import <React/RCTUIManager.h>
1010
#import <React/RCTViewManager.h>
11+
#import "RNTLegacyView.h"
1112
#import "RNTMyNativeViewComponentView.h"
1213

1314
@interface RNTMyLegacyNativeViewManager : RCTViewManager
@@ -22,10 +23,11 @@ @implementation RNTMyLegacyNativeViewManager
2223

2324
RCT_REMAP_VIEW_PROPERTY(opacity, alpha, CGFloat)
2425

26+
RCT_EXPORT_VIEW_PROPERTY(onColorChanged, RCTBubblingEventBlock)
27+
2528
- (UIView *)view
2629
{
27-
UIView *view = [[UIView alloc] init];
28-
view.backgroundColor = UIColor.greenColor;
30+
RNTLegacyView *view = [[RNTLegacyView alloc] init];
2931
return view;
3032
}
3133

‎packages/rn-tester/NativeComponentExample/js/MyLegacyViewNativeComponent.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,22 @@ import type {HostComponent} from 'react-native';
1212
import type {ViewProps} from 'react-native/Libraries/Components/View/ViewPropTypes';
1313
import {requireNativeComponent} from 'react-native';
1414

15+
type ColorChangedEvent = {
16+
nativeEvent: {
17+
backgroundColor: {
18+
hue: number,
19+
saturation: number,
20+
brightness: number,
21+
alpha: number,
22+
},
23+
},
24+
};
25+
1526
type NativeProps = $ReadOnly<{|
1627
...ViewProps,
1728
opacity?: number,
1829
color?: string,
30+
onColorChanged?: (event: ColorChangedEvent) => void,
1931
|}>;
2032

2133
export type MyLegacyViewType = HostComponent<NativeProps>;

‎packages/rn-tester/NativeComponentExample/js/MyNativeView.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,35 @@ const colors = [
2626
'#000033',
2727
];
2828

29+
class HSBA {
30+
hue: number;
31+
saturation: number;
32+
brightness: number;
33+
alpha: number;
34+
35+
constructor(
36+
hue: number = 0.0,
37+
saturation: number = 0.0,
38+
brightness: number = 0.0,
39+
alpha: number = 0.0,
40+
) {
41+
this.hue = hue;
42+
this.saturation = saturation;
43+
this.brightness = brightness;
44+
this.alpha = alpha;
45+
}
46+
47+
toString(): string {
48+
return `h: ${this.hue}, s: ${this.saturation}, b: ${this.brightness}, a: ${this.alpha}`;
49+
}
50+
}
51+
2952
// This is an example component that migrates to use the new architecture.
3053
export default function MyNativeView(props: {}): React.Node {
3154
const ref = useRef<React.ElementRef<MyNativeViewType> | null>(null);
3255
const [opacity, setOpacity] = useState(1.0);
3356
const [color, setColor] = useState('#FF0000');
57+
const [hsba, setHsba] = useState<HSBA>(new HSBA());
3458
return (
3559
<View style={{flex: 1}}>
3660
<Text style={{color: 'red'}}>Fabric View</Text>
@@ -40,7 +64,18 @@ export default function MyNativeView(props: {}): React.Node {
4064
style={{flex: 1}}
4165
opacity={opacity}
4266
color={color}
67+
onColorChanged={event =>
68+
setHsba(
69+
new HSBA(
70+
event.nativeEvent.backgroundColor.hue,
71+
event.nativeEvent.backgroundColor.saturation,
72+
event.nativeEvent.backgroundColor.brightness,
73+
event.nativeEvent.backgroundColor.alpha,
74+
),
75+
)
76+
}
4377
/>
78+
<Text>HSBA: {hsba.toString()}</Text>
4479
<Button
4580
title="Change Background"
4681
onPress={() => {

0 commit comments

Comments
 (0)
Please sign in to comment.