Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

All RBuilder child declarations are extracted to separate file #509

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 0 additions & 89 deletions kotlin-react/src/main/kotlin/react/RBuilder.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,25 +25,6 @@ interface RBuilder {
childList.add(this)
}

fun <P : RProps> child(
type: Any,
props: P,
children: List<Any>
): ReactElement =
child(createElement(type, props, *children.toTypedArray()))

fun <P : RProps> child(
type: Any,
props: P,
handler: RHandler<P>
): ReactElement {
val children = with(RElementBuilder(props)) {
handler()
childList
}
return child(type, props, children)
}

operator fun <P : RProps> RClass<P>.invoke(
handler: RHandler<P>
): ReactElement =
Expand All @@ -70,30 +51,6 @@ interface RBuilder {
): ReactElement =
child(this, clone(props), children)

fun <P : RProps> child(
klazz: KClass<out Component<P, *>>,
handler: RHandler<P>
): ReactElement =
klazz.rClass.invoke(handler)

fun <T, P : RProps> childFunction(
klazz: KClass<out Component<P, *>>,
handler: RHandler<P>,
children: RBuilder.(T) -> Unit
): ReactElement =
child(
type = klazz.rClass,
props = RElementBuilder(jsObject<P>()).apply(handler).attrs,
children = listOf { value: T -> buildElement { children(value) } }
)

fun <P : RProps> node(
klazz: KClass<out Component<P, *>>,
props: P,
children: List<Any> = emptyList()
): ReactElement =
klazz.rClass.node(props, children)

fun RProps.children() {
childList.addAll(Children.toArray(children))
}
Expand Down Expand Up @@ -165,23 +122,6 @@ interface RBuilder {
fun RBuilder(): RBuilder =
RBuilderImpl()

inline fun <P : RProps, reified C : Component<P, *>> RBuilder.child(
noinline handler: RHandler<P>
): ReactElement =
child(C::class, handler)

inline fun <T, P : RProps, reified C : Component<P, *>> RBuilder.childFunction(
noinline handler: RHandler<P>,
noinline children: RBuilder.(T) -> Unit
): ReactElement =
childFunction(C::class, handler, children)

inline fun <P : RProps, reified C : Component<P, *>> RBuilder.node(
props: P,
children: List<Any> = emptyList()
): ReactElement =
node(C::class, props, children)

open class RBuilderImpl : RBuilder {
override val childList = mutableListOf<Any>()
}
Expand Down Expand Up @@ -273,32 +213,3 @@ fun <P : RProps> functionalComponent(
return fc.unsafeCast<FunctionComponent<P>>()
}

/**
* Append functional component [component] as child of current builder
*/
@Suppress("EXTENSION_SHADOWED_BY_MEMBER")
fun <P : RProps> RBuilder.child(
component: FC<P>,
props: P = jsObject(),
handler: RHandler<P> = {}
): ReactElement =
child(component, props, handler)

fun <T, P : RProps> RBuilder.childFunction(
component: FC<P>,
handler: RHandler<P> = {},
children: RBuilder.(T) -> Unit
): ReactElement =
childFunction(component, jsObject<P>(), handler, children)

fun <T, P : RProps> RBuilder.childFunction(
component: FC<P>,
props: P,
handler: RHandler<P> = {},
children: RBuilder.(T) -> Unit
): ReactElement =
child(
type = component,
props = RElementBuilder(props).apply(handler).attrs,
children = listOf { value: T -> buildElement { children(value) } }
)
98 changes: 98 additions & 0 deletions kotlin-react/src/main/kotlin/react/RBuilderChild.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package react

import kotlinext.js.jsObject
import kotlin.reflect.KClass

// Core

fun <P : RProps> RBuilder.child(
type: Any,
props: P,
children: List<Any>
): ReactElement =
child(createElement(type, props, *children.toTypedArray()))

fun <P : RProps> RBuilder.child(
type: Any,
props: P,
handler: RHandler<P>
): ReactElement {
val children = with(RElementBuilder(props)) {
handler()
childList
}
return child(type, props, children)
}

// KClass

fun <P : RProps> RBuilder.child(
klazz: KClass<out Component<P, *>>,
handler: RHandler<P>
): ReactElement =
klazz.rClass.invoke(handler)

fun <T, P : RProps> RBuilder.childFunction(
klazz: KClass<out Component<P, *>>,
handler: RHandler<P>,
children: RBuilder.(T) -> Unit
): ReactElement =
child(
type = klazz.rClass,
props = RElementBuilder(jsObject<P>()).apply(handler).attrs,
children = listOf { value: T -> buildElement { children(value) } }
)

fun <P : RProps> RBuilder.node(
klazz: KClass<out Component<P, *>>,
props: P,
children: List<Any> = emptyList()
): ReactElement =
klazz.rClass.node(props, children)

// Reified Component Type

inline fun <P : RProps, reified C : Component<P, *>> RBuilder.child(
noinline handler: RHandler<P>
): ReactElement =
child(C::class, handler)

inline fun <T, P : RProps, reified C : Component<P, *>> RBuilder.childFunction(
noinline handler: RHandler<P>,
noinline children: RBuilder.(T) -> Unit
): ReactElement =
childFunction(C::class, handler, children)

inline fun <P : RProps, reified C : Component<P, *>> RBuilder.node(
props: P,
children: List<Any> = emptyList()
): ReactElement =
node(C::class, props, children)

// Function Component

fun <P : RProps> RBuilder.child(
component: FC<P>,
props: P = jsObject(),
handler: RHandler<P> = {}
): ReactElement =
child(component.unsafeCast<Any>(), props, handler)

fun <T, P : RProps> RBuilder.childFunction(
component: FC<P>,
handler: RHandler<P> = {},
children: RBuilder.(T) -> Unit
): ReactElement =
childFunction(component, jsObject<P>(), handler, children)

fun <T, P : RProps> RBuilder.childFunction(
component: FC<P>,
props: P,
handler: RHandler<P> = {},
children: RBuilder.(T) -> Unit
): ReactElement =
child(
type = component,
props = RElementBuilder(props).apply(handler).attrs,
children = listOf { value: T -> buildElement { children(value) } }
)