generated from mpetuska/template-kmp-library
-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathMDCChips.kt
143 lines (138 loc) · 4.07 KB
/
MDCChips.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
package showcases
import androidx.compose.runtime.*
import dev.petuska.katalog.runtime.Showcase
import dev.petuska.katalog.runtime.layout.InteractiveShowcase
import dev.petuska.kmdc.chips.MDCChipsAttrsScope
import dev.petuska.kmdc.chips.action.Checkmark
import dev.petuska.kmdc.chips.action.Graphic
import dev.petuska.kmdc.chips.action.Icon
import dev.petuska.kmdc.chips.action.Label
import dev.petuska.kmdc.chips.grid.*
import dev.petuska.kmdc.chips.listbox.FilterChip
import dev.petuska.kmdc.chips.listbox.MDCChipsListbox
import dev.petuska.kmdc.chips.onInteraction
import dev.petuska.kmdc.chips.onRemoval
import dev.petuska.kmdc.chips.onSelection
import dev.petuska.kmdcx.icons.MDCIcon
import dev.petuska.kmdcx.icons.mdcIcon
import org.jetbrains.compose.web.dom.Text
import sandbox.control.BooleanControl
import sandbox.util.NamedBlock
private class MDCChipsVM {
var disabled by mutableStateOf(false)
var multiselectable by mutableStateOf(false)
var overflow by mutableStateOf(false)
var touch by mutableStateOf(false)
var removed1 by mutableStateOf(false)
var removed2 by mutableStateOf(false)
}
@Composable
@Showcase(id = "MDCChips")
fun MDCChips() = InteractiveShowcase(
viewModel = { MDCChipsVM() },
controls = {
BooleanControl("Disabled", ::disabled)
BooleanControl("Multiselectable", ::multiselectable)
BooleanControl("Overflow", ::overflow)
BooleanControl("Touch", ::touch)
},
) {
NamedBlock("Grid") {
MDCChipsGrid(overflow = overflow, attrs = {
registerEvents()
onRemoval {
if (it.detail.chipID == "1") removed1 = true
if (it.detail.chipID == "2") removed2 = true
}
}) {
ActionChip(
"0",
disabled = disabled,
withPrimaryIcon = true,
touch = touch,
) {
Graphic {
Icon(attrs = { mdcIcon() }) { Text(MDCIcon.Favorite.type) }
Checkmark()
}
Label("ActionChip")
}
if (!removed1) {
InputChip(
"1",
withPrimaryIcon = true,
withTrailingAction = true,
disabled = disabled,
touch = touch,
) {
PrimaryAction {
Graphic {
Icon(attrs = { mdcIcon() }) { Text(MDCIcon.Favorite.type) }
Checkmark()
}
Label("InputChip with leading icon and trailing action")
}
TrailingAction {
Icon(attrs = { mdcIcon() }) { Text(MDCIcon.Close.type) }
}
}
}
if (!removed2) {
InputChip(
"2",
disabled = disabled,
withTrailingAction = true,
navigableTrailingAction = false,
touch = touch,
) {
PrimaryAction {
Label("InputChip with non-navigable trailing action")
}
TrailingAction {
Icon(attrs = { mdcIcon() }) { Text(MDCIcon.Close.type) }
}
}
}
InputChip("3", disabled = disabled, touch = touch) {
PrimaryAction {
Label("InputChip without trailing action")
}
}
}
}
NamedBlock("Listbox") {
val selected = remember { mutableStateListOf("1") }
MDCChipsListbox(overflow = overflow, multiselectable = multiselectable, attrs = {
registerEvents()
onSelection {
if (it.detail.isSelected) {
selected.add(it.detail.chipID)
} else {
selected.remove(it.detail.chipID)
}
}
}) {
repeat(5) { id ->
FilterChip(
"$id",
selected = "$id" in selected,
disabled = disabled,
withPrimaryGraphic = true,
withPrimaryIcon = true,
touch = touch,
) {
Graphic {
Icon(attrs = { mdcIcon() }) { Text(MDCIcon.Favorite.type) }
Checkmark()
}
Label("FilterChip - $id")
}
}
}
}
}
private fun MDCChipsAttrsScope.registerEvents() {
onInteraction { console.log("MDCChips#onInteraction", it.detail) }
onSelection { console.log("MDCChips#onSelection", it.detail) }
onRemoval { console.log("MDCChips#onRemoval", it.detail) }
}