Skip to content

Commit 2872c89

Browse files
authored
isFocused added to check if view is focused (#9)
OtpCell cursor condition added to only be visible, when PinInput is focused.
1 parent 719c090 commit 2872c89

File tree

1 file changed

+9
-2
lines changed
  • speld/src/main/java/com/yogeshpaliyal/speld

1 file changed

+9
-2
lines changed

speld/src/main/java/com/yogeshpaliyal/speld/OtpView.kt

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import androidx.compose.ui.Modifier
1515
import androidx.compose.ui.draw.clip
1616
import androidx.compose.ui.focus.FocusRequester
1717
import androidx.compose.ui.focus.focusRequester
18+
import androidx.compose.ui.focus.onFocusChanged
1819
import androidx.compose.ui.platform.LocalSoftwareKeyboardController
1920
import androidx.compose.ui.text.input.KeyboardType
2021
import androidx.compose.ui.unit.dp
@@ -66,10 +67,13 @@ fun PinInput(
6667
value: String = "",
6768
disableKeypad: Boolean = false,
6869
obscureText: String? = "*",
70+
cursorVisibleOnlyOnFocus: Boolean = true,
6971
onValueChanged: (String) -> Unit
7072
) {
7173
val focusRequester = remember { FocusRequester() }
7274
val keyboard = LocalSoftwareKeyboardController.current
75+
val isFocused = remember { mutableStateOf(false) }
76+
7377
TextField(
7478
readOnly = disableKeypad,
7579
value = value,
@@ -86,7 +90,10 @@ fun PinInput(
8690
// Hide the text field
8791
modifier = Modifier
8892
.size(0.dp)
89-
.focusRequester(focusRequester),
93+
.focusRequester(focusRequester)
94+
.onFocusChanged {
95+
isFocused.value = it.isFocused
96+
},
9097
keyboardOptions = KeyboardOptions(
9198
keyboardType = KeyboardType.Number
9299
)
@@ -110,7 +117,7 @@ fun PinInput(
110117
keyboard?.show()
111118
},
112119
value = value.getOrNull(it),
113-
isCursorVisible = value.length == it,
120+
isCursorVisible = (isFocused.value || !cursorVisibleOnlyOnFocus) && value.length == it,
114121
obscureText
115122
)
116123
if (it != length - 1)

0 commit comments

Comments
 (0)