Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Unverified Commit c273001e authored by Wolf-Martell Montwé's avatar Wolf-Martell Montwé
Browse files

feat(ui): add AdvancedTexInput to design system

parent cdc38a0a
Loading
Loading
Loading
Loading
+87 −0
Original line number Diff line number Diff line
package app.k9mail.core.ui.compose.designsystem.molecule.input

import androidx.compose.runtime.Composable
import androidx.compose.ui.text.SpanStyle
import androidx.compose.ui.text.TextRange
import androidx.compose.ui.text.buildAnnotatedString
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.input.TextFieldValue
import androidx.compose.ui.text.withStyle
import androidx.compose.ui.tooling.preview.Preview
import app.k9mail.core.ui.compose.designsystem.PreviewWithThemes

@Composable
@Preview(showBackground = true)
internal fun AdvancedTextInputPreview() {
    PreviewWithThemes {
        AdvancedTextInput(
            onTextChange = {},
        )
    }
}

@Composable
@Preview(showBackground = true)
internal fun AdvancedTextInputIsRequiredPreview() {
    PreviewWithThemes {
        AdvancedTextInput(
            onTextChange = {},
            label = "Text input is required",
            isRequired = true,
        )
    }
}

@Composable
@Preview(showBackground = true)
internal fun AdvancedTextInputWithErrorPreview() {
    PreviewWithThemes {
        AdvancedTextInput(
            onTextChange = {},
            errorMessage = "Text input error",
        )
    }
}

@Composable
@Preview(showBackground = true)
internal fun AdvancedTextInputWithAnnotatedStringPreview() {
    PreviewWithThemes {
        AdvancedTextInput(
            onTextChange = {},
            text = TextFieldValue(
                annotatedString = buildAnnotatedString {
                    append("Text input with ")
                    withStyle(style = SpanStyle(fontWeight = FontWeight.Bold)) {
                        append("Annotated")
                    }
                },
            ),
        )
    }
}

@Composable
@Preview(showBackground = true)
internal fun AdvancedTextInputWithSelectionPreview() {
    PreviewWithThemes {
        AdvancedTextInput(
            onTextChange = {},
            text = TextFieldValue("Text input with selection", selection = TextRange(0, 4)),
        )
    }
}

@Composable
@Preview(showBackground = true)
internal fun AdvancedTextInputWithCompositionPreview() {
    PreviewWithThemes {
        AdvancedTextInput(
            onTextChange = {},
            text = TextFieldValue(
                text = "Text input with composition",
                composition = TextRange(0, 4),
            ),
        )
    }
}
+47 −0
Original line number Diff line number Diff line
package app.k9mail.core.ui.compose.designsystem.molecule.input

import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.input.TextFieldValue
import app.k9mail.core.ui.compose.designsystem.atom.textfield.TextFieldOutlined

/**
 * A text input field that uses [TextFieldValue] to support text selection and composition.
 *
 * It supports annotated strings, which can be used to display rich text or formatted text.
 */
@Suppress("LongParameterList")
@Composable
fun AdvancedTextInput(
    onTextChange: (TextFieldValue) -> Unit,
    modifier: Modifier = Modifier,
    text: TextFieldValue = TextFieldValue(""),
    label: String? = null,
    isRequired: Boolean = false,
    errorMessage: String? = null,
    contentPadding: PaddingValues = inputContentPadding(),
    isSingleLine: Boolean = true,
    isEnabled: Boolean = true,
    keyboardOptions: KeyboardOptions = KeyboardOptions.Default,
) {
    InputLayout(
        modifier = modifier,
        contentPadding = contentPadding,
        errorMessage = errorMessage,
    ) {
        TextFieldOutlined(
            value = text,
            onValueChange = onTextChange,
            label = label,
            isEnabled = isEnabled,
            isRequired = isRequired,
            hasError = errorMessage != null,
            isSingleLine = isSingleLine,
            modifier = Modifier.fillMaxWidth(),
            keyboardOptions = keyboardOptions,
        )
    }
}