Skip to content

Commit 6ef333b

Browse files
committed
If dropping "after" an expanded item with children, change target to be "before" its first actual child item.
1 parent 66f48a7 commit 6ef333b

File tree

1 file changed

+30
-3
lines changed
  • packages/react-aria-components/src

1 file changed

+30
-3
lines changed

packages/react-aria-components/src/Tree.tsx

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -304,9 +304,36 @@ function TreeInner<T extends object>({props, collection, treeRef: ref}: TreeInne
304304

305305
// Handle ambiguous drop position: 'after last child' or 'after parent'
306306
if (target?.type === 'item' && target.dropPosition === 'after') {
307-
let item = state.collection.getItem(target.key);
308-
let parentKey = item?.parentKey;
309-
307+
let currentItem = state.collection.getItem(target.key);
308+
309+
// If dropping "after" an expanded item with children,
310+
// change target to be "before" its first actual child item.
311+
if (currentItem && currentItem.hasChildNodes && state.expandedKeys.has(currentItem.key) && state.collection.getChildren) {
312+
let firstChildItemNode: Node<any> | null = null;
313+
for (let child of state.collection.getChildren(currentItem.key)) {
314+
if (child.type === 'item' || child.type === 'loader') {
315+
firstChildItemNode = child;
316+
break;
317+
}
318+
}
319+
320+
if (firstChildItemNode) {
321+
const newTarget = {
322+
type: 'item',
323+
key: firstChildItemNode.key,
324+
dropPosition: 'before'
325+
} as const;
326+
327+
if (isValidDropTarget(newTarget)) {
328+
tracking.boundaryContext = null;
329+
tracking.lastY = y;
330+
return newTarget;
331+
}
332+
}
333+
}
334+
335+
let parentKey = currentItem?.parentKey;
336+
310337
if (parentKey) {
311338
let parentItem = state.collection.getItem(parentKey);
312339
let isParentExpanded = parentItem && state.expandedKeys.has(parentKey);

0 commit comments

Comments
 (0)