Skip to content

Commit d966a87

Browse files
authored
library: Add new component TabRow (#48)
* library: feat: new component TabRow
1 parent cfc07a0 commit d966a87

File tree

2 files changed

+118
-0
lines changed

2 files changed

+118
-0
lines changed

composeApp/src/commonMain/kotlin/component/OtherComponent.kt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import top.yukonga.miuix.kmp.basic.ButtonDefaults
2525
import top.yukonga.miuix.kmp.basic.Card
2626
import top.yukonga.miuix.kmp.basic.Slider
2727
import top.yukonga.miuix.kmp.basic.SmallTitle
28+
import top.yukonga.miuix.kmp.basic.TabRow
2829
import top.yukonga.miuix.kmp.basic.Text
2930
import top.yukonga.miuix.kmp.basic.TextButton
3031
import top.yukonga.miuix.kmp.basic.TextField
@@ -41,6 +42,9 @@ fun OtherComponent(padding: PaddingValues) {
4142
var text2 by remember { mutableStateOf(TextFieldValue("")) }
4243
var progress by remember { mutableStateOf(0.5f) }
4344
val progressDisable by remember { mutableStateOf(0.5f) }
45+
val tabTexts =
46+
listOf("tab1", "tab2", "tab3", "tab4", "tab5", "tab6", "tab7", "tab8", "tab9", "tab10")
47+
var selectedTabIndex1 by remember { mutableStateOf(0) }
4448

4549
SmallTitle(text = "Button")
4650
Row(
@@ -133,6 +137,18 @@ fun OtherComponent(padding: PaddingValues) {
133137
.padding(bottom = 12.dp)
134138
)
135139

140+
SmallTitle(text = "TabRow")
141+
TabRow(
142+
tabs = tabTexts,
143+
selectedTabIndex = selectedTabIndex1,
144+
modifier = Modifier
145+
.padding(horizontal = 12.dp)
146+
.padding(bottom = 12.dp)
147+
) {
148+
selectedTabIndex1 = it
149+
}
150+
151+
136152
SmallTitle(text = "Card")
137153
Card(
138154
modifier = Modifier
@@ -163,4 +179,6 @@ fun OtherComponent(padding: PaddingValues) {
163179
style = MiuixTheme.textStyles.paragraph
164180
)
165181
}
182+
183+
166184
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
package top.yukonga.miuix.kmp.basic
2+
3+
import androidx.compose.foundation.layout.Arrangement
4+
import androidx.compose.foundation.layout.Box
5+
import androidx.compose.foundation.layout.fillMaxHeight
6+
import androidx.compose.foundation.layout.fillMaxSize
7+
import androidx.compose.foundation.layout.fillMaxWidth
8+
import androidx.compose.foundation.layout.height
9+
import androidx.compose.foundation.layout.width
10+
import androidx.compose.foundation.lazy.LazyRow
11+
import androidx.compose.foundation.lazy.itemsIndexed
12+
import androidx.compose.foundation.lazy.rememberLazyListState
13+
import androidx.compose.runtime.Composable
14+
import androidx.compose.runtime.remember
15+
import androidx.compose.ui.Alignment
16+
import androidx.compose.ui.Modifier
17+
import androidx.compose.ui.graphics.Color
18+
import androidx.compose.ui.platform.LocalDensity
19+
import androidx.compose.ui.semantics.Role
20+
import androidx.compose.ui.semantics.role
21+
import androidx.compose.ui.semantics.semantics
22+
import androidx.compose.ui.unit.Dp
23+
import androidx.compose.ui.unit.dp
24+
import androidx.compose.ui.unit.times
25+
import top.yukonga.miuix.kmp.theme.MiuixTheme
26+
import top.yukonga.miuix.kmp.utils.SmoothRoundedCornerShape
27+
import top.yukonga.miuix.kmp.utils.getWindowSize
28+
29+
30+
/**
31+
* A [TabRow] with Miuix style.
32+
*
33+
* @param tabs The text to be displayed in the [TabRow].
34+
* @param selectedTabIndex The selected tab index of the [TabRow]
35+
* @param modifier The modifier to be applied to the [TabRow].
36+
* @param backgroundColor The background color of the tab in [TabRow].
37+
* @param contentColor The text color of the tab in [TabRow].
38+
* @param selectedBackgroundColor The background color of the selected tab in [TabRow].
39+
* @param selectedColor The text color of the selected tab in [TabRow].
40+
* @param cornerRadius The round corner radius of the tab in [TabRow].
41+
* @param onSelect The callback when the tab of the [TabRow] is clicked.
42+
*/
43+
@Composable
44+
fun TabRow(
45+
tabs: List<String>,
46+
selectedTabIndex: Int,
47+
modifier: Modifier = Modifier,
48+
backgroundColor: Color = MiuixTheme.colorScheme.background,
49+
contentColor: Color = MiuixTheme.colorScheme.onSurfaceVariantSummary,
50+
selectedBackgroundColor: Color = MiuixTheme.colorScheme.surface,
51+
selectedColor: Color = MiuixTheme.colorScheme.onSurface,
52+
cornerRadius: Dp = 8.dp,
53+
onSelect: ((Int) -> Unit)? = null,
54+
) {
55+
val listState = rememberLazyListState()
56+
val windowSize = getWindowSize()
57+
val density = LocalDensity.current
58+
var tabWidth: Dp
59+
with(density) {
60+
tabWidth =
61+
((windowSize.width.toDp() - (tabs.size - 1) * 9.dp) / tabs.size).coerceAtLeast(62.dp)
62+
}
63+
64+
val shape = remember { SmoothRoundedCornerShape(cornerRadius) }
65+
66+
LazyRow(
67+
state = listState,
68+
modifier = modifier
69+
.fillMaxWidth().height(42.dp),
70+
verticalAlignment = Alignment.CenterVertically,
71+
horizontalArrangement = Arrangement.spacedBy(9.dp)
72+
) {
73+
itemsIndexed(tabs) { index, tabText ->
74+
Surface(
75+
shape = shape,
76+
onClick = {
77+
onSelect?.invoke(index)
78+
},
79+
enabled = onSelect != null,
80+
color =
81+
if (selectedTabIndex == index) selectedBackgroundColor
82+
else backgroundColor,
83+
modifier = Modifier.fillMaxHeight()
84+
.width(tabWidth).semantics { role = Role.Tab }
85+
) {
86+
Box(
87+
modifier = Modifier.fillMaxSize(),
88+
contentAlignment = Alignment.Center
89+
) {
90+
Text(
91+
text = tabText,
92+
color = if (selectedTabIndex == index) selectedColor else contentColor,
93+
maxLines = 1
94+
)
95+
}
96+
}
97+
}
98+
}
99+
100+
}

0 commit comments

Comments
 (0)