Skip to content

Commit

Permalink
Fix focus issues in InputAge component
Browse files Browse the repository at this point in the history
  • Loading branch information
msasikanth committed Oct 20, 2023
1 parent e3eec73 commit 2f00df8
Showing 1 changed file with 38 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,16 @@ import androidx.compose.material.icons.filled.Event
import androidx.compose.material.icons.outlined.Cancel
import androidx.compose.material3.Icon
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.focus.FocusDirection
import androidx.compose.ui.focus.FocusRequester
import androidx.compose.ui.focus.focusRequester
import androidx.compose.ui.platform.LocalFocusManager
import androidx.compose.ui.platform.testTag
import androidx.compose.ui.text.input.ImeAction
import androidx.compose.ui.text.input.KeyboardType
Expand Down Expand Up @@ -49,6 +57,7 @@ fun InputAge(
supportingText: List<SupportingTextData>? = null,
isRequired: Boolean = false,
imeAction: ImeAction = ImeAction.Next,
onNextClicked: (() -> Unit)? = null,
dateOfBirthLabel: String = provideStringResource("date_birth"),
orLabel: String = provideStringResource("or"),
ageLabel: String = provideStringResource("age"),
Expand Down Expand Up @@ -89,17 +98,35 @@ fun InputAge(
null
}

val focusManager = LocalFocusManager.current
val focusRequester = remember { FocusRequester() }

var previousInputType by remember { mutableStateOf(inputType) }
LaunchedEffect(inputType) {
when {
previousInputType == None && (inputType is DateOfBirth || inputType is Age) -> {
focusRequester.requestFocus()
}
else -> {
// no-op
}
}

if (previousInputType != inputType) {
previousInputType = inputType
}
}

InputShell(
modifier = modifier.testTag("INPUT_AGE"),
modifier = modifier.testTag("INPUT_AGE").focusRequester(focusRequester),
title = title,
state = state,
isRequiredField = isRequired,
inputField = {
when (inputType) {
None -> {
TextButtonSelector(
modifier = Modifier.focusable(true)
.testTag("INPUT_AGE_MODE_SELECTOR"),
modifier = Modifier.testTag("INPUT_AGE_MODE_SELECTOR"),
firstOptionText = dateOfBirthLabel,
onClickFirstOption = {
onValueChanged.invoke(DateOfBirth.EMPTY)
Expand Down Expand Up @@ -140,6 +167,13 @@ fun InputAge(
enabled = state != InputShellState.DISABLED,
state = state,
keyboardOptions = KeyboardOptions(imeAction = imeAction, keyboardType = KeyboardType.Number),
onNextClicked = {
if (onNextClicked != null) {
onNextClicked()
} else {
focusManager.moveFocus(FocusDirection.Down)
}
},
)
}
}
Expand All @@ -155,6 +189,7 @@ fun InputAge(
)
},
onClick = {
focusRequester.requestFocus()
onValueChanged.invoke(None)
},
)
Expand Down

0 comments on commit 2f00df8

Please sign in to comment.