Skip to content

Commit c4ce435

Browse files
authored
Merge pull request #409 from Spendesk/task/update-grapes-section
Add action slot to GrapesSection
2 parents 7521080 + de7db6b commit c4ce435

File tree

1 file changed

+119
-9
lines changed
  • library-compose/src/main/java/com/spendesk/grapes/compose/section

1 file changed

+119
-9
lines changed

library-compose/src/main/java/com/spendesk/grapes/compose/section/GrapesSection.kt

+119-9
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,23 @@
11
package com.spendesk.grapes.compose.section
22

3+
import androidx.compose.foundation.layout.Arrangement
4+
import androidx.compose.foundation.layout.Box
35
import androidx.compose.foundation.layout.Column
6+
import androidx.compose.foundation.layout.PaddingValues
7+
import androidx.compose.foundation.layout.Row
48
import androidx.compose.foundation.layout.fillMaxWidth
59
import androidx.compose.foundation.layout.height
610
import androidx.compose.foundation.layout.padding
11+
import androidx.compose.foundation.layout.widthIn
12+
import androidx.compose.material3.ButtonColors
713
import androidx.compose.material3.Card
814
import androidx.compose.material3.CardDefaults
915
import androidx.compose.material3.Text
16+
import androidx.compose.material3.TextButton
1017
import androidx.compose.runtime.Composable
18+
import androidx.compose.ui.Alignment
1119
import androidx.compose.ui.Modifier
20+
import androidx.compose.ui.text.style.TextOverflow
1221
import androidx.compose.ui.tooling.preview.Preview
1322
import androidx.compose.ui.unit.dp
1423
import com.spendesk.grapes.compose.internal.Slot
@@ -22,6 +31,7 @@ import com.spendesk.grapes.compose.theme.GrapesTheme
2231
fun GrapesSection(
2332
title: String,
2433
modifier: Modifier = Modifier,
34+
action: @Composable (GrapesSectionActionScope.() -> Unit)? = null,
2535
content: @Composable () -> Unit,
2636
) {
2737
Card(
@@ -33,28 +43,128 @@ fun GrapesSection(
3343
),
3444
) {
3545
Column(
36-
modifier = Modifier.padding(vertical = GrapesTheme.dimensions.unit8),
46+
modifier = Modifier
47+
.padding(vertical = GrapesTheme.dimensions.unit8)
48+
) {
49+
Row(
50+
verticalAlignment = Alignment.CenterVertically,
51+
horizontalArrangement = Arrangement.SpaceBetween,
52+
) {
53+
Text(
54+
text = title,
55+
style = GrapesTheme.typography.titleS,
56+
color = GrapesTheme.colors.contentSecondaryBGSecondary,
57+
maxLines = 1,
58+
overflow = TextOverflow.Ellipsis,
59+
modifier = Modifier
60+
.weight(1f)
61+
.padding(
62+
horizontal = GrapesTheme.dimensions.unit16,
63+
vertical = GrapesTheme.dimensions.unit8,
64+
)
65+
)
66+
if (action != null) {
67+
Box {
68+
GrapesSectionActionScope.action()
69+
}
70+
}
71+
}
72+
content()
73+
}
74+
}
75+
}
76+
77+
object GrapesSectionActionScope {
78+
79+
@Composable
80+
fun Action(
81+
title: String,
82+
modifier: Modifier = Modifier,
83+
enabled: Boolean = true,
84+
onClick: () -> Unit,
85+
) {
86+
TextButton(
87+
onClick = onClick,
88+
contentPadding = PaddingValues(
89+
horizontal = GrapesTheme.dimensions.unit16,
90+
vertical = 0.dp,
91+
),
92+
shape = GrapesTheme.shapes.radius8,
93+
enabled = enabled,
94+
colors = ButtonColors(
95+
contentColor = GrapesTheme.colors.contentBrandDefault,
96+
containerColor = GrapesTheme.colors.backgroundPrimaryDefault,
97+
disabledContentColor = GrapesTheme.colors.contentDisable,
98+
disabledContainerColor = GrapesTheme.colors.backgroundPrimaryDefault,
99+
),
100+
modifier = modifier
101+
.widthIn(min = GrapesTheme.dimensions.unit48)
102+
.height(GrapesTheme.dimensions.unit32)
37103
) {
38104
Text(
39105
text = title,
40106
style = GrapesTheme.typography.titleS,
41-
color = GrapesTheme.colors.contentSecondaryBGSecondary,
42-
modifier = Modifier.padding(
43-
horizontal = GrapesTheme.dimensions.unit16,
44-
vertical = GrapesTheme.dimensions.unit8,
45-
),
107+
maxLines = 1,
108+
overflow = TextOverflow.Ellipsis,
109+
)
110+
}
111+
}
112+
}
113+
114+
@Preview
115+
@Preview(fontScale = 2f)
116+
@Composable
117+
private fun PreviewGrapesSectionWithAction() {
118+
GrapesTheme {
119+
GrapesSection(
120+
title = "Some section title",
121+
action = {
122+
Action(
123+
title = "Actionable",
124+
onClick = {},
125+
enabled = true,
126+
)
127+
},
128+
) {
129+
Slot(
130+
modifier = Modifier
131+
.fillMaxWidth()
132+
.height(100.dp)
133+
)
134+
}
135+
}
136+
}
137+
138+
@Preview
139+
@Composable
140+
private fun PreviewGrapesSectionWithActionDisabled() {
141+
GrapesTheme {
142+
GrapesSection(
143+
title = "Some section title",
144+
action = {
145+
Action(
146+
title = "Actionable",
147+
onClick = {},
148+
enabled = false,
149+
)
150+
},
151+
) {
152+
Slot(
153+
modifier = Modifier
154+
.fillMaxWidth()
155+
.height(100.dp)
46156
)
47-
content()
48157
}
49158
}
50159
}
51160

52161
@Preview
53162
@Composable
54-
private fun GrapesSectionPreview() {
163+
private fun PreviewGrapesSectionWithoutAction() {
55164
GrapesTheme {
56165
GrapesSection(
57-
title = "Title",
166+
title = "Some section title",
167+
action = null,
58168
) {
59169
Slot(
60170
modifier = Modifier

0 commit comments

Comments
 (0)