generated from mpetuska/template-kmp-library
-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathMDCMenu.kt
201 lines (194 loc) · 6.09 KB
/
MDCMenu.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package showcases
import androidx.compose.runtime.*
import dev.petuska.katalog.runtime.Showcase
import dev.petuska.katalog.runtime.layout.InteractiveShowcase
import dev.petuska.kmdc.core.KMDCInternalAPI
import dev.petuska.kmdc.core.domain.Point
import dev.petuska.kmdc.core.rememberMutableStateOf
import dev.petuska.kmdc.list.item.CheckboxGraphic
import dev.petuska.kmdc.list.item.Label
import dev.petuska.kmdc.list.item.Text
import dev.petuska.kmdc.menu.*
import dev.petuska.kmdc.menu.surface.*
import dev.petuska.kmdc.textfield.MDCTextField
import dev.petuska.kmdc.textfield.MDCTextFieldType
import dev.petuska.kmdc.textfield.icon.MDCTextFieldTrailingIcon
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.ChoiceControl
import sandbox.util.NamedBlock
import sandbox.util.NamedGroup
private class MDCMenuVM {
var anchorCorner by mutableStateOf(Corner.BOTTOM_START)
var defaultFocusState by mutableStateOf(DefaultFocusState.NONE)
var disabled by mutableStateOf(false)
var fullWidth by mutableStateOf(true)
var fixed by mutableStateOf(false)
var quickOpen by mutableStateOf(false)
var hoisted by mutableStateOf(false)
var wrapFocus by mutableStateOf(false)
var open by mutableStateOf(false)
var selectedIds = mutableStateListOf<Int>()
var selectedId by mutableStateOf<Int?>(null)
val menuItems = listOf("Java", "Kotlin", "Scala", "Groovy", "JavaScript", "TypeScript")
}
@Composable
@Showcase(id = "MDCMenu")
fun MDCMenu() = InteractiveShowcase(
viewModel = { MDCMenuVM() },
controls = {
ChoiceControl(
title = "Anchor Corner",
options = mapOf(
"TOP_LEFT" to Corner.TOP_LEFT,
"TOP_RIGHT" to Corner.TOP_RIGHT,
"BOTTOM_LEFT" to Corner.BOTTOM_LEFT,
"BOTTOM_RIGHT" to Corner.BOTTOM_RIGHT,
"TOP_START" to Corner.TOP_START,
"TOP_END" to Corner.TOP_END,
"BOTTOM_START" to Corner.BOTTOM_START,
"BOTTOM_END" to Corner.BOTTOM_END,
),
selected = ::anchorCorner
)
ChoiceControl(
title = "Default Focus State",
options = mapOf(
"NONE" to DefaultFocusState.NONE,
"LIST_ROOT" to DefaultFocusState.LIST_ROOT,
"FIRST_ITEM" to DefaultFocusState.FIRST_ITEM,
"LAST_ITEM" to DefaultFocusState.LAST_ITEM,
),
selected = ::defaultFocusState
)
BooleanControl("Disabled", ::disabled)
BooleanControl("Full Width", ::fullWidth)
BooleanControl("Fixed", ::fixed)
BooleanControl("Quick Open", ::quickOpen)
BooleanControl("Hoisted", ::hoisted)
BooleanControl("Wrap Focus", ::wrapFocus)
NamedGroup("Absolute") {
BooleanControl("Open", ::open)
}
},
) {
NamedBlock("Absolute | multi-select") {
MDCMenu(
open = open,
selectedIds = selectedIds,
fullWidth = fullWidth,
fixed = fixed,
anchorCorner = anchorCorner,
quickOpen = quickOpen,
hoisted = hoisted,
wrapFocus = wrapFocus,
defaultFocusState = defaultFocusState,
absolutePosition = Point(0.0, 0.0),
attrs = {
registerEvents()
onClosed { open = false }
onSelected {
val id = it.detail.index
if (id in selectedIds) selectedIds.remove(id) else selectedIds.add(id)
}
}
) {
menuItems.chunked(2).forEachIndexed { i1, items ->
SelectionGroup {
items.forEachIndexed { i2, text ->
MenuItem(
disabled = disabled,
) {
val id = "mdc-menu-item-${i1 * 2 + i2}"
CheckboxGraphic(checked = i1 * 2 + i2 in selectedIds, id = id)
Label(text = text, forId = id)
}
}
}
}
}
}
@OptIn(KMDCInternalAPI::class)
NamedBlock("Anchored | single-select") {
var open by rememberMutableStateOf(false)
var input by rememberMutableStateOf("")
MDCMenuSurfaceAnchor(attrs = {
style {
property("width", "fit-content")
}
}) {
MDCTextField(
value = input,
label = "Favourite Language",
type = MDCTextFieldType.Outlined,
attrs = {
onFocus { open = true }
onClick { open = true }
onInput {
input = it.value
open = true
}
onChange { e ->
val candidate = menuItems.firstOrNull { it.equals(e.value, true) }
?: menuItems.filter { it.contains(e.value, true) }.takeIf { it.size == 1 }?.first()
selectedId = candidate?.let(menuItems::indexOf) ?: selectedId
input = selectedId?.let(menuItems::get) ?: ""
e.target.blur()
open = false
}
},
trailingIcon = {
MDCTextFieldTrailingIcon(clickable = true, attrs = {
onClick { open = !open }
mdcIcon()
}) {
Text(
if (open) {
MDCIcon.ArrowDropUp.type
} else {
MDCIcon.ArrowDropDown.type
}
)
}
}
)
MDCMenu(
open = open,
selectedId = selectedId,
fullWidth = fullWidth,
fixed = fixed,
anchorCorner = anchorCorner,
quickOpen = quickOpen,
hoisted = hoisted,
wrapFocus = wrapFocus,
defaultFocusState = defaultFocusState,
attrs = {
registerEvents()
onSelected {
selectedId = it.detail.index
input = menuItems[it.detail.index]
open = false
}
onClosed { open = false }
}
) {
menuItems.forEach {
MenuItem(
disabled = disabled,
activated = input.isNotBlank() && it.contains(input, ignoreCase = true)
) {
Text(it)
}
}
}
}
}
}
private fun MDCMenuAttrsScope.registerEvents() {
onSelected { console.log("MDCMenu#onSelected", it.detail) }
onOpened { console.log("MDCMenu#onOpened", it.detail) }
onClosing { console.log("MDCMenu#onClosing", it.detail) }
onClosed { console.log("MDCMenu#onClosed", it.detail) }
}