11package de.westnordost.streetcomplete.ui.common
22
3+ import androidx.compose.foundation.BorderStroke
34import androidx.compose.foundation.interaction.MutableInteractionSource
45import androidx.compose.foundation.layout.Box
56import androidx.compose.foundation.layout.PaddingValues
67import androidx.compose.foundation.layout.RowScope
8+ import androidx.compose.foundation.layout.calculateEndPadding
9+ import androidx.compose.foundation.layout.calculateStartPadding
710import androidx.compose.foundation.layout.padding
11+ import androidx.compose.material.ButtonColors
812import androidx.compose.material.ButtonElevation
913import androidx.compose.material.DropdownMenu
1014import androidx.compose.material.ExperimentalMaterialApi
1115import androidx.compose.material.Icon
1216import androidx.compose.material.LocalTextStyle
13- import androidx.compose.material.OutlinedButton
1417import androidx.compose.material.Text
1518import androidx.compose.runtime.Composable
1619import androidx.compose.runtime.getValue
@@ -19,6 +22,8 @@ import androidx.compose.runtime.remember
1922import androidx.compose.runtime.setValue
2023import androidx.compose.ui.Modifier
2124import androidx.compose.ui.draw.rotate
25+ import androidx.compose.ui.graphics.Shape
26+ import androidx.compose.ui.platform.LocalLayoutDirection
2227import androidx.compose.ui.tooling.preview.Preview
2328import androidx.compose.ui.tooling.preview.datasource.LoremIpsum
2429import androidx.compose.ui.unit.dp
@@ -39,32 +44,56 @@ import kotlin.collections.forEach
3944@Composable
4045fun <T > SelectButton (
4146 items : List <T >,
42- selectedItem : T ,
4347 onSelectedItem : (T ) -> Unit ,
4448 modifier : Modifier = Modifier ,
49+ selectedItem : T ? = null,
50+ style : ButtonStyle = ButtonStyle .Default ,
4551 enabled : Boolean = true,
4652 interactionSource : MutableInteractionSource ? = null,
47- elevation : ButtonElevation ? = null,
53+ elevation : ButtonElevation ? = style.elevation,
54+ shape : Shape = style.shape,
55+ border : BorderStroke ? = style.border,
56+ colors : ButtonColors = style.buttonColors,
57+ contentPadding : PaddingValues = style.contentPadding,
58+ showDropDownArrow : Boolean = true,
4859 itemContent : @Composable (RowScope .(item: T ) -> Unit ),
60+ content : @Composable (RowScope .() -> Unit ) = { selectedItem?.let { itemContent(it) } }
4961) {
5062 var expanded by remember { mutableStateOf(false ) }
5163
64+ val revisedContentPadding = if (showDropDownArrow) {
65+ val layoutDirection = LocalLayoutDirection .current
66+ PaddingValues (
67+ start = contentPadding.calculateStartPadding(layoutDirection),
68+ top = contentPadding.calculateTopPadding(),
69+ end = contentPadding.calculateEndPadding(layoutDirection) - 8 .dp,
70+ bottom = contentPadding.calculateBottomPadding(),
71+ )
72+ } else {
73+ contentPadding
74+ }
75+
5276 Box (modifier = modifier) {
53- OutlinedButton (
77+ Button2 (
5478 onClick = { expanded = ! expanded },
55- contentPadding = PaddingValues (start = 16 .dp, top = 8 .dp, end = 8 .dp, bottom = 8 .dp),
5679 enabled = enabled,
5780 interactionSource = interactionSource,
5881 elevation = elevation,
82+ shape = shape,
83+ border = border,
84+ colors = colors,
85+ contentPadding = revisedContentPadding,
5986 ) {
60- itemContent(selectedItem)
61- Icon (
62- painter = painterResource(Res .drawable.ic_arrow_drop_down_24),
63- contentDescription = null ,
64- modifier = Modifier
65- .padding(start = 8 .dp)
66- .rotate(if (expanded) 180f else 0f )
67- )
87+ content()
88+ if (showDropDownArrow) {
89+ Icon (
90+ painter = painterResource(Res .drawable.ic_arrow_drop_down_24),
91+ contentDescription = null ,
92+ modifier = Modifier
93+ .padding(start = 8 .dp)
94+ .rotate(if (expanded) 180f else 0f )
95+ )
96+ }
6897 }
6998 DropdownMenu (
7099 expanded = expanded,
@@ -95,5 +124,6 @@ private fun LengthInputSelectorPreview() {
95124 items = words,
96125 selectedItem = selected,
97126 onSelectedItem = { selected = it },
98- ) { Text (it) }
127+ itemContent = { Text (it) }
128+ )
99129}
0 commit comments