Skip to content

Commit cf506e3

Browse files
committed
fix: dont allow dropping on descendant (#227)
1 parent a227ced commit cf506e3

File tree

3 files changed

+43
-7
lines changed

3 files changed

+43
-7
lines changed

next-release-notes.md

+1-7
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
1-
<!--
2-
### Breaking Changes
3-
4-
### Features
5-
61
### Bug Fixes and Improvements
72

8-
### Other Changes
9-
-->
3+
* Fixes bug where dropping an item on its own descendant makes it vanish (#227)

packages/core/src/controlledEnvironment/useOnDragOverTreeHandler.ts

+31
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,31 @@ const getHoveringPosition = (
5454
return { linearIndex, offset, targetItem, targetLinearItem };
5555
};
5656

57+
const useIsDescendant = () => {
58+
const getParentOfLinearItem = useGetGetParentOfLinearItem();
59+
const isDescendant = (
60+
treeId: string,
61+
itemLinearIndex: number,
62+
potentialParents: TreeItem[]
63+
) => {
64+
const { parentLinearIndex, parent } = getParentOfLinearItem(
65+
itemLinearIndex,
66+
treeId
67+
);
68+
69+
if (potentialParents.find(p => p.index === parent.item)) {
70+
return true;
71+
}
72+
73+
if (parent.depth === 0) {
74+
return false;
75+
}
76+
77+
return isDescendant(treeId, parentLinearIndex, potentialParents);
78+
};
79+
return isDescendant;
80+
};
81+
5782
export const useOnDragOverTreeHandler = (
5883
lastDragCode: string,
5984
setLastDragCode: (code: string) => void,
@@ -72,6 +97,7 @@ export const useOnDragOverTreeHandler = (
7297
trees,
7398
} = useTreeEnvironment();
7499
const getParentOfLinearItem = useGetGetParentOfLinearItem();
100+
const isDescendant = useIsDescendant();
75101

76102
return useStableHandler(
77103
(
@@ -157,6 +183,11 @@ export const useOnDragOverTreeHandler = (
157183
linearIndex = parentLinearIndex;
158184
}
159185

186+
if (isDescendant(treeId, linearIndex, draggingItems)) {
187+
onDragAtPosition(undefined);
188+
return;
189+
}
190+
160191
const { depth } = targetItem;
161192
const targetItemData = items[targetItem.item];
162193

packages/core/test/dnd-basics.spec.tsx

+11
Original file line numberDiff line numberDiff line change
@@ -324,5 +324,16 @@ describe('dnd basics', () => {
324324
'after',
325325
]);
326326
});
327+
328+
it('cant drop items on its own descendants', async () => {
329+
const test = await new TestUtil().renderOpenTree();
330+
await test.clickItem('a');
331+
await test.selectItems('a', 'b');
332+
await test.startDrag('a');
333+
await test.dragOver('bb');
334+
await test.drop();
335+
await test.expectTreeUnchanged();
336+
await test.expectItemContentsVisibleAndUnchanged('a', 'b', 'bb');
337+
});
327338
});
328339
});

0 commit comments

Comments
 (0)