From d56cc3845134037a5d3d3f9cd149d85b786cb639 Mon Sep 17 00:00:00 2001 From: Joffrey Bion Date: Fri, 2 Aug 2019 01:19:57 +0200 Subject: First connected component --- .../ui/components/home/ChooseNameForm.kt | 41 ++++++++++++++++++++++ .../luxons/sevenwonders/ui/components/home/Home.kt | 4 +-- .../org/luxons/sevenwonders/ui/redux/Utils.kt | 21 +++++++++++ 3 files changed, 63 insertions(+), 3 deletions(-) create mode 100644 sw-ui-kt/src/main/kotlin/org/luxons/sevenwonders/ui/components/home/ChooseNameForm.kt create mode 100644 sw-ui-kt/src/main/kotlin/org/luxons/sevenwonders/ui/redux/Utils.kt (limited to 'sw-ui-kt/src/main/kotlin/org') diff --git a/sw-ui-kt/src/main/kotlin/org/luxons/sevenwonders/ui/components/home/ChooseNameForm.kt b/sw-ui-kt/src/main/kotlin/org/luxons/sevenwonders/ui/components/home/ChooseNameForm.kt new file mode 100644 index 00000000..8bf43f1d --- /dev/null +++ b/sw-ui-kt/src/main/kotlin/org/luxons/sevenwonders/ui/components/home/ChooseNameForm.kt @@ -0,0 +1,41 @@ +package org.luxons.sevenwonders.ui.components.home + +import kotlinx.html.InputType +import kotlinx.html.js.onChangeFunction +import kotlinx.html.js.onSubmitFunction +import org.luxons.sevenwonders.ui.redux.ChooseUserName +import org.luxons.sevenwonders.ui.redux.connectDispatch +import react.RBuilder +import react.RClass +import react.RComponent +import react.RProps +import react.RState +import react.dom.* + +private interface ChooseNameFormProps: RProps { + var chooseUsername: (String) -> Unit +} + +private data class ChooseNameFormState(val username: String): RState + +private class ChooseNameForm(props: ChooseNameFormProps): RComponent(props) { + + override fun RBuilder.render() { + form { + attrs.onSubmitFunction = { props.chooseUsername(state.username) } + + input(type = InputType.text) { + attrs { + value = state.username + onChangeFunction = { + setState(transformState = { ChooseNameFormState(value) }) + } + } + } + } + } +} + +val chooseNameForm: RClass = connectDispatch(ChooseNameForm::class) { dispatch, _ -> + chooseUsername = { name -> dispatch(ChooseUserName(name)) } +} diff --git a/sw-ui-kt/src/main/kotlin/org/luxons/sevenwonders/ui/components/home/Home.kt b/sw-ui-kt/src/main/kotlin/org/luxons/sevenwonders/ui/components/home/Home.kt index abbd84a9..458dc7c4 100644 --- a/sw-ui-kt/src/main/kotlin/org/luxons/sevenwonders/ui/components/home/Home.kt +++ b/sw-ui-kt/src/main/kotlin/org/luxons/sevenwonders/ui/components/home/Home.kt @@ -16,7 +16,5 @@ fun RBuilder.home() = styledDiv { img(src = LOGO, alt = "Seven Wonders") {} - p { - +"Great app!" - } + chooseNameForm {} } diff --git a/sw-ui-kt/src/main/kotlin/org/luxons/sevenwonders/ui/redux/Utils.kt b/sw-ui-kt/src/main/kotlin/org/luxons/sevenwonders/ui/redux/Utils.kt new file mode 100644 index 00000000..e40e11bc --- /dev/null +++ b/sw-ui-kt/src/main/kotlin/org/luxons/sevenwonders/ui/redux/Utils.kt @@ -0,0 +1,21 @@ +package org.luxons.sevenwonders.ui.redux + +import react.RClass +import react.RComponent +import react.RProps +import react.RState +import react.invoke +import react.redux.rConnect +import redux.RAction +import redux.WrapperAction +import kotlin.reflect.KClass + +inline fun connectDispatch( + clazz: KClass>, + crossinline mapDispatchToProps: T.((RAction) -> WrapperAction, RProps) -> Unit +): RClass { + val connect = rConnect(mapDispatchToProps = { dispatch, ownProps -> + mapDispatchToProps(dispatch, ownProps) + }) + return connect.invoke(clazz.js.unsafeCast>()) +} -- cgit