|
| 1 | +package com.getcode.ui.components |
| 2 | + |
| 3 | +import androidx.compose.foundation.layout.PaddingValues |
| 4 | +import androidx.compose.material3.SegmentedButton |
| 5 | +import androidx.compose.material3.SegmentedButtonColors |
| 6 | +import androidx.compose.material3.SegmentedButtonDefaults |
| 7 | +import androidx.compose.material3.SingleChoiceSegmentedButtonRow |
| 8 | +import androidx.compose.material3.SingleChoiceSegmentedButtonRowScope |
| 9 | +import androidx.compose.runtime.Composable |
| 10 | +import androidx.compose.ui.Modifier |
| 11 | +import androidx.compose.ui.unit.Dp |
| 12 | + |
| 13 | +@Composable |
| 14 | +fun <T> SegmentedControl( |
| 15 | + options: List<T>, |
| 16 | + selected: T?, |
| 17 | + colors: SegmentedButtonColors = SegmentedButtonDefaults.colors(), |
| 18 | + contentPadding: PaddingValues = SegmentedButtonDefaults.ContentPadding, |
| 19 | + mapper: @Composable SingleChoiceSegmentedButtonRowScope.(T) -> Unit, |
| 20 | + space: Dp = SegmentedButtonDefaults.BorderWidth, |
| 21 | + modifier: Modifier = Modifier, |
| 22 | + onSelectionChanged: (T) -> Unit, |
| 23 | +) { |
| 24 | + SingleChoiceSegmentedButtonRow( |
| 25 | + modifier = modifier, |
| 26 | + space = space, |
| 27 | + ) { |
| 28 | + options.mapIndexed { index, item -> |
| 29 | + SegmentedButton( |
| 30 | + selected = item == selected, |
| 31 | + onClick = { onSelectionChanged(item) }, |
| 32 | + colors = colors, |
| 33 | + shape = SegmentedButtonDefaults.itemShape(index, count = options.count()), |
| 34 | + contentPadding = contentPadding, |
| 35 | + icon = {}, |
| 36 | + label = { |
| 37 | + mapper(item) |
| 38 | + }, |
| 39 | + ) |
| 40 | + } |
| 41 | + } |
| 42 | +} |
| 43 | + |
0 commit comments