OtpField

fun OtpField(state: OtpState, onStateChange: (OtpState) -> Unit, onComplete: (code: String) -> Unit, accessibility: OtpFieldAccessibility, modifier: Modifier = Modifier, enabled: Boolean = true, style: OtpFieldStyle = OtpFieldDefaults.style(), mask: OtpMask = OtpMask.None, hapticFeedbackOnError: Boolean = true, alignment: OtpAlignment = OtpAlignment.CENTER, onInputRejected: (OtpInputRejection) -> Unit = {})(source)

One-time-code input that exposes a single logical text field to screen readers while drawing the digits as decorative cells (cells, group separators and placeholders are not in the accessibility tree).

The host owns state and verification: call OtpState.withError or OtpState.cleared after a failed verification. onComplete fires once per incomplete-to-complete transition. accessibility is required and must be localized by the host. With a mask other than OtpMask.None the field uses a password keyboard and password semantics, and screen readers get the label and progress only (tested: no digits in any semantics, no copy or cut action). hapticFeedbackOnError vibrates once when OtpState.isError becomes true. alignment (default centered) only positions the cells and error text; the digits inside the row always read left to right. The field spans the available width.

A single input that inserts several characters at once (paste, autofill, keyboard suggestion) is accepted only with exactly the field's length; see OtpState.withInput. Otherwise the state stays unchanged, onInputRejected is called once with the character counts, and OtpFieldAccessibility.inputRejectedMessage appears below the field in the slot of the error message in a polite live region, announced on every rejection (also an identical one). While OtpState.isError the newest message wins: the rejection replaces the error text, the error styling and semantics stay, and the error text returns when the rejection is cleared. A rejection is not an error state itself: no error border or haptic feedback, no focus move, and onStateChange is not called when the state is unchanged. A blank message shows nothing. The message is cleared by the next edit and when state changes. A rejection means the field length probably differs from the code length of the host's backend.