generated from mpetuska/template-kmp-library
-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathMDCDialog.kt
134 lines (128 loc) · 4.21 KB
/
MDCDialog.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
package showcases
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import dev.petuska.katalog.runtime.Showcase
import dev.petuska.katalog.runtime.layout.InteractiveShowcase
import dev.petuska.katalog.runtime.util.LoremIpsum
import dev.petuska.kmdc.dialog.*
import dev.petuska.kmdc.list.MDCList
import dev.petuska.kmdc.list.MDCListSelection
import dev.petuska.kmdc.list.MDCListType
import dev.petuska.kmdc.list.item.Label
import dev.petuska.kmdc.list.item.ListItem
import dev.petuska.kmdc.list.item.RadioGraphic
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.control.TextControl
import sandbox.util.NamedBlock
import sandbox.util.NamedGroup
private class MDCDialogVM {
var fullscreen by mutableStateOf(false)
var stacked by mutableStateOf(false)
var scrimClickAction by mutableStateOf("close")
var escapeKeyAction by mutableStateOf("close")
var simpleOpen by mutableStateOf(false)
var interactiveOpen by mutableStateOf(false)
val ringtones = listOf("None", "Callisto", "Ganymede", "Luna", "Oberon", "Phobos")
var selected by mutableStateOf<String?>(null)
}
@Composable
@Showcase(id = "MDCDialog")
fun MDCDialog() = InteractiveShowcase(
viewModel = { MDCDialogVM() },
controls = {
BooleanControl("Fullscreen", ::fullscreen)
BooleanControl("Stacked", ::stacked)
TextControl("Scrim Click Action", ::scrimClickAction)
TextControl("Escape Click Action", ::escapeKeyAction)
NamedGroup("Simple") {
BooleanControl("Open", ::simpleOpen)
}
NamedGroup("Interactive") {
BooleanControl("Open", ::interactiveOpen)
}
},
) {
NamedBlock("Simple") {
MDCDialog(
open = simpleOpen,
fullscreen = fullscreen,
stacked = stacked,
scrimClickAction = scrimClickAction.takeIf(String::isNotBlank) ?: "close",
escapeKeyAction = escapeKeyAction.takeIf(String::isNotBlank) ?: "close",
attrs = {
registerEvents()
onOpening { interactiveOpen = false }
onClosed { simpleOpen = false }
}
) {
Header {
Title("Dialog Title")
if (fullscreen) CloseButton(MDCIcon.Close.type, attrs = { mdcIcon() })
}
Content {
Text(LoremIpsum)
}
Actions {
Action(action = "accept", text = "Accept")
Action(action = "ok", text = "OK")
Action(action = "custom", text = "Custom", default = true)
Action(action = "close", text = "Close")
}
}
}
NamedBlock("Interactive") {
MDCDialog(
open = interactiveOpen,
fullscreen = fullscreen,
stacked = stacked,
scrimClickAction = scrimClickAction.takeIf(String::isNotBlank) ?: "close",
escapeKeyAction = escapeKeyAction.takeIf(String::isNotBlank) ?: "close",
attrs = {
registerEvents()
onOpening { simpleOpen = false }
onClosed {
interactiveOpen = false
selected = it.detail.action?.takeIf { a -> a in ringtones } ?: selected
}
}
) {
Title("Choose a Ringtone")
Content {
MDCList(
type = MDCListType.Avatar,
selection = MDCListSelection.SingleRadio,
) {
ringtones.forEachIndexed { index, item ->
ListItem(
attrs = {
mdcDialogAction(item)
if (selected == item || selected == null && index == 0) {
mdcDialogInitialFocus()
}
}
) {
val id = "mdc-dialog-item-$index"
RadioGraphic(checked = selected == item, id = id)
Label(text = item, forId = id)
}
}
}
}
Actions {
Action(action = "close", text = "Cancel")
Action(action = "accept", text = "OK")
}
}
}
}
private fun MDCDialogAttrsScope.registerEvents() {
onOpening { console.log("MDCDialog#onOpening", it.detail) }
onOpened { console.log("MDCDialog#onOpened", it.detail) }
onClosing { console.log("MDCDialog#onClosing", it.detail) }
onClosed { console.log("MDCDialog#onClosed", it.detail) }
}