Skip to content

Commit aadb7b0

Browse files
Merge pull request #14768 from woocommerce/issue/WCOutlinedButton_enhancment
[Compose component] - Add loading param to WCOutlinedButton
2 parents ef133c4 + d1e3f49 commit aadb7b0

File tree

2 files changed

+40
-25
lines changed

2 files changed

+40
-25
lines changed

WooCommerce/src/main/kotlin/com/woocommerce/android/ui/bookings/compose/BookingAppointmentDetails.kt

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,8 @@ import androidx.compose.foundation.layout.Column
88
import androidx.compose.foundation.layout.Row
99
import androidx.compose.foundation.layout.fillMaxWidth
1010
import androidx.compose.foundation.layout.padding
11-
import androidx.compose.foundation.layout.size
1211
import androidx.compose.material3.ButtonDefaults
13-
import androidx.compose.material3.CircularProgressIndicator
1412
import androidx.compose.material3.HorizontalDivider
15-
import androidx.compose.material3.LocalContentColor
1613
import androidx.compose.material3.MaterialTheme
1714
import androidx.compose.material3.Text
1815
import androidx.compose.runtime.Composable
@@ -93,16 +90,9 @@ fun BookingAppointmentDetails(
9390
),
9491
onClick = onCancelBooking,
9592
enabled = model.cancelButtonEnabled,
96-
) {
97-
if (model.cancelInProgressShown) {
98-
CircularProgressIndicator(
99-
color = LocalContentColor.current,
100-
modifier = Modifier.size(24.dp)
101-
)
102-
} else {
103-
Text(text = stringResource(R.string.booking_details_cancel_booking_button))
104-
}
105-
}
93+
text = stringResource(R.string.booking_details_cancel_booking_button),
94+
loading = model.cancelInProgressShown,
95+
)
10696
HorizontalDivider(thickness = 0.5.dp)
10797
}
10898
}

WooCommerce/src/main/kotlin/com/woocommerce/android/ui/compose/component/Buttons.kt

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -123,10 +123,8 @@ fun WCColoredButton(
123123
) {
124124
Box {
125125
if (loading) {
126-
CircularProgressIndicator(
127-
color = LocalContentColor.current,
126+
ButtonCircularProgressIndicator(
128127
modifier = Modifier
129-
.size(24.dp)
130128
.align(Alignment.Center)
131129
)
132130
}
@@ -185,14 +183,15 @@ fun WCOutlinedButton(
185183
leadingIcon: @Composable (() -> Unit)? = null,
186184
trailingIcon: @Composable (() -> Unit)? = null,
187185
enabled: Boolean = true,
186+
loading: Boolean = false,
188187
shape: Shape = MaterialTheme.shapes.small,
189188
colors: ButtonColors = ButtonDefaults.wcOutlinedButtonColors(),
190189
border: BorderStroke? = ButtonDefaults.wcOutlinedButtonBorder(enabled),
191190
contentPadding: PaddingValues = ButtonDefaults.ContentPadding,
192191
interactionSource: MutableInteractionSource? = null,
193192
) {
194193
WCOutlinedButton(
195-
onClick = onClick,
194+
onClick = { if (!loading) onClick() },
196195
modifier = modifier,
197196
enabled = enabled,
198197
shape = shape,
@@ -201,14 +200,21 @@ fun WCOutlinedButton(
201200
contentPadding = contentPadding,
202201
interactionSource = interactionSource
203202
) {
204-
if (leadingIcon != null) {
205-
leadingIcon()
206-
Spacer(modifier = Modifier.width(dimensionResource(id = R.dimen.minor_100)))
207-
}
208-
Text(text = text)
209-
if (trailingIcon != null) {
210-
Spacer(modifier = Modifier.width(dimensionResource(id = R.dimen.minor_100)))
211-
trailingIcon()
203+
if (loading) {
204+
ButtonCircularProgressIndicator(
205+
modifier = Modifier
206+
.align(Alignment.CenterVertically)
207+
)
208+
} else {
209+
if (leadingIcon != null) {
210+
leadingIcon()
211+
Spacer(modifier = Modifier.width(dimensionResource(id = R.dimen.minor_100)))
212+
}
213+
Text(text = text)
214+
if (trailingIcon != null) {
215+
Spacer(modifier = Modifier.width(dimensionResource(id = R.dimen.minor_100)))
216+
trailingIcon()
217+
}
212218
}
213219
}
214220
}
@@ -415,6 +421,15 @@ private fun ProvideMaterial3CompositionForMaterial2(
415421
}
416422
}
417423

424+
@Composable
425+
private fun ButtonCircularProgressIndicator(modifier: Modifier = Modifier) {
426+
CircularProgressIndicator(
427+
color = LocalContentColor.current,
428+
modifier = modifier
429+
.size(24.dp)
430+
)
431+
}
432+
418433
@LightDarkThemePreviews
419434
@Composable
420435
private fun ButtonsPreview() {
@@ -459,6 +474,11 @@ private fun ButtonsPreview() {
459474
)
460475
}
461476
)
477+
WCColoredButton(
478+
text = "Button Loading",
479+
loading = true,
480+
onClick = {},
481+
)
462482

463483
WCOutlinedButton(onClick = {}) {
464484
Text(text = "Outlined Button")
@@ -475,6 +495,11 @@ private fun ButtonsPreview() {
475495
Icon(imageVector = Icons.Default.Check, contentDescription = null, modifier = Modifier.size(16.dp))
476496
}
477497
)
498+
WCOutlinedButton(
499+
text = "Outlined Button Loading",
500+
loading = true,
501+
onClick = {},
502+
)
478503

479504
WCTextButton(onClick = {}) {
480505
Text(text = "Text button")

0 commit comments

Comments
 (0)