@Composable
actual fun DropdownSelector(
modifier: Modifier,
selectedIndex: Int,
items: List<String>,
label: String,
errorRes: StringResource?,
onChange: (Int) -> Unit,
) {
var expanded by remember { mutableStateOf(false) }
val selectedItemText = when {
items.isEmpty() -> ""
selectedIndex < 0 -> items[0]
else -> items[selectedIndex]
}
val indicatorColor = when {
errorRes != null -> MaterialTheme.colors.error.copy(alpha = ContentAlpha.high)
expanded -> MaterialTheme.colors.primary.copy(alpha = ContentAlpha.high)
else -> MaterialTheme.colors.onSurface.copy(alpha = 0.42f)
}
Column(modifier) {
OutlinedTextField(
value = selectedItemText,
label = { Text(label) },
onValueChange = {},
readOnly = true,
trailingIcon = {
IconButton(
onClick = {
expanded = !expanded
},
) {
Icon(
imageVector = Icons.Default.ArrowDropDown,
contentDescription = "select",
Modifier.rotate(
if (expanded) 180f else 0f
),
)
}
},
isError = errorRes != null
)
DropdownMenu(
expanded = expanded,
onDismissRequest = {
expanded = false
},
) {
items.forEachIndexed { index, item ->
DropdownMenuItem(
onClick = {
onChange(index)
expanded = false
}
) {
Text(text = item)
}
}
}
if (errorRes != null) {
Text(
text = label,
modifier = Modifier
.background(color = MaterialTheme.colors.background),
style = MaterialTheme.typography.caption.copy(color = indicatorColor)
)
}
}
}