Change CheckboxInput and SwitchInput to InputLayout with error message

This commit is contained in:
Wolf-Martell Montwé 2023-06-12 10:10:59 +02:00
parent 2490d7402d
commit 86e8ea30d0
No known key found for this signature in database
GPG key ID: 6D45B21512ACBF72
3 changed files with 90 additions and 28 deletions

View file

@ -131,6 +131,7 @@ fun LazyGridScope.inputItems() {
}
sectionHeaderItem(text = "CheckboxInput")
sectionSubtitleItem(text = "Default")
item {
ItemOutlined {
WithRememberedState(input = false) { state ->
@ -142,8 +143,22 @@ fun LazyGridScope.inputItems() {
}
}
}
sectionSubtitleItem(text = "With error")
item {
ItemOutlined {
WithRememberedState(input = false) { state ->
CheckboxInput(
text = "Check the box",
checked = state.value,
onCheckedChange = { state.value = it },
errorMessage = "Checkbox must be checked",
)
}
}
}
sectionHeaderItem(text = "SwitchInput")
sectionSubtitleItem(text = "Default")
item {
ItemOutlined {
WithRememberedState(input = false) { state ->
@ -155,4 +170,17 @@ fun LazyGridScope.inputItems() {
}
}
}
sectionSubtitleItem(text = "With error")
item {
ItemOutlined {
WithRememberedState(input = false) { state ->
SwitchInput(
text = "Switch the toggle",
checked = state.value,
onCheckedChange = { state.value = it },
errorMessage = "Switch must be checked",
)
}
}
}
}

View file

@ -5,7 +5,6 @@ import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
@ -21,14 +20,18 @@ fun CheckboxInput(
checked: Boolean,
onCheckedChange: (Boolean) -> Unit,
modifier: Modifier = Modifier,
errorMessage: String? = null,
contentPadding: PaddingValues = inputContentPadding(),
) {
InputLayout(
modifier = modifier,
contentPadding = contentPadding,
errorMessage = errorMessage,
) {
Row(
modifier = Modifier
.padding(contentPadding)
.fillMaxWidth()
.clickable { onCheckedChange(!checked) }
.then(modifier),
.clickable { onCheckedChange(!checked) },
verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.spacedBy(MainTheme.spacings.half),
) {
@ -38,6 +41,7 @@ fun CheckboxInput(
)
TextBody1(text = text)
}
}
}
@Preview(showBackground = true)
@ -52,6 +56,19 @@ internal fun CheckboxInputPreview() {
}
}
@Preview(showBackground = true)
@Composable
internal fun CheckboxInputWithErrorPreview() {
PreviewWithThemes {
CheckboxInput(
text = "CheckboxInput",
checked = false,
onCheckedChange = {},
errorMessage = "Error message",
)
}
}
@Preview(showBackground = true)
@Composable
internal fun CheckboxInputCheckedPreview() {

View file

@ -5,7 +5,6 @@ import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
@ -21,14 +20,18 @@ fun SwitchInput(
checked: Boolean,
onCheckedChange: (Boolean) -> Unit,
modifier: Modifier = Modifier,
errorMessage: String? = null,
contentPadding: PaddingValues = inputContentPadding(),
) {
InputLayout(
modifier = modifier,
contentPadding = contentPadding,
errorMessage = errorMessage,
) {
Row(
modifier = Modifier
.padding(contentPadding)
.fillMaxWidth()
.clickable { onCheckedChange(!checked) }
.then(modifier),
.clickable { onCheckedChange(!checked) },
verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.spacedBy(MainTheme.spacings.half),
) {
@ -38,6 +41,7 @@ fun SwitchInput(
)
TextBody1(text = text)
}
}
}
@Preview(showBackground = true)
@ -52,6 +56,19 @@ internal fun SwitchInputPreview() {
}
}
@Preview(showBackground = true)
@Composable
internal fun SwitchInputWithErrorPreview() {
PreviewWithThemes {
SwitchInput(
text = "SwitchInput",
checked = false,
onCheckedChange = {},
errorMessage = "Error message",
)
}
}
@Preview(showBackground = true)
@Composable
internal fun SwitchInputCheckedPreview() {