Skip to content

Commit 94ca492

Browse files
committed
feat: mutate child nodes with native state
1 parent e7bbd7d commit 94ca492

File tree

8 files changed

+31
-187
lines changed

8 files changed

+31
-187
lines changed

apps/example/src/Screens/Chat.tsx

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,7 @@ export function Chat({
3030
style={styles.container}
3131
>
3232
<Button onPress={() => Alert.alert('Hey')} title="Press me" />
33-
<ScrollView
34-
contentInsetAdjustmentBehavior="automatic"
35-
style={styles.inverted}
36-
contentContainerStyle={styles.content}
37-
{...rest}
38-
>
33+
<View style={styles.inverted} {...rest}>
3934
{MESSAGES.map((text, i) => {
4035
const odd = i % 2;
4136

@@ -63,7 +58,7 @@ export function Chat({
6358
</View>
6459
);
6560
})}
66-
</ScrollView>
61+
</View>
6762
<TextInput
6863
style={[styles.input, { backgroundColor: 'white', color: 'black' }]}
6964
placeholderTextColor={'#B0B0B0'}

packages/react-native-bottom-tabs/common/cpp/react/renderer/components/RNCTabView/RNCTabViewComponentDescriptor.h

Lines changed: 7 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,37 +9,18 @@ namespace facebook::react {
99

1010
class RNCTabViewComponentDescriptor final : public ConcreteComponentDescriptor<RNCTabViewShadowNode>
1111
{
12-
#ifdef ANDROID
1312
public:
1413
RNCTabViewComponentDescriptor(const ComponentDescriptorParameters &parameters)
15-
: ConcreteComponentDescriptor(parameters), measurementsManager_(
16-
std::make_shared<RNCTabViewMeasurementsManager>(contextContainer_)) {}
17-
18-
void adopt(ShadowNode &shadowNode) const override
19-
{
14+
: ConcreteComponentDescriptor(parameters) {}
15+
16+
void adopt(ShadowNode &shadowNode) const override {
17+
react_native_assert(dynamic_cast<RNCTabViewShadowNode *>(&shadowNode));
18+
19+
const auto tabViewShadowNode = dynamic_cast<RNCTabViewShadowNode *>(&shadowNode);
20+
tabViewShadowNode->updateStateIfNeeded();
2021
ConcreteComponentDescriptor::adopt(shadowNode);
21-
22-
auto &rncTabViewShadowNode =
23-
static_cast<RNCTabViewShadowNode &>(shadowNode);
24-
25-
// `RNCTabViewShadowNode` uses `RNCTabViewMeasurementsManager` to
26-
// provide measurements to Yoga.
27-
rncTabViewShadowNode.setSliderMeasurementsManager(
28-
measurementsManager_);
29-
30-
// All `RNCTabViewShadowNode`s must have leaf Yoga nodes with properly
31-
// setup measure function.
32-
rncTabViewShadowNode.enableMeasurement();
3322
}
3423

35-
private:
36-
const std::shared_ptr<RNCTabViewMeasurementsManager> measurementsManager_;
37-
#else
38-
public:
39-
RNCTabViewComponentDescriptor(const ComponentDescriptorParameters &parameters)
40-
: ConcreteComponentDescriptor(parameters) {}
41-
#endif
42-
4324
};
4425

4526
}

packages/react-native-bottom-tabs/common/cpp/react/renderer/components/RNCTabView/RNCTabViewMeasurementsManager.cpp

Lines changed: 0 additions & 65 deletions
This file was deleted.

packages/react-native-bottom-tabs/common/cpp/react/renderer/components/RNCTabView/RNCTabViewMeasurementsManager.h

Lines changed: 0 additions & 31 deletions
This file was deleted.
Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,20 @@
11
#include "RNCTabViewShadowNode.h"
2-
#include "RNCTabViewMeasurementsManager.h"
32

43
namespace facebook::react {
54

65
extern const char RNCTabViewComponentName[] = "RNCTabView";
76

8-
#ifdef ANDROID
9-
void RNCTabViewShadowNode::setSliderMeasurementsManager(
10-
const std::shared_ptr<RNCTabViewMeasurementsManager> &
11-
measurementsManager)
12-
{
7+
void RNCTabViewShadowNode::updateStateIfNeeded() {
138
ensureUnsealed();
14-
measurementsManager_ = measurementsManager;
15-
}
16-
17-
#pragma mark - LayoutableShadowNode
18-
19-
Size RNCTabViewShadowNode::measureContent(
20-
const LayoutContext & /*layoutContext*/,
21-
const LayoutConstraints &layoutConstraints) const
22-
{
23-
return measurementsManager_->measure(getSurfaceId(), layoutConstraints);
24-
}
25-
26-
#endif
9+
const auto &stateData = getStateData();
10+
auto frameSize = stateData.frameSize;
11+
12+
for (auto &child : getLayoutableChildNodes()) {
13+
auto yogaChild = dynamic_cast<YogaLayoutableShadowNode*>(child);
14+
if (!yogaChild->getSealed() && yogaChild->getLayoutMetrics().frame.size != frameSize) {
15+
yogaChild->setSize(frameSize);
16+
}
17+
}
18+
};
2719

2820
}

packages/react-native-bottom-tabs/common/cpp/react/renderer/components/RNCTabView/RNCTabViewShadowNode.h

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
#include <react/renderer/components/RNCTabView/EventEmitters.h>
99
#include <react/renderer/components/view/ConcreteViewShadowNode.h>
1010

11-
#include "RNCTabViewMeasurementsManager.h"
12-
1311
namespace facebook::react {
1412

1513
JSI_EXPORT extern const char RNCTabViewComponentName[];
@@ -26,23 +24,9 @@ RNCTabViewState>
2624
{
2725
public:
2826
using ConcreteViewShadowNode::ConcreteViewShadowNode;
29-
30-
#ifdef ANDROID
31-
void setSliderMeasurementsManager(
32-
const std::shared_ptr<RNCTabViewMeasurementsManager> &measurementsManager);
33-
34-
#pragma mark - LayoutableShadowNode
35-
36-
Size measureContent(
37-
const LayoutContext &layoutContext,
38-
const LayoutConstraints &layoutConstraints) const override;
39-
40-
private:
41-
std::shared_ptr<RNCTabViewMeasurementsManager> measurementsManager_;
42-
#endif
43-
27+
28+
void updateStateIfNeeded();
4429
};
45-
4630
}
4731

4832
#endif

packages/react-native-bottom-tabs/common/cpp/react/renderer/components/RNCTabView/RNCTabViewState.h

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#ifdef __cplusplus
22

33
#pragma once
4-
4+
#include <react/renderer/graphics/Size.h>
55
#ifdef ANDROID
66
#include <folly/dynamic.h>
77
#include <react/renderer/mapbuffer/MapBuffer.h>
@@ -14,20 +14,9 @@ class RNCTabViewState
1414
{
1515
public:
1616
RNCTabViewState() = default;
17+
RNCTabViewState(Size frameSize): frameSize(frameSize) {};
1718

18-
#ifdef ANDROID
19-
RNCTabViewState(RNCTabViewState const &previousState, folly::dynamic data) {};
20-
21-
folly::dynamic getDynamic() const
22-
{
23-
return {};
24-
};
25-
26-
MapBuffer getMapBuffer() const
27-
{
28-
return MapBufferBuilder::EMPTY();
29-
};
30-
#endif
19+
Size frameSize;
3120
};
3221

3322
}

packages/react-native-bottom-tabs/ios/Fabric/RCTTabViewComponentView.mm

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ @interface RCTTabViewComponentView () <RCTRNCTabViewViewProtocol, TabViewProvide
3838
@implementation RCTTabViewComponentView {
3939
TabViewProvider *_tabViewProvider;
4040
NSMutableArray<PlatformView *> *_reactSubviews;
41+
RNCTabViewShadowNode::ConcreteState::Shared _state;
4142
}
4243

4344
+ (ComponentDescriptorProvider)componentDescriptorProvider
@@ -85,6 +86,10 @@ - (void)unmountChildComponentView:(PlatformView<RCTComponentViewProtocol> *)chil
8586
[childComponentView removeFromSuperview];
8687
}
8788

89+
- (void)updateState:(const facebook::react::State::Shared &)state oldState:(const facebook::react::State::Shared &)oldState {
90+
_state = std::static_pointer_cast<const RNCTabViewShadowNode::ConcreteState>(state);
91+
}
92+
8893
- (void)updateProps:(Props::Shared const &)props oldProps:(Props::Shared const &)oldProps
8994
{
9095
const auto &oldViewProps = *std::static_pointer_cast<RNCTabViewProps const>(_props);
@@ -246,13 +251,7 @@ - (void)onTabBarMeasuredWithHeight:(NSInteger)height reactTag:(NSNumber *)reactT
246251
}
247252

248253
- (void)onLayoutWithSize:(CGSize)size reactTag:(NSNumber *)reactTag {
249-
auto eventEmitter = std::static_pointer_cast<const RNCTabViewEventEmitter>(_eventEmitter);
250-
if (eventEmitter) {
251-
eventEmitter->onNativeLayout(RNCTabViewEventEmitter::OnNativeLayout {
252-
.height = size.height,
253-
.width = size.width
254-
});
255-
}
254+
_state->updateState(RNCTabViewState({size.width, size.height}));
256255
}
257256

258257
@end

0 commit comments

Comments
 (0)