Skip to content

Commit

Permalink
isFocused added to check if view is focused (#9)
Browse files Browse the repository at this point in the history
OtpCell cursor condition added to only be visible, when PinInput is focused.
  • Loading branch information
IMshub10 authored Oct 4, 2023
1 parent 719c090 commit 2872c89
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions speld/src/main/java/com/yogeshpaliyal/speld/OtpView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -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
)
Expand All @@ -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)
Expand Down

0 comments on commit 2872c89

Please sign in to comment.