Skip to content

Commit

Permalink
refactor(EmailTextField): Use interactionSource focus state
Browse files Browse the repository at this point in the history
  • Loading branch information
FabianDevel committed Dec 16, 2024
1 parent 3214d4d commit 84d93a2
Showing 1 changed file with 10 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import androidx.compose.animation.animateColorAsState
import androidx.compose.foundation.interaction.FocusInteraction
import androidx.compose.foundation.interaction.FocusInteraction.Focus
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.interaction.collectIsFocusedAsState
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.text.BasicTextField
import androidx.compose.foundation.text.KeyboardActions
Expand Down Expand Up @@ -69,10 +70,11 @@ fun EmailAddressTextField(
) {

var text by rememberSaveable { mutableStateOf(initialValue) }
var isFocused by rememberSaveable { mutableStateOf(false) }
var currentFocus: Focus? by remember { mutableStateOf(null) }
val interactionSource = remember { MutableInteractionSource() }
val focusRequester = remember { FocusRequester() }
val lastChipFocusRequester = remember { FocusRequester() }

val isFocused by interactionSource.collectIsFocusedAsState()

val cursorColor by animateColorAsState(
targetValue = if (isError) {
Expand Down Expand Up @@ -101,7 +103,6 @@ fun EmailAddressTextField(
onValueChange = ::updateUiTextValue,
modifier = modifier
.onFocusChanged { focusState ->
isFocused = focusState.isFocused
if (focusState.isFocused) {
val newFocus = Focus()
if (interactionSource.tryEmit(newFocus)) currentFocus = newFocus
Expand All @@ -114,7 +115,7 @@ fun EmailAddressTextField(
val shouldFocusLastChip = isFocused && validatedEmails.get().isNotEmpty()
if (event.type == KeyEventType.KeyDown && event.key == Key.Backspace && shouldFocusLastChip) {
runCatching {
focusRequester.requestFocus()
lastChipFocusRequester.requestFocus()
}.onFailure {
SentryLog.e(EMAIL_FIELD_TAG, "The focusRequested wasn't registered with a non empty Chip list", it)
}
Expand Down Expand Up @@ -149,7 +150,11 @@ fun EmailAddressTextField(
itemVerticalAlignment = Alignment.CenterVertically,
) {
validatedEmails.get().forEach { email ->
val chipModifier = if (email == validatedEmails.get().lastOrNull()) Modifier.focusRequester(focusRequester) else Modifier
val chipModifier = if (email == validatedEmails.get().lastOrNull()) {
Modifier.focusRequester(lastChipFocusRequester)
} else {
Modifier
}
SwissTransferInputChip(
modifier = chipModifier,
text = email,
Expand Down

0 comments on commit 84d93a2

Please sign in to comment.