Skip to content

Commit e0a7c38

Browse files
authored
fix drop targets in virtualized tree (#8427)
1 parent 8884fc7 commit e0a7c38

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

packages/@react-stately/layout/src/ListLayout.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -587,6 +587,22 @@ export class ListLayout<T, O extends ListLayoutOptions = ListLayoutOptions> exte
587587
if (target.dropPosition === 'before') {
588588
rect = new Rect(layoutInfo.rect.x, layoutInfo.rect.y - this.dropIndicatorThickness / 2, layoutInfo.rect.width, this.dropIndicatorThickness);
589589
} else if (target.dropPosition === 'after') {
590+
// Render after last visible descendant of the drop target.
591+
let targetNode = this.collection.getItem(target.key);
592+
if (targetNode) {
593+
let targetLevel = targetNode.level ?? 0;
594+
let currentKey = this.collection.getKeyAfter(target.key);
595+
596+
while (currentKey != null) {
597+
let node = this.collection.getItem(currentKey);
598+
if (!node || node.level <= targetLevel) {
599+
break;
600+
}
601+
602+
layoutInfo = this.getLayoutInfo(currentKey) || layoutInfo;
603+
currentKey = this.collection.getKeyAfter(currentKey);
604+
}
605+
}
590606
rect = new Rect(layoutInfo.rect.x, layoutInfo.rect.maxY - this.dropIndicatorThickness / 2, layoutInfo.rect.width, this.dropIndicatorThickness);
591607
} else {
592608
rect = layoutInfo.rect;

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -876,3 +876,22 @@ export const TreeWithDragAndDrop = {
876876
...TreeExampleDynamic.argTypes
877877
}
878878
};
879+
880+
function TreeDragAndDropVirtualizedRender(args) {
881+
return (
882+
<div style={{display: 'flex', gap: 12, flexWrap: 'wrap'}}>
883+
<Virtualizer layout={ListLayout} layoutOptions={{rowHeight: 30}}>
884+
<TreeDragAndDropExample defaultExpandedKeys={['projects', 'reports', 'project-2', 'project-5', 'report-1', 'reports-1', 'reports-1A', 'reports-1AB']} {...args} />
885+
</Virtualizer>
886+
<Virtualizer layout={ListLayout} layoutOptions={{rowHeight: 30}}>
887+
<SecondTree {...args} />
888+
</Virtualizer>
889+
</div>
890+
);
891+
}
892+
893+
export const TreeWithDragAndDropVirtualized = {
894+
...TreeWithDragAndDrop,
895+
render: TreeDragAndDropVirtualizedRender,
896+
name: 'Tree with drag and drop (virtualized)'
897+
};

0 commit comments

Comments
 (0)