generated from mpetuska/template-kmp-library
-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathMDCTextField.kt
110 lines (103 loc) · 3.53 KB
/
MDCTextField.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
package showcases
import androidx.compose.runtime.*
import dev.petuska.katalog.runtime.Showcase
import dev.petuska.katalog.runtime.layout.InteractiveShowcase
import dev.petuska.kmdc.textfield.MDCTextArea
import dev.petuska.kmdc.textfield.MDCTextField
import dev.petuska.kmdc.textfield.MDCTextFieldType
import dev.petuska.kmdc.textfield.icon.MDCTextFieldLeadingIcon
import dev.petuska.kmdc.textfield.icon.MDCTextFieldTrailingIcon
import dev.petuska.kmdcx.icons.MDCIcon
import dev.petuska.kmdcx.icons.mdcIcon
import org.jetbrains.compose.web.attributes.InputType
import org.jetbrains.compose.web.dom.Text
import sandbox.control.BooleanControl
import sandbox.control.ChoiceControl
import sandbox.control.RangeControl
import sandbox.control.TextControl
import sandbox.util.NamedBlock
import sandbox.util.NamedGroup
private class MDCTextFieldVM {
var type by mutableStateOf(MDCTextFieldType.Filled)
var disabled by mutableStateOf(false)
var password by mutableStateOf(false)
var label by mutableStateOf("")
var helperText by mutableStateOf("")
var maxLength by mutableStateOf(50)
var leadingIcon by mutableStateOf(false)
var trailingIcon by mutableStateOf(false)
var prefix by mutableStateOf("")
var suffix by mutableStateOf("")
var rows by mutableStateOf(8)
var columns by mutableStateOf(40)
}
@Composable
@Showcase(id = "MDCTextField")
fun MDCTextField() = InteractiveShowcase(
viewModel = { MDCTextFieldVM() },
controls = {
ChoiceControl("Type", MDCTextFieldType.entries.associateBy(MDCTextFieldType::name), ::type)
BooleanControl("Password", ::password)
BooleanControl("Disabled", ::disabled)
TextControl("Label", ::label)
TextControl("Helper Text", ::helperText)
RangeControl("Max Length", ::maxLength, converter = Number::toInt)
NamedGroup("Field") {
TextControl("Prefix", ::prefix)
TextControl("Suffix", ::suffix)
BooleanControl("Leading Icon", ::leadingIcon)
BooleanControl("Trailing Icon", ::trailingIcon)
}
NamedGroup("Area") {
RangeControl("Rows", ::rows, min = 1, max = 16, converter = Number::toInt)
RangeControl("Columns", ::columns, min = 1, max = 80, converter = Number::toInt)
}
},
) {
var text by remember { mutableStateOf("") }
NamedBlock("Field", attrs = { style { property("width", "fit-content") } }) {
MDCTextField(
value = text,
type = type,
inputType = if (password) InputType.Password else InputType.Text,
disabled = disabled,
label = label.takeIf(String::isNotBlank),
helperText = helperText.takeIf(String::isNotBlank),
maxLength = maxLength.toUInt(),
prefix = prefix.takeIf(String::isNotBlank),
suffix = suffix.takeIf(String::isNotBlank),
attrs = {
onInput { text = it.value }
},
leadingIcon = if (leadingIcon) {
{
MDCTextFieldLeadingIcon(attrs = { mdcIcon() }) { Text(MDCIcon.Phone.type) }
}
} else {
null
},
trailingIcon = if (trailingIcon) {
{
MDCTextFieldTrailingIcon(attrs = { mdcIcon() }) { Text(MDCIcon.Event.type) }
}
} else {
null
}
)
}
NamedBlock("Area", attrs = { style { property("width", "fit-content") } }) {
MDCTextArea(
value = text,
type = type,
disabled = disabled,
label = label.takeIf(String::isNotBlank),
helperText = helperText.takeIf(String::isNotBlank),
maxLength = maxLength.toUInt(),
rows = rows.toUInt(),
columns = columns.toUInt(),
attrs = {
onInput { text = it.value }
}
)
}
}