Skip to content

Commit ed53cfc

Browse files
authored
Fix selection failed to work when data source isn't a static list (#188)
1 parent 28bf5c5 commit ed53cfc

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

core/src/commonMain/kotlin/com/dragselectcompose/core/GridDragSelect.kt

+6-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import androidx.compose.foundation.gestures.detectDragGesturesAfterLongPress
44
import androidx.compose.foundation.gestures.scrollBy
55
import androidx.compose.foundation.lazy.grid.LazyGridState
66
import androidx.compose.runtime.LaunchedEffect
7+
import androidx.compose.runtime.getValue
8+
import androidx.compose.runtime.rememberUpdatedState
79
import androidx.compose.ui.Modifier
810
import androidx.compose.ui.composed
911
import androidx.compose.ui.geometry.Offset
@@ -71,11 +73,13 @@ public fun <Item> Modifier.gridDragSelect(
7173
if (!enableHaptics) null
7274
else hapticFeedback ?: GridDragSelectDefaults.hapticsFeedback
7375

76+
val currentItems by rememberUpdatedState(items)
77+
7478
pointerInput(Unit) {
7579
detectDragGesturesAfterLongPress(
7680
onDragStart = { offset ->
7781
state.gridState.itemIndexAtPosition(offset)?.let { startIndex ->
78-
val item = items.getOrNull(startIndex)
82+
val item = currentItems.getOrNull(startIndex)
7983
if (item != null && state.selected.contains(item).not()) {
8084
haptics?.performHapticFeedback(HapticFeedbackType.LongPress)
8185
state.startDrag(item, startIndex)
@@ -92,7 +96,7 @@ public fun <Item> Modifier.gridDragSelect(
9296
?: return@whenDragging
9397

9498
val newSelection =
95-
items.getSelectedItems(itemPosition, dragState, state::isSelected)
99+
currentItems.getSelectedItems(itemPosition, dragState, state::isSelected)
96100

97101
updateDrag(current = itemPosition)
98102
updateSelected(newSelection)

0 commit comments

Comments
 (0)