Skip to content

Commit

Permalink
fix(textfield): char counter now displays even if no helper is provid…
Browse files Browse the repository at this point in the history
…ed (#1445)

Co-authored-by: spark-ui-bot <spark-ui-bot@users.noreply.github.com>
  • Loading branch information
soulcramer and spark-ui-bot authored Jan 28, 2025
1 parent c50c65e commit da5546b
Show file tree
Hide file tree
Showing 18 changed files with 121 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import com.adevinta.spark.ExperimentalSparkApi
import com.adevinta.spark.components.icons.Icon
import com.adevinta.spark.components.textfields.AddonScope
import com.adevinta.spark.components.textfields.TextField
import com.adevinta.spark.components.textfields.TextFieldCharacterCounter
import com.adevinta.spark.components.textfields.TextFieldState
import com.adevinta.spark.icons.Check
import com.adevinta.spark.icons.SparkIcon
Expand Down Expand Up @@ -148,6 +149,85 @@ internal class TextFieldScreenshot {
}
}

@OptIn(ExperimentalLayoutApi::class)
@Test
fun helperMessage() {
paparazzi.sparkSnapshot {
FlowColumn(
modifier = Modifier.padding(8.dp),
verticalArrangement = Arrangement.spacedBy(8.dp),
horizontalArrangement = Arrangement.spacedBy(8.dp),
) {
// With no helper, status message or char counter
TextField(
value = "value",
onValueChange = {},
label = "Label",
)
// With helper, but no status message or char counter
TextField(
value = "value",
onValueChange = {},
label = "Label",
helper = "helper",
)
// With helper, status message but no char counter
TextField(
value = "value",
onValueChange = {},
label = "Label",
helper = "helper",
state = TextFieldState.Error,
stateMessage = "state message",
)
// With helper, status message & char counter
TextField(
value = "value",
onValueChange = {},
label = "Label",
helper = "helper",
state = TextFieldState.Error,
stateMessage = "state message",
)
// With no helper, status message but with char counter
TextField(
value = "value",
onValueChange = {},
label = "Label",
counter = TextFieldCharacterCounter(10, 50),
)
// With helper, char counter but no status message
TextField(
value = "value",
onValueChange = {},
label = "Label",
helper = "helper",
counter = TextFieldCharacterCounter(10, 50),
)
// With helper, status message but no char counter
TextField(
value = "value",
onValueChange = {},
label = "Label",
helper = "helper",
state = TextFieldState.Error,
stateMessage = "state message",
counter = TextFieldCharacterCounter(10, 50),
)
// With helper, status message & char counter
TextField(
value = "value",
onValueChange = {},
label = "Label",
helper = "helper",
state = TextFieldState.Error,
stateMessage = "state message",
counter = TextFieldCharacterCounter(10, 50),
)
}
}
}

@OptIn(ExperimentalSparkApi::class)
@Composable
private fun TextFields(
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,7 @@ private fun ColumnScope.PreviewTextFields(
enabled = true,
state = state,
stateMessage = stateMessage,
counter = TextFieldCharacterCounter(0, 300),
required = true,
label = "Label",
placeholder = "Placeholder",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ private fun OutlinedBorderContainerBox(

@Composable
private fun SupportingText(
text: String,
text: String?,
counterComposable: @Composable ((Modifier) -> Unit)?,
statusIcon: @Composable ((Modifier) -> Unit)?,
) {
Expand All @@ -333,7 +333,7 @@ private fun SupportingText(
statusIcon?.invoke(Modifier.padding(end = 4.dp))
Text(
modifier = Modifier.weight(1f, fill = true),
text = text,
text = text.orEmpty(),
)
counterComposable?.invoke(Modifier.padding(start = 8.dp))
}
Expand Down Expand Up @@ -433,18 +433,14 @@ private fun supportText(
stateMessage: String?,
counterComposable: @Composable ((Modifier) -> Unit)?,
stateIcon: @Composable ((Modifier) -> Unit)?,
): (@Composable () -> Unit)? = if (stateMessage != null && state != null) {
{
SupportingText(
text = stateMessage,
counterComposable = counterComposable,
statusIcon = stateIcon,
)
}
} else if (helper != null) {
): (@Composable () -> Unit)? = if (
(stateMessage != null && state != null) || helper != null || counterComposable != null
) {
{
// Prioritize the state message if there's one and fallback to the helper otherwise
val message = state?.let { stateMessage } ?: helper
SupportingText(
text = helper,
text = message,
counterComposable = counterComposable,
statusIcon = stateIcon,
)
Expand All @@ -461,7 +457,7 @@ internal object TextFieldDefault {

return { modifier ->
Icon(
modifier = modifier.size(16.dp),
modifier = modifier.size(18.dp),
sparkIcon = state.icon,
contentDescription = null,
)
Expand Down

0 comments on commit da5546b

Please sign in to comment.