-
Notifications
You must be signed in to change notification settings - Fork 164
/
RBuilder.kt
191 lines (155 loc) · 4.89 KB
/
RBuilder.kt
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package react
import kotlinext.js.js
import kotlinext.js.jso
import kotlin.reflect.KClass
typealias Render = RBuilder.() -> Unit
@ReactDsl
interface RBuilder {
val childList: MutableList<ReactNode>
fun child(element: ReactNode) {
childList.add(element)
}
operator fun ReactNode.unaryPlus() {
child(this)
}
operator fun String.unaryPlus() {
child(ReactNode(this))
}
fun <P : Props> child(
type: ElementType<P>,
props: P = jso(),
handler: RHandler<P>? = null,
) {
if (handler == null) {
child(createElement(type, props))
return
}
val children = with(RElementBuilder(props)) {
handler()
childList
}
child(type, props, children)
}
private fun <P : Props> child(
type: ElementType<P>,
props: P,
children: List<ReactNode>,
) {
child(createElement(type, props, *children.toTypedArray()))
}
operator fun <P : Props> ElementType<P>.invoke() {
child(createElement(this))
}
operator fun <P : Props> ElementType<P>.invoke(
handler: RHandler<P>,
) {
child(this, handler = handler)
}
operator fun <T> Provider<T>.invoke(
value: T,
handler: RHandler<ProviderProps<T>>,
) {
child(this) {
attrs.value = value
handler()
}
}
operator fun <T> Consumer<T>.invoke(
handler: RBuilder.(T) -> Unit,
) {
child(this) {
attrs.children = { value -> buildElements { handler(value) } }
}
}
fun <P : Props> child(
klazz: KClass<out Component<P, *>>,
handler: RHandler<P>,
) {
klazz.react(handler)
}
fun PropsWithChildren.children() {
childList.addAll(Children.toArray(children))
}
/**
* Current implementation doesn't solve any problem,
* it works in much the same way as a simple `forEach`
* */
@Deprecated(message = "Legacy API (use forEach)")
fun <T> Iterable<T>.renderEach(fn: RBuilder.(T) -> Unit) {
mapTo(childList) {
buildElement { fn(it) }
}
}
/**
* Current implementation doesn't solve any problem,
* it works in much the same way as a simple `forEachIndexed`
* */
@Deprecated(message = "Legacy API (use forEachIndexed)")
fun <T> Iterable<T>.renderEachIndexed(fn: RBuilder.(Int, T) -> Unit) {
mapIndexedTo(childList) { index, it ->
buildElement { fn(index, it) }
}
}
private fun ReactElement.deprecatedWithKey(newKey: Key) {
val index = childList.indexOf(this)
if (index >= 0) {
childList.removeAt(index)
val elementWithKey = cloneElement(this, jso { key = newKey })
childList.add(index, elementWithKey)
}
}
@Deprecated("Legacy API (use cloneElement)")
fun ReactElement.withKey(newKey: Key) = deprecatedWithKey(newKey)
@Deprecated("Legacy API (use cloneElement)")
fun ReactElement.withKey(newKey: Number) = deprecatedWithKey(newKey.toString())
}
@JsName("createBuilder")
fun RBuilder(): RBuilder =
RBuilderImpl()
inline fun <P : Props, reified C : Component<P, *>> RBuilder.child(
noinline handler: RHandler<P>,
) {
child(C::class, handler)
}
open class RBuilderImpl : RBuilder {
override val childList = mutableListOf<ReactNode>()
}
open class RBuilderMultiple : RBuilderImpl()
fun <T : RBuilder> buildElements(builder: T, handler: T.() -> Unit): dynamic {
val nodes = builder.apply(handler).childList
return when (nodes.size) {
0 -> null
1 -> nodes.first()
else -> createElement(Fragment, js {}, *nodes.toTypedArray())
}
}
fun buildElements(handler: Render): dynamic = buildElements(RBuilder(), handler)
open class RBuilderSingle : RBuilderImpl()
inline fun <T : RBuilder> buildElement(rBuilder: T, handler: T.() -> Unit): ReactElement =
rBuilder.apply(handler)
.childList.first()
.unsafeCast<ReactElement>()
inline fun buildElement(handler: Render): ReactElement =
buildElement(RBuilder(), handler)
interface RElementBuilder<out P : Props> : RBuilder {
val attrs: P
fun attrs(handler: P.() -> Unit) {
attrs.handler()
}
var key: Key
@Deprecated(message = "Write-only property", level = DeprecationLevel.HIDDEN)
get() = error("")
set(value) {
attrs.key = value
}
var ref: Ref<*>
@Deprecated(message = "Write-only property", level = DeprecationLevel.HIDDEN)
get() = error("")
set(value) {
attrs.ref = value
}
}
fun <P : Props> RElementBuilder(attrs: P): RElementBuilder<P> =
RElementBuilderImpl(attrs)
open class RElementBuilderImpl<out P : Props>(override val attrs: P) : RElementBuilder<P>, RBuilderImpl()
typealias RHandler<P> = RElementBuilder<P>.() -> Unit