forked from facebook/react-native
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathElement.h
More file actions
160 lines (139 loc) · 4.03 KB
/
Element.h
File metadata and controls
160 lines (139 loc) · 4.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#pragma once
#include <functional>
#include <memory>
#include <react/core/ShadowNode.h>
#include <react/element/ElementFragment.h>
namespace facebook {
namespace react {
/*
* `Element<>` is an abstraction layer that allows describing component
* hierarchy in a declarative way. Creating `Element`s themself does not create
* a component tree (aka `ShadowNode` tree) but describes some hierarchical
* structure that might be used to build an actual component tree (similar to
* XML Elements).
* `Element` provides some basic type-safety guarantees: all modifications
* of element objects require using objects (such as Props or State) of
* compatible type.
*/
template <typename ShadowNodeT>
class Element final {
public:
using ConcreteProps = typename ShadowNodeT::ConcreteProps;
using SharedConcreteProps = std::shared_ptr<ConcreteProps const>;
using ConcreteState = typename ShadowNodeT::ConcreteState;
using SharedConcreteState = std::shared_ptr<ConcreteState const>;
using ConcreteShadowNode = ShadowNodeT;
using ConcreteUnsharedShadowNode = std::shared_ptr<ConcreteShadowNode>;
using ConcreteReferenceCallback =
std::function<void(std::shared_ptr<ShadowNodeT const> const &shadowNode)>;
/*
* Constructs an `Element`.
*/
Element() {
fragment_.componentHandle = ShadowNodeT::Handle();
fragment_.componentName = ShadowNodeT::Name();
fragment_.props = ShadowNodeT::defaultSharedProps();
}
/*
* Converts to `ElementFragment` object.
*/
operator ElementFragment() {
return fragment_;
}
/*
* Sets `tag`.
*/
Element &tag(Tag tag) {
fragment_.tag = tag;
return *this;
}
/*
* Sets `surfaceId`.
*/
Element &surfaceId(SurfaceId surfaceId) {
fragment_.surfaceId = surfaceId;
return *this;
}
/*
* Sets `props`.
*/
Element &props(SharedConcreteProps props) {
fragment_.props = props;
return *this;
}
/*
* Sets `props` using callback.
*/
Element &props(std::function<SharedConcreteProps()> callback) {
fragment_.props = callback();
return *this;
}
/*
* Sets `state`.
*/
Element &state(SharedConcreteState state) {
fragment_.state = state;
return *this;
}
/*
* Sets `state` using callback.
*/
Element &state(std::function<SharedConcreteState()> callback) {
fragment_.state = state();
return *this;
}
/*
* Sets children.
*/
Element &children(std::vector<ElementFragment> children) {
auto fragments = ElementFragment::List{};
fragments.reserve(children.size());
for (auto const &child : children) {
fragments.push_back(child);
}
fragment_.children = fragments;
return *this;
}
/*
* Calls the callback during component construction with a pointer to the
* component which is being constructed.
*/
Element &reference(
std::function<void(ConcreteUnsharedShadowNode const &shadowNode)>
callback) {
fragment_.referenceCallback = callback;
return *this;
}
/*
* During component construction, assigns a given pointer to a component
* that is being constructed.
*/
Element &reference(ConcreteUnsharedShadowNode &outShadowNode) {
fragment_.referenceCallback = [&](ShadowNode::Shared const &shadowNode) {
outShadowNode = std::const_pointer_cast<ConcreteShadowNode>(
std::static_pointer_cast<ConcreteShadowNode const>(shadowNode));
};
return *this;
}
/*
* Calls the callback with a reference to a just constructed component.
*/
Element &finalize(
std::function<void(ConcreteShadowNode &shadowNode)> finalizeCallback) {
fragment_.finalizeCallback = [=](ShadowNode &shadowNode) {
return finalizeCallback(static_cast<ConcreteShadowNode &>(shadowNode));
};
return *this;
}
private:
friend class ComponentBuilder;
ElementFragment fragment_;
};
} // namespace react
} // namespace facebook