Skip to content

TextField

TextField is a basic input component in Miuix for receiving text input from users. The component provides rich customization options, supporting label animations, leading and trailing icons, and other features.

Import

kotlin
import top.yukonga.miuix.kmp.basic.TextField

Basic Usage

The TextField component can be used to get user input:

kotlin
var text by remember { mutableStateOf("") }

TextField(
    value = text,
    onValueChange = { text = it },
    label = "Username"
)

Input Types

TextField with Label

The label automatically moves to the top when the input field gains focus or has content:

kotlin
var text by remember { mutableStateOf("") }

TextField(
    value = text,
    onValueChange = { text = it },
    label = "Email Address"
)

Using Label as Placeholder

kotlin
var text by remember { mutableStateOf("") }

TextField(
    value = text,
    onValueChange = { text = it },
    label = "Please enter content",
    useLabelAsPlaceholder = true
)

Component States

Disabled State

kotlin
var text by remember { mutableStateOf("") }
TextField(
    value = text,
    onValueChange = { text = it },
    label = "Disabled Input Field",
    enabled = false
)

Read-Only State

kotlin
var text by remember { mutableStateOf("This is read-only content") }

TextField(
    value = text,
    onValueChange = { text = it },
    label = "Read-Only Input Field",
    readOnly = true
)

Properties

TextField Properties

Property NameTypeDescriptionDefault ValueRequired
valueString or TextFieldValueText value of the input field-Yes
onValueChange(String) -> Unit or (TextFieldValue) -> UnitCallback when text changes-Yes
modifierModifierModifier applied to the input fieldModifierNo
insideMarginDpSizeInternal padding of input fieldDpSize(16.dp, 16.dp)No
backgroundColorColorBackground colorMiuixTheme.colorScheme.secondaryContainerNo
cornerRadiusDpCorner radius16.dpNo
labelStringLabel text""No
labelColorColorLabel text colorMiuixTheme.colorScheme.onSecondaryContainerNo
borderColorColorBorder color when focusedMiuixTheme.colorScheme.primaryNo
useLabelAsPlaceholderBooleanUse label as placeholderfalseNo
enabledBooleanWhether input field is enabledtrueNo
readOnlyBooleanWhether input field is read-onlyfalseNo
textStyleTextStyleText styleMiuixTheme.textStyles.mainNo
keyboardOptionsKeyboardOptionsKeyboard optionsKeyboardOptions.DefaultNo
keyboardActionsKeyboardActionsKeyboard actionsKeyboardActions.DefaultNo
leadingIcon@Composable (() -> Unit)?Leading iconnullNo
trailingIcon@Composable (() -> Unit)?Trailing iconnullNo
singleLineBooleanSingle line inputfalseNo
maxLinesIntMaximum linesIf singleLine is true then 1, else Int.MAX_VALUENo
minLinesIntMinimum lines1No
visualTransformationVisualTransformationVisual transformationVisualTransformation.NoneNo
onTextLayout(TextLayoutResult) -> UnitText layout callback{}No
interactionSourceMutableInteractionSource?Interaction sourcenullNo
cursorBrushBrushCursor brushSolidColor(MiuixTheme.colorScheme.primary)No

Advanced Usage

TextField with Icons

kotlin
var text by remember { mutableStateOf("") }

TextField(
    value = text,
    onValueChange = { text = it },
    label = "Search",
    leadingIcon = {
        Icon(
            imageVector = MiuixIcons.Useful.Search,
            contentDescription = "Search Icon",
            modifier = Modifier.padding(horizontal = 12.dp)
        )
    }
)

Password Input Field

kotlin
var password by remember { mutableStateOf("") }
var passwordVisible by remember { mutableStateOf(false) }

TextField(
    value = password,
    onValueChange = { password = it },
    label = "Password",
    visualTransformation = if (passwordVisible) VisualTransformation.None else PasswordVisualTransformation(),
    keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Password),
    trailingIcon = {
        IconButton(
            onClick = { passwordVisible = !passwordVisible },
            modifier = Modifier.padding(end = 12.dp)
        ) {
            Icon(
                imageVector = MiuixIcons.Useful.Rename,
                tint = if (passwordVisible) MiuixTheme.colorScheme.primary else MiuixTheme.colorScheme.onSecondaryContainer,
                contentDescription = if (passwordVisible) "Hide Password" else "Show Password"
            )
        }
    }
)

Input Field with Validation

kotlin
var email by remember { mutableStateOf("") }
var isError by remember { mutableStateOf(false) }
var errorColor = Color.Red.copy(0.3f)
val emailPattern = remember { Regex("[a-zA-Z0-9._-]+@[a-z]+\\.+[a-z]+") }

Column {
    TextField(
        value = email,
        onValueChange = {
            email = it
            isError = email.isNotEmpty() && !emailPattern.matches(email)
        },
        label = "Email",
        labelColor = if (isError) errorColor else MiuixTheme.colorScheme.onSecondaryContainer,
        keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Email)
    )
    if (isError) {
        Text(
            text = "Please enter a valid email address",
            color = errorColor,
                style = MiuixTheme.textStyles.body2,
            modifier = Modifier.padding(start = 16.dp, top = 4.dp)
        )
    }
}

Custom Styles

kotlin
var text by remember { mutableStateOf("") }

TextField(
    value = text,
    onValueChange = { text = it },
    label = "Custom Input Field",
    cornerRadius = 8.dp,
    backgroundColor = MiuixTheme.colorScheme.primary.copy(alpha = 0.1f),
    textStyle = TextStyle(
        fontWeight = FontWeight.Medium,
        fontSize = 16.sp,
        color = MiuixTheme.colorScheme.primary
    )
)

Using TextFieldValue

When you need more fine-grained control over text selection and cursor position:

kotlin
var textFieldValue by remember { mutableStateOf(TextFieldValue("")) }

TextField(
    value = textFieldValue,
    onValueChange = { textFieldValue = it },
    label = "Advanced Input Control",
    // TextFieldValue provides control over text, selection range, and cursor position
)

Released under the Apache-2.0 License