HelperText

fun HelperText(label: String, state: HelperTextState, modifier: Modifier = Modifier)(source)

Helper Text is a validation component designed to help out and provide guidance to form elements. This component consists only of an icon indicator and a label and it should be used together with other components.

The Helper Text component has three different states.

Parameters

label

The text to be displayed.

state

defines the validation state of the component

modifier

Modifier to be applied in the Row around the component

See also

Samples

val options = (0..2).map { index -> Pair(index, "Item $index") }
var selectedId by rememberSaveable { mutableStateOf<Int?>(null) }
var helperState by rememberSaveable { mutableStateOf(HelperTextState.Regular) }

// Updates message and helper state depending on the field validation
val helperText = when (helperState) {
    HelperTextState.Regular -> "Select an item"
    HelperTextState.Success -> "All good"
    HelperTextState.Error -> "You need to select an option"
    HelperTextState.Warning -> "Ok, but can be improved"
}

// Update the selected option of the radio group
fun selectOption(id: Int) {
    selectedId = id
    if (helperState == HelperTextState.Error) helperState = HelperTextState.Success
}

fun submitForm() {
    // Validate fields
    if (selectedId == null) {
        helperState = HelperTextState.Error
    }
    // DO SOMETHING HERE
}
SkapaTheme2(isSystemInDarkTheme()) {
    Column(
        modifier = Modifier
            .fillMaxWidth()
            .padding(SkapaSpacing.space100),
        verticalArrangement = Arrangement.spacedBy(SkapaSpacing.space50)
    ) {
        // Group of radio buttons to be validated
        Column(
            Modifier
                .selectableGroup()
                .semantics {
                    if (helperState == HelperTextState.Error) {
                        error(helperText)
                    } else {
                        contentDescription = helperText
                    }
                }
        ) {
            options.forEach { (id, label) ->
                RadioButton(label = label, selected = (id == selectedId), variant = RadioButtonVariant.Subtle, onClick = { selectOption(id) })
            }
        }
        // Helper text to support radio button validation
        HelperText(label = helperText, state = helperState, modifier = Modifier.clearAndSetSemantics { })

        // Button to save the information
        Row(horizontalArrangement = Arrangement.spacedBy(SkapaSpacing.space50)) {
            Button(label = "Submit", size = ButtonSize.Small, onClick = ::submitForm)
            Button(label = "Clear", variant = ButtonVariant.Secondary, size = ButtonSize.Small) {
                selectedId = null
                helperState = HelperTextState.Regular
            }
        }
    }
}