forked from JetBrains/kotlin-wrappers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Product.kt
166 lines (145 loc) · 4.62 KB
/
Product.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package example
/**
* A thinking-in-react implementation by [email protected] ([email protected])
* This is a port of https://reactjs.org/docs/thinking-in-react.html
*
* Date: Nov 24, 2017
*/
import kotlinext.js.*
import kotlinx.html.*
import kotlinx.html.js.*
import org.w3c.dom.*
import react.*
import react.dom.*
fun RBuilder.productCategoryRow(category: String) {
tr {
th {
attrs.colSpan = "2"
+category
}
}
}
fun RBuilder.productRow(name: String, price: Double, isStocked: Boolean) {
tr {
td {
if (isStocked)
+name
else
span {
attrs.jsStyle = js {
color = "red"
}
+name
}
}
td {
+"$$price"
}
}
}
fun RBuilder.productTable(products: Array<Product>, filterText: String, inStockOnly: Boolean) {
var lastCategory = ""
table {
thead { tr { th { +"Name" }; th { +"Price" } } }
tbody {
products.forEach {
//show the category name
if (it.category != lastCategory) {
productCategoryRow(it.category)
}
lastCategory = it.category
//show the product per filter text and inStock flag
if (!(
(filterText.isNotEmpty() and !it.name.toUpperCase().contains(filterText.toUpperCase())) or
(inStockOnly and !it.isStocked))
) {
productRow(it.name, it.price, it.isStocked)
}
}
}
}
}
interface SearchBarProps : RProps {
var onClick: () -> Unit
var onChange: (String) -> () -> Unit
var inStockOnly: Boolean
var filterText: String
}
class SearchBar(props: SearchBarProps) : RComponent<SearchBarProps, RState>(props) {
override fun RBuilder.render() {
div {
input(type = InputType.text, name = "filterText") {
attrs {
value = props.filterText
placeholder = "Search products"
onChangeFunction = {
val target = it.target as HTMLInputElement
props.onChange(target.value)()
}
}
}
p {
input(type = InputType.checkBox, name = "showInstockOnly") {
attrs {
value = if (props.inStockOnly) "1" else "0"
onClickFunction = {
props.onClick()
}
}
}
+"Show in-stock products only"
}
}
}
}
fun RBuilder.searchBar(initialInStockOnly: Boolean,
initialFilterText: String,
handleFilterInputChange: (String) -> () -> Unit,
handleInStockInputClick: () -> () -> Unit
) = child(SearchBar::class) {
attrs.onClick = handleInStockInputClick()
attrs.onChange = handleFilterInputChange
attrs.inStockOnly = initialInStockOnly
attrs.filterText = initialFilterText
}
interface ProductProps : RProps {
}
interface ProductState : RState {
var filterText: String
var inStockOnly: Boolean
}
data class Product(val category: String, val price: Double, val isStocked: Boolean, val name: String)
var products = arrayOf(
Product("Sporting Goods", 49.9, true, "Football"),
Product("Sporting Goods", 9.9, true, "Baseball"),
Product("Sporting Goods", 29.9, false, "Basketball"),
Product("Electronics", 99.9, true, "iPod Touch"),
Product("Electronics", 999.9, false, "iPhone X"),
Product("Electronics", 199.9, true, "Nexus")
)
class ProductComponent(props: ProductProps) : RComponent<ProductProps, ProductState>(props) {
override fun ProductState.init(props: ProductProps) {
filterText = ""
inStockOnly = false
}
private fun handleFilterInputChange(targetValue: String) = {
setState {
filterText = targetValue
}
}
private fun handleInStockInputClick() = {
setState {
inStockOnly = !inStockOnly
}
}
override fun RBuilder.render() {
div {
searchBar(state.inStockOnly, state.filterText,
{ filterText: String -> handleFilterInputChange(filterText) },
{ handleInStockInputClick() })
productTable(products, state.filterText, state.inStockOnly)
}
}
}
fun RBuilder.product() = child(ProductComponent::class) {
}