From 3e7f80f0fdcf961b2497a5237158be6efd15b5c7 Mon Sep 17 00:00:00 2001 From: kb-shub <54928767+IMshub10@users.noreply.github.com> Date: Tue, 3 Oct 2023 22:07:12 +0530 Subject: [PATCH] isFocused added to check if view is focused OtpCell cursor condition added to only be visible, when PinInput is focused. --- .../src/main/java/com/yogeshpaliyal/speld/OtpView.kt | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/speld/src/main/java/com/yogeshpaliyal/speld/OtpView.kt b/speld/src/main/java/com/yogeshpaliyal/speld/OtpView.kt index 015576b..03a6452 100644 --- a/speld/src/main/java/com/yogeshpaliyal/speld/OtpView.kt +++ b/speld/src/main/java/com/yogeshpaliyal/speld/OtpView.kt @@ -15,6 +15,7 @@ import androidx.compose.ui.Modifier import androidx.compose.ui.draw.clip import androidx.compose.ui.focus.FocusRequester import androidx.compose.ui.focus.focusRequester +import androidx.compose.ui.focus.onFocusChanged import androidx.compose.ui.platform.LocalSoftwareKeyboardController import androidx.compose.ui.text.input.KeyboardType import androidx.compose.ui.unit.dp @@ -66,10 +67,13 @@ fun PinInput( value: String = "", disableKeypad: Boolean = false, obscureText: String? = "*", + cursorVisibleOnlyOnFocus: Boolean = true, onValueChanged: (String) -> Unit ) { val focusRequester = remember { FocusRequester() } val keyboard = LocalSoftwareKeyboardController.current + val isFocused = remember { mutableStateOf(false) } + TextField( readOnly = disableKeypad, value = value, @@ -86,7 +90,10 @@ fun PinInput( // Hide the text field modifier = Modifier .size(0.dp) - .focusRequester(focusRequester), + .focusRequester(focusRequester) + .onFocusChanged { + isFocused.value = it.isFocused + }, keyboardOptions = KeyboardOptions( keyboardType = KeyboardType.Number ) @@ -110,7 +117,7 @@ fun PinInput( keyboard?.show() }, value = value.getOrNull(it), - isCursorVisible = value.length == it, + isCursorVisible = (isFocused.value || !cursorVisibleOnlyOnFocus) && value.length == it, obscureText ) if (it != length - 1)