From 42173da5ba860da6de8ca9a58ce00b18d94669da Mon Sep 17 00:00:00 2001 From: manu Date: Thu, 11 Jan 2024 09:48:03 +0100 Subject: [PATCH] fix: [ANDROAPP-5749] focus when changing section --- form/src/main/java/org/dhis2/form/ui/Form.kt | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/form/src/main/java/org/dhis2/form/ui/Form.kt b/form/src/main/java/org/dhis2/form/ui/Form.kt index 9f5ac6d8d35..7cfe4f9be61 100644 --- a/form/src/main/java/org/dhis2/form/ui/Form.kt +++ b/form/src/main/java/org/dhis2/form/ui/Form.kt @@ -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 @@ -55,6 +58,7 @@ fun Form( } } } + var focusNext by remember { mutableStateOf(false) } LazyColumn( modifier = Modifier .fillMaxSize() @@ -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 + } } } @@ -124,6 +132,7 @@ fun Form( onNextClicked = { if (index == section.fields.size - 1) { onNextSection() + focusNext = true } else { focusManager.moveFocus(FocusDirection.Down) }