Skip to content

Commit e17fa9f

Browse files
🐛 fix drag and drop into empty list
1 parent c713070 commit e17fa9f

File tree

1 file changed

+24
-3
lines changed

1 file changed

+24
-3
lines changed

client/src/components/Board/Board.jsx

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -222,14 +222,35 @@ function Board({
222222
return;
223223
}
224224

225-
const activeContainer = findContainer(draggedId);
225+
// Find the original container from props (before any drag state changes)
226+
const originalContainer = Object.keys(groupedCards).find((key) =>
227+
groupedCards[key].some((card) => card.id === draggedId),
228+
);
229+
const activeContainer = originalContainer || findContainer(draggedId);
226230
let overContainer = findContainer(overId);
227231

228232
// Handle dropping into empty list via drop zone
229233
if (isOverListDropZone) {
230234
overContainer = overId;
231235
}
232236

237+
// Special case: when active.id === over.id (e.g., dragging first card into empty list)
238+
// The card might have been moved to a different container during handleDragOver
239+
if (draggedId === overId && activeContainer) {
240+
// Check if the card is now in a different container (moved during drag)
241+
const currentContainer = findContainer(draggedId);
242+
if (currentContainer && currentContainer !== activeContainer) {
243+
// Card was moved to a different container, use that as the target
244+
overContainer = currentContainer;
245+
overId = null; // No specific card target, just the container
246+
} else if (activeContainer === currentContainer) {
247+
// Card wasn't moved, this might be a no-op
248+
// If active and over are the same and in same container, treat as no-op
249+
setActiveId(null);
250+
return;
251+
}
252+
}
253+
233254
if (!activeContainer || !overContainer) {
234255
setActiveId(null);
235256
return;
@@ -239,7 +260,7 @@ function Board({
239260
// Moving within the same list
240261
const items = cardItems[activeContainer];
241262
const oldIndex = items.findIndex((item) => item.id === draggedId);
242-
const newIndex = items.findIndex((item) => item.id === overId);
263+
const newIndex = overId ? items.findIndex((item) => item.id === overId) : items.length;
243264

244265
if (oldIndex !== newIndex) {
245266
setCardItems((prev) => ({
@@ -253,7 +274,7 @@ function Board({
253274
} else {
254275
// Moving between different lists
255276
const overItems = cardItems[overContainer] || [];
256-
const overIndex = overItems.findIndex((item) => item.id === overId);
277+
const overIndex = overId ? overItems.findIndex((item) => item.id === overId) : -1;
257278
const newIndex = overIndex >= 0 ? overIndex : overItems.length;
258279

259280
// Get the current board ID from the first list

0 commit comments

Comments
 (0)