Skip to content

Instantly share code, notes, and snippets.

@ch8n
Created November 20, 2021 08:58
Show Gist options
  • Save ch8n/599727b2ab4cae6c20eb576f3a490453 to your computer and use it in GitHub Desktop.
Save ch8n/599727b2ab4cae6c20eb576f3a490453 to your computer and use it in GitHub Desktop.
@ExperimentalComposeUiApi
@Composable
fun OtpBugView() {
val (editValue, setEditValue) = remember { mutableStateOf("") }
val otpLength = remember { 4 }
val focusRequester = remember { FocusRequester() }
val keyboard = LocalSoftwareKeyboardController.current
// hidden TextField for controlling OTP Cells UI
TextField(
value = editValue,
onValueChange = {
if (it.length <= otpLength) {
setEditValue(it)
}
},
modifier = Modifier
.size(0.dp)
.focusRequester(focusRequester),
keyboardOptions = KeyboardOptions.Default.copy(
keyboardType = KeyboardType.Number
)
)
Row(
modifier = Modifier.fillMaxWidth(),
horizontalArrangement = Arrangement.Center
) {
(0 until otpLength).map { index ->
OtpCell(
modifier = Modifier
.size(36.dp)
.clickable {
focusRequester.requestFocus()
// this is required so if keyboard is dissmissed
// then could be open again with focus
keyboard?.show()
}
.border(1.dp, Color.DarkGray),
value = editValue.getOrNull(index)?.toString() ?: "",
isCursorVisible = editValue.length == index
)
Spacer(modifier = Modifier.size(8.dp))
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment