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

Reduce ambiguous child API #622

Merged
merged 1 commit into from
Aug 12, 2021
Merged
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
42 changes: 19 additions & 23 deletions kotlin-react/src/main/kotlin/react/RBuilder.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,44 +26,52 @@ interface RBuilder {
fun <P : Props> child(
type: ElementType<P>,
props: P,
children: List<ReactNode>,
) {
child(createElement(type, props, *children.toTypedArray()))
child(createElement(type, props))
}

fun <P : Props> child(
type: ElementType<P>,
props: P,
handler: RHandler<P>,
) {
val props = jsObject<P>()
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(
handler: RHandler<P>,
) {
child(this, jsObject(), handler)
child(this, handler)
}

operator fun <T> Provider<T>.invoke(
value: T,
handler: RHandler<ProviderProps<T>>,
) {
child(this, jsObject { this.value = value }, handler)
child(this, handler = {
attrs.value = value
handler()
})
}

operator fun <T> Consumer<T>.invoke(
handler: RBuilder.(T) -> Unit,
) {
child(this, jsObject<ConsumerProps<T>> {
this.children = { value ->
buildElements { handler(value) }
}
}) {}
child(this, props = jsObject {
children = { value -> buildElements { handler(value) } }
})
}

@Deprecated(
Expand Down Expand Up @@ -176,7 +184,7 @@ inline fun <P : Props, reified C : Component<P, *>> RBuilder.node(
props: P,
children: List<ReactNode> = emptyList(),
) {
child(C::class.react, props, children)
child(createElement(C::class.react, props, *children.toTypedArray()))
}

open class RBuilderImpl : RBuilder {
Expand Down Expand Up @@ -234,15 +242,3 @@ fun <P : Props> RElementBuilder(attrs: P): RElementBuilder<P> =
open class RElementBuilderImpl<out P : Props>(override val attrs: P) : RElementBuilder<P>, RBuilderImpl()

typealias RHandler<P> = RElementBuilder<P>.() -> Unit

/**
* Append function component [component] as child of current builder
*/
@Suppress("EXTENSION_SHADOWED_BY_MEMBER")
fun <P : Props> RBuilder.child(
component: FC<P>,
props: P = jsObject(),
handler: RHandler<P> = {},
) {
child(component, props, handler)
}
2 changes: 1 addition & 1 deletion kotlin-styled/src/main/kotlin/styled/StyledComponents.kt
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ private external interface GlobalStylesComponentProps : RProps {
private object GlobalStyles {
private val component = fc<GlobalStylesComponentProps> { props ->
props.globalStyles.forEach {
child(it, jsObject {}, emptyList())
child(it, props = jsObject {})
}
}

Expand Down