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

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

feat(ui): add TextFieldOutlined overload that accepts a TextFieldValue

parent 265c799a
Loading
Loading
Loading
Loading
+7 −0
Original line number Diff line number Diff line
package app.k9mail.core.ui.compose.designsystem.atom.textfield

import androidx.compose.runtime.Composable
import androidx.compose.ui.text.input.TextFieldValue

private val LINE_BREAK = "[\\r\\n]".toRegex()

@@ -8,6 +9,12 @@ internal fun stripLineBreaks(onValueChange: (String) -> Unit): (String) -> Unit
    onValueChange(value.replace(LINE_BREAK, replacement = ""))
}

internal fun stripTextFieldValueLineBreaks(onValueChange: (TextFieldValue) -> Unit): (TextFieldValue) -> Unit {
    return { value ->
        onValueChange(value.copy(text = value.text.replace(LINE_BREAK, replacement = "")))
    }
}

internal fun selectLabel(
    label: String?,
    isRequired: Boolean,
+33 −0
Original line number Diff line number Diff line
@@ -3,6 +3,7 @@ package app.k9mail.core.ui.compose.designsystem.atom.textfield
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.input.TextFieldValue
import androidx.compose.material3.OutlinedTextField as Material3OutlinedTextField

@Suppress("LongParameterList")
@@ -33,3 +34,35 @@ fun TextFieldOutlined(
        keyboardOptions = keyboardOptions,
    )
}

/**
 * Overload of [TextFieldOutlined] that accepts a [TextFieldValue] instead of a [String].
 */
@Suppress("LongParameterList")
@Composable
fun TextFieldOutlined(
    value: TextFieldValue,
    onValueChange: (TextFieldValue) -> Unit,
    modifier: Modifier = Modifier,
    label: String? = null,
    trailingIcon: @Composable (() -> Unit)? = null,
    isEnabled: Boolean = true,
    isReadOnly: Boolean = false,
    isRequired: Boolean = false,
    hasError: Boolean = false,
    isSingleLine: Boolean = true,
    keyboardOptions: KeyboardOptions = KeyboardOptions.Default,
) {
    Material3OutlinedTextField(
        value = value,
        onValueChange = if (isSingleLine) stripTextFieldValueLineBreaks(onValueChange) else onValueChange,
        modifier = modifier,
        enabled = isEnabled,
        label = selectLabel(label, isRequired),
        trailingIcon = trailingIcon,
        readOnly = isReadOnly,
        isError = hasError,
        singleLine = isSingleLine,
        keyboardOptions = keyboardOptions,
    )
}