Skip to content

Commit 773e777

Browse files
SebastianAignerJSMonk
authored andcommitted
Update to new Kotlin-React DSL
1 parent 28ad43f commit 773e777

File tree

3 files changed

+25
-22
lines changed

3 files changed

+25
-22
lines changed

src/jsMain/kotlin/App.kt

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import react.*
2-
import react.dom.*
3-
import kotlinx.html.js.*
42
import kotlinx.coroutines.*
3+
import react.dom.html.ReactHTML.h1
4+
import react.dom.html.ReactHTML.li
5+
import react.dom.html.ReactHTML.ul
56

67
private val scope = MainScope()
78

8-
val app = fc<Props> {
9+
val App = FC<Props> {
910
var shoppingList by useState(emptyList<ShoppingListItem>())
1011

1112
useEffectOnce {
@@ -21,7 +22,7 @@ val app = fc<Props> {
2122
shoppingList.sortedByDescending(ShoppingListItem::priority).forEach { item ->
2223
li {
2324
key = item.toString()
24-
attrs.onClickFunction = {
25+
onClick = {
2526
scope.launch {
2627
deleteShoppingListItem(item)
2728
shoppingList = getShoppingList()
@@ -31,8 +32,8 @@ val app = fc<Props> {
3132
}
3233
}
3334
}
34-
child(inputComponent) {
35-
attrs.onSubmit = { input ->
35+
InputComponent {
36+
onSubmit = { input ->
3637
val cartItem = ShoppingListItem(input.replace("!", ""), input.count { it == '!' })
3738
scope.launch {
3839
addShoppingListItem(cartItem)

src/jsMain/kotlin/InputComponent.kt

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,35 @@
1+
import org.w3c.dom.HTMLFormElement
12
import react.*
2-
import react.dom.*
3-
import kotlinx.html.js.*
4-
import kotlinx.html.InputType
5-
import org.w3c.dom.events.Event
63
import org.w3c.dom.HTMLInputElement
4+
import react.dom.events.ChangeEventHandler
5+
import react.dom.events.FormEventHandler
6+
import react.dom.html.InputType
7+
import react.dom.html.ReactHTML.form
8+
import react.dom.html.ReactHTML.input
79

810
external interface InputProps : Props {
911
var onSubmit: (String) -> Unit
1012
}
1113

12-
val inputComponent = fc<InputProps> { props ->
14+
val InputComponent = FC<InputProps> { props ->
1315
val (text, setText) = useState("")
1416

15-
val submitHandler: (Event) -> Unit = {
17+
val submitHandler: FormEventHandler<HTMLFormElement> = {
1618
it.preventDefault()
1719
setText("")
1820
props.onSubmit(text)
1921
}
2022

21-
val changeHandler: (Event) -> Unit = {
22-
val value = (it.target as HTMLInputElement).value
23-
setText(value)
23+
val changeHandler: ChangeEventHandler<HTMLInputElement> = {
24+
setText(it.target.value)
2425
}
2526

2627
form {
27-
attrs.onSubmitFunction = submitHandler
28-
input(InputType.text) {
29-
attrs.onChangeFunction = changeHandler
30-
attrs.value = text
28+
onSubmit = submitHandler
29+
input {
30+
type = InputType.text
31+
onChange = changeHandler
32+
value = text
3133
}
3234
}
3335
}

src/jsMain/kotlin/Main.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import react.dom.render
22
import kotlinx.browser.document
3+
import react.create
34

45
fun main() {
5-
render(document.getElementById("root")) {
6-
child(app)
7-
}
6+
val container = document.getElementById("root") ?: error("Couldn't find container!")
7+
render(App.create(), container)
88
}

0 commit comments

Comments
 (0)