Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

isFocused added to check if view is focused #9

Merged
merged 1 commit into from
Oct 4, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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