Skip to content

Commit 0f7bd10

Browse files
committed
fix(FilterTextField): Fix synchronization issues
If the text of a `TextField` is updated from a flow, this can lead to a jumping cursor and swallowed characters [1]. For a discussion of the issue see [2]. Fix this by introducing a local mutable state for the text value as suggested in [3]. Fixes #109. [1]: JetBrains/compose-multiplatform#3089 [2]: https://medium.com/androiddevelopers/effective-state-management-for-textfield-in-compose-d6e5b070fbe5 [3]: cashapp/molecule#63 (comment) Signed-off-by: Martin Nonnenmacher <[email protected]>
1 parent 2d92453 commit 0f7bd10

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

src/main/kotlin/composables/FilterTextField.kt

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ import androidx.compose.material.Text
1010
import androidx.compose.material.TextField
1111
import androidx.compose.material.TextFieldDefaults
1212
import androidx.compose.runtime.Composable
13+
import androidx.compose.runtime.getValue
14+
import androidx.compose.runtime.mutableStateOf
15+
import androidx.compose.runtime.remember
16+
import androidx.compose.runtime.setValue
1317
import androidx.compose.ui.Modifier
1418
import androidx.compose.ui.graphics.Color
1519
import androidx.compose.ui.res.painterResource
@@ -26,10 +30,14 @@ fun FilterTextField(
2630
onFilterChange: (String) -> Unit
2731
) {
2832
val filterIcon = painterResource(icon.resource)
33+
var localFilterText by remember(filterText) { mutableStateOf(filterText) }
2934

3035
TextField(
31-
value = filterText,
32-
onValueChange = onFilterChange,
36+
value = localFilterText,
37+
onValueChange = {
38+
localFilterText = it
39+
onFilterChange(it)
40+
},
3341
placeholder = { Text(label) },
3442
singleLine = true,
3543
leadingIcon = { Icon(filterIcon, label) },

0 commit comments

Comments
 (0)