-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathChipView.kt
More file actions
128 lines (114 loc) · 4.32 KB
/
Copy pathChipView.kt
File metadata and controls
128 lines (114 loc) · 4.32 KB
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
package expo.modules.jetpackcomposereactnative.views.chip
import android.content.Context
import android.view.ViewGroup.LayoutParams.WRAP_CONTENT
import androidx.compose.material3.*
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.ui.platform.ComposeView
import androidx.compose.ui.Modifier
import expo.modules.jetpackcomposereactnative.common.ModifierProp
import expo.modules.jetpackcomposereactnative.common.toModifier
import expo.modules.jetpackcomposereactnative.views.icon.IconComposable
import expo.modules.jetpackcomposereactnative.views.icon.IconProps
import expo.modules.kotlin.AppContext
import expo.modules.kotlin.viewevent.EventDispatcher
import expo.modules.kotlin.viewevent.ViewEventCallback
import expo.modules.kotlin.views.ExpoView
data class ChipProps(
var labelText: String = "",
var selected: Boolean = false,
var leadingIcon: String? = null,
var trailingIcon: String? = null,
var modifier: ModifierProp = emptyList(),
var variant: String = "assist",
var enabled: Boolean = true
)
class ChipView(context: Context, appContext: AppContext) : ExpoView(context, appContext) {
private var props = mutableStateOf(ChipProps())
private val onChipClick by EventDispatcher()
init {
ComposeView(context).also {
it.layoutParams = LayoutParams(WRAP_CONTENT, WRAP_CONTENT) // Allow the content to wrap
it.setContent {
ChipComposable(props = props.value, onClick = onChipClick)
}
addView(it)
}
}
fun updateLabelText(labelText: String) {
props.value = props.value.copy(labelText = labelText)
}
fun updateModifier(modifier: ModifierProp) {
props.value = props.value.copy(modifier = modifier)
}
fun updateVariant(variant: String) {
props.value = props.value.copy(variant = variant)
}
fun updateTrailingIcon(trailingIcon: String?) {
props.value = props.value.copy(trailingIcon = trailingIcon)
}
fun updateLeadingIcon(leadingIcon: String?) {
props.value = props.value.copy(leadingIcon = leadingIcon)
}
fun updateSelected(selected: Boolean) {
props.value = props.value.copy(selected = selected)
}
fun updateEnabled(enabled: Boolean) {
props.value = props.value.copy(enabled = enabled)
}
}
@Composable
fun ChipComposable(props: ChipProps, onClick: ViewEventCallback<Map<String, Any>>) {
val modifier: Modifier = props.modifier.toModifier()
val labelContent: @Composable () -> Unit = { Text(text = props.labelText) }
val leadingIconContent: @Composable (() -> Unit)? = props.leadingIcon?.let {
{ IconComposable(props = IconProps(name = it)) }
}
val trailingIconContent: @Composable (() -> Unit)? = props.trailingIcon?.let {
{ IconComposable(props = IconProps(name = it)) }
}
val onClickAction = { onClick(mapOf()) }
when (props.variant.lowercase()) {
"assist" -> AssistChip(
label = labelContent,
leadingIcon = leadingIconContent,
trailingIcon = trailingIconContent,
enabled = props.enabled,
onClick = onClickAction,
modifier = modifier
)
"filter" -> FilterChip(
label = labelContent,
leadingIcon = leadingIconContent,
trailingIcon = trailingIconContent,
selected = props.selected,
enabled = props.enabled,
onClick = onClickAction,
modifier = modifier
)
"input" -> InputChip(
label = labelContent,
leadingIcon = leadingIconContent,
trailingIcon = trailingIconContent,
selected = props.selected,
enabled = props.enabled,
onClick = onClickAction,
modifier = modifier
)
"suggestion" -> SuggestionChip(
label = labelContent,
icon = leadingIconContent,
onClick = onClickAction,
enabled = props.enabled,
modifier = modifier
)
else -> AssistChip(
label = labelContent,
leadingIcon = leadingIconContent,
trailingIcon = trailingIconContent,
enabled = props.enabled,
onClick = onClickAction,
modifier = modifier
)
}
}