Skip to content

Commit

Permalink
fix: [ANDROAPP-5749] focus when changing section
Browse files Browse the repository at this point in the history
  • Loading branch information
mmmateos committed Jan 11, 2024
1 parent 05d9897 commit 42173da
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion form/src/main/java/org/dhis2/form/ui/Form.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@ import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.derivedStateOf
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.focus.FocusDirection
import androidx.compose.ui.graphics.Color
Expand Down Expand Up @@ -55,6 +58,7 @@ fun Form(
}
}
}
var focusNext by remember { mutableStateOf(false) }
LazyColumn(
modifier = Modifier
.fillMaxSize()
Expand All @@ -75,10 +79,14 @@ fun Form(
val isSectionOpen = remember(section.state) {
derivedStateOf { section.state == SectionState.OPEN }
}

LaunchedEffect(isSectionOpen.value) {
if (isSectionOpen.value) {
scrollState.animateScrollToItem(sections.indexOf(section))
focusManager.moveFocus(FocusDirection.Next)
if (focusNext) {
focusManager.moveFocus(FocusDirection.Next)
focusNext = false
}
}
}

Expand Down Expand Up @@ -124,6 +132,7 @@ fun Form(
onNextClicked = {
if (index == section.fields.size - 1) {
onNextSection()
focusNext = true
} else {
focusManager.moveFocus(FocusDirection.Down)
}
Expand Down

0 comments on commit 42173da

Please sign in to comment.