fix: DnD for moving child before parent with single root item#9841
fix: DnD for moving child before parent with single root item#9841rothsandro wants to merge 2 commits intoadobe:mainfrom
Conversation
|
Thanks for the PR and especially the test, I think we may have a bug in our story. I can see the drop indicator now, but if I drop it, I get this error |
|
Hm, I can't reproduce this error, I properly get the "moving [project-1] before projects" log. Are you using the same Story and updated rows? http://localhost:9003/?path=/story/react-aria-components-tree--tree-with-drag-and-drop diff --git a/packages/react-aria-components/stories/Tree.stories.tsx b/packages/react-aria-components/stories/Tree.stories.tsx
index cf04d5eed..75036d948 100644
--- a/packages/react-aria-components/stories/Tree.stories.tsx
+++ b/packages/react-aria-components/stories/Tree.stories.tsx
@@ -399,31 +399,7 @@ export const TreeExampleStaticNoActions: StoryObj<typeof TreeExampleStaticNoActi
let rows = [
{id: 'projects', name: 'Projects', childItems: [
- {id: 'project-1', name: 'Project 1'},
- {id: 'project-2', name: 'Project 2', childItems: [
- {id: 'project-2A', name: 'Project 2A'},
- {id: 'project-2B', name: 'Project 2B'},
- {id: 'project-2C', name: 'Project 2C'}
- ]},
- {id: 'project-3', name: 'Project 3'},
- {id: 'project-4', name: 'Project 4'},
- {id: 'project-5', name: 'Project 5', childItems: [
- {id: 'project-5A', name: 'Project 5A'},
- {id: 'project-5B', name: 'Project 5B'},
- {id: 'project-5C', name: 'Project 5C'}
- ]}
- ]},
- {id: 'reports', name: 'Reports', childItems: [
- {id: 'reports-1', name: 'Reports 1', childItems: [
- {id: 'reports-1A', name: 'Reports 1A', childItems: [
- {id: 'reports-1AB', name: 'Reports 1AB', childItems: [
- {id: 'reports-1ABC', name: 'Reports 1ABC'}
- ]}
- ]},
- {id: 'reports-1B', name: 'Reports 1B'},
- {id: 'reports-1C', name: 'Reports 1C'}
- ]},
- {id: 'reports-2', name: 'Reports 2'}
+ {id: 'project-1', name: 'Project 1'}
]}
]; |
|
Ah, I did not adjust the story. I went to "Tree With Drag And Drop", dragged one of the top level items over to the second tree, then opened it, and dragged its child before it. |
|
I can reproduce it but doesn't seem to be related to the other problem / fix. It's also reproducible when moving the child item after the root item (which didn't have the missing drop indicator problem). It seems somehow related / caused by the react-spectrum/packages/react-stately/src/data/useTreeData.ts Lines 262 to 267 in 1bd82a0 Edit: It also only happens in strict mode. |
|
Ah, that's great! it'll be fixed by this PR then fix(useTreeData): insert items into a child node with StrictMode Though what were the other issues you found when you passed in a new Map? So we can address those issues here. Thanks for looking into that. |
|
There was only an issue when creating a new map inside |
Closes
See discussion #9796
If a tree contains a single root item with a child, the child can't be moved before the parent element via drag & drop, the drag indicator doesn't appear and dropping it calls
onMovewith an invalid id.I made a fix by checking the node type (as suggested by snowystinger in the discussion), because the bug was caused by a
contentnode which resulted in the invalid id. While checking the type likely makes sense (it's also done further up in the code for other cases), I'm not really confident with the change. I'm not sure what use case the whole Handle converting "after" to "before next" block handles and if the change breaks anything else. The if statement also doesn't check whether the target actually hasafteras drop position, so maybe this would be another way to fix it.✅ Pull Request Checklist:
📝 Test Instructions:
🧢 Your Project: