Has anyone got a dropdown selector implementation ...
# compose-desktop
b
Has anyone got a dropdown selector implementation I could steal?
b
Thanks, but I'm looking for something in plain material.
i
ExposedDropDownMenu probably is what are you looking for. It is currently android-only, but you can copy it into your code.
👍 1
a
Just done exactly that:
Copy code
@Composable
fun <T> DropDownSelect(
    items: List<T>,
    selected: MutableState<T>,
    itemLabelBuilder: (T) -> String = { it.toString() },
    modifier: Modifier = Modifier,
    label: @Composable (() -> Unit)? = null
) {
    var expanded by remember { mutableStateOf(false) }
    Box(modifier = modifier.fillMaxWidth().wrapContentSize(Alignment.TopStart)) {
        OutlinedTextField(
            value = itemLabelBuilder(selected.value),
            label = label,
            onValueChange = {},
            readOnly = true,
            trailingIcon = {
                Icon(Icons.Default.ArrowDropDown, "select", Modifier.clickable(onClick = { expanded = true }))
            },
            modifier = modifier.fillMaxWidth()
        )
        DropdownMenu(
            expanded = expanded,
            onDismissRequest = { expanded = false },
            //modifier = Modifier.fillMaxWidth()
        ) {
            items.forEach { item: T ->
                DropdownMenuItem(
                    onClick = {
                        selected.value = item
                        expanded = false
                    }
                ) {
                    Text(text = itemLabelBuilder(item))
                }
            }
        }
    }
}
Probably not the most beautiful thing, but it works.
🏴‍☠️ 3