@@ -15,14 +15,18 @@ import androidx.compose.foundation.lazy.itemsIndexed
15
15
import androidx.compose.foundation.lazy.rememberLazyListState
16
16
import androidx.compose.runtime.Composable
17
17
import androidx.compose.runtime.LaunchedEffect
18
+ import androidx.compose.runtime.MutableState
18
19
import androidx.compose.runtime.derivedStateOf
20
+ import androidx.compose.runtime.mutableStateOf
19
21
import androidx.compose.runtime.remember
20
22
import androidx.compose.runtime.rememberCoroutineScope
21
23
import androidx.compose.ui.Modifier
22
24
import androidx.compose.ui.focus.FocusDirection
25
+ import androidx.compose.ui.focus.FocusManager
23
26
import androidx.compose.ui.graphics.Color
24
27
import androidx.compose.ui.platform.LocalFocusManager
25
28
import androidx.compose.ui.unit.dp
29
+ import kotlinx.coroutines.CoroutineScope
26
30
import kotlinx.coroutines.launch
27
31
import org.dhis2.commons.resources.ResourceManager
28
32
import org.dhis2.form.model.FieldUiModel
@@ -55,6 +59,7 @@ fun Form(
55
59
}
56
60
}
57
61
}
62
+ val focusNext = remember { mutableStateOf(false ) }
58
63
LazyColumn (
59
64
modifier = Modifier
60
65
.fillMaxSize()
@@ -75,11 +80,10 @@ fun Form(
75
80
val isSectionOpen = remember(section.state) {
76
81
derivedStateOf { section.state == SectionState .OPEN }
77
82
}
78
- LaunchedEffect (isSectionOpen.value) {
79
- if (isSectionOpen.value) {
80
- scrollState.animateScrollToItem(sections.indexOf(section))
81
- focusManager.moveFocus(FocusDirection .Next )
82
- }
83
+
84
+ LaunchIfTrue (isSectionOpen.value) {
85
+ scrollState.animateScrollToItem(sections.indexOf(section))
86
+ focusManager.moveFocusNext(focusNext)
83
87
}
84
88
85
89
val onNextSection: () -> Unit = {
@@ -124,6 +128,7 @@ fun Form(
124
128
onNextClicked = {
125
129
if (index == section.fields.size - 1 ) {
126
130
onNextSection()
131
+ focusNext.value = true
127
132
} else {
128
133
focusManager.moveFocus(FocusDirection .Down )
129
134
}
@@ -140,6 +145,22 @@ fun Form(
140
145
}
141
146
}
142
147
148
+ private fun FocusManager.moveFocusNext (focusNext : MutableState <Boolean >) {
149
+ if (focusNext.value) {
150
+ this .moveFocus(FocusDirection .Next )
151
+ focusNext.value = false
152
+ }
153
+ }
154
+
155
+ @Composable
156
+ private fun LaunchIfTrue (key : Boolean , block : suspend CoroutineScope .() -> Unit ) {
157
+ LaunchedEffect (key) {
158
+ if (key) {
159
+ block()
160
+ }
161
+ }
162
+ }
163
+
143
164
private fun getNextSection (section : FormSection , sections : List <FormSection >): FormSection ? {
144
165
val currentIndex = sections.indexOf(section)
145
166
if (currentIndex != - 1 && currentIndex < sections.size - 1 ) {
0 commit comments