forked from JetBrains/kotlin-wrappers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTodo.kt
85 lines (75 loc) · 2.32 KB
/
Todo.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
package example
import kotlinx.html.*
import kotlinx.html.js.*
import org.w3c.dom.*
import react.*
import react.dom.*
/**
* A to-do list implementation by [email protected]
*/
interface TodoProps : RProps {
var initialItems: List<String?>
}
interface TodoState : RState {
var items: List<String?>
var text: String
}
class Todo(props: TodoProps) : RComponent<TodoProps, TodoState>(props) {
override fun TodoState.init(props: TodoProps) {
items = props.initialItems
text = ""
}
override fun RBuilder.render() {
div {
input(type = InputType.text, name = "itemText") {
key = "itemText"
attrs {
value = state.text
placeholder = "Add a to-do item"
onChangeFunction = {
val target = it.target as HTMLInputElement
setState {
text = target.value
}
}
}
}
button {
+"Add"
attrs {
onClickFunction = {
if (state.text.isNotEmpty()) {
setState {
items += text
text = ""
}
}
}
}
}
h3 {
ul {
for ((index, item) in state.items.withIndex()) {
li {
key = index.toString()
+item.toString()
button {
+"×"
attrs {
onClickFunction = {
setState {
items = items.filterIndexed { i, _ -> i != index }
}
}
}
}
}
}
}
}
}
}
}
fun RBuilder.todo(items: List<String?> = listOf("Hello", "World")) = child(Todo::class) {
attrs.initialItems = items
}