JengaOtpField

fun JengaOtpField(value: String, onValueChange: (String) -> Unit, contentDescription: String, modifier: Modifier = Modifier, length: Int = JengaOtpFieldDefaults.Length, onComplete: (String) -> Unit? = null, inputType: JengaOtpInputType = JengaOtpInputType.Numeric, status: JengaTextFieldStatus = JengaTextFieldStatus.Default, supportingText: String? = null, enabled: Boolean = true, readOnly: Boolean = false, masked: Boolean = false, autoFocus: Boolean = false, style: JengaOtpCellStyle = JengaOtpCellStyle.Outlined, cellSize: DpSize = JengaOtpFieldDefaults.cellSize, cellSpacing: Dp = JengaOtpFieldDefaults.cellSpacing, groupSize: Int = 0, shape: Shape = JengaOtpFieldDefaults.shape, colors: JengaOtpFieldColors = JengaOtpFieldDefaults.colors(), textStyle: TextStyle = JengaOtpFieldDefaults.textStyle, cell: @Composable (JengaOtpCellState) -> Unit? = null)(source)

A one-time-code input drawn as a row of cells.

One text input sits under all the cells, so typing, backspace, paste, SMS code autofill (on platforms that support it) and screen readers treat the code as a single field. Input is filtered by inputType and capped at length before onValueChange is called, so a pasted "Your code is 123-456" becomes "123456".

The field never clears itself: after a failed check, set status to JengaTextFieldStatus.Error (the cells turn red and shake) and clear value if the code should be re-entered. The shake runs when status changes to Error, so set it back to JengaTextFieldStatus.Default when the user edits the code; a later failure then shakes again.

Parameters

value

the code entered so far.

onValueChange

called with the filtered, length-capped code.

contentDescription

what a screen reader announces for the field, e.g. "Verification code".

modifier

the Modifier for the whole field (cells + supporting text).

length

the number of cells.

onComplete

called once each time the code reaches length characters.

inputType

which characters are accepted; also picks the keyboard.

status

validation status; drives cell borders and the supporting-text color.

supportingText

optional helper or error text under the cells.

enabled

whether the field can be focused and edited.

readOnly

whether the field is focusable but not editable, e.g. while the code is checked.

masked

whether to show a dot instead of each character.

autoFocus

whether to focus the field and show the keyboard when it first appears.

style

how the default cells are drawn; ignored when cell is set.

cellSize

the largest size of one cell.

cellSpacing

the space between cells.

groupSize

when above 0, adds a wider gap after every groupSize cells (3 gives 123 456).

colors

the color set; defaults to JengaOtpFieldDefaults.colors.

textStyle

the character text style.

cell

optional custom cell; it is drawn inside a box of the cell's size.

Samples

var code by remember {
    mutableStateOf(
        value = "",
    )
}
JengaOtpField(
    value = code,
    onValueChange = { code = it },
    contentDescription = "Verification code",
    onComplete = { },
    autoFocus = true,
)