-
Notifications
You must be signed in to change notification settings - Fork 1.4k
fix: Update Tree drag and drop focus #9751
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,6 +14,7 @@ import { | |
| clearGlobalDnDState, | ||
| DIRECTORY_DRAG_TYPE, | ||
| droppableCollectionMap, | ||
| getItemElement, | ||
| getTypes, | ||
| globalDndState, | ||
| isInternalDropOperation, | ||
|
|
@@ -273,6 +274,25 @@ export function useDroppableCollection(props: DroppableCollectionOptions, state: | |
| first = item.parentKey; | ||
| } | ||
|
|
||
| if (item?.type === 'content') { | ||
| let parentKey = item.parentKey; | ||
| while (parentKey) { | ||
| let parent = state.collection.getItem(parentKey); | ||
| if (parent?.type !== 'item') { | ||
| parentKey = parent?.parentKey; | ||
| continue; | ||
| } | ||
| // If an item doesn't exist in the collection, | ||
| // it's because its parent is collapsed, and we should focus its parent instead. | ||
| let item = getItemElement(ref, parentKey); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is this just a check that there is a dom node being rendered? can we just set the collection to being focused and then let the selectable item focus itself? I'm assuming not, but I don't understand why.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In short, if an item does not exist in the collection, it means its parent is collapsed. In this case, we should not focus that item, but rather focus its parent, which is consistent with our previous behavior. This applies not only to the case of dropping an item inside another item, but also to the case of dropping a parent item with children into another tree: the newly inserted item is collapsed by default, so we should focus the top-level parent. |
||
| if (item) { | ||
| first = parentKey; | ||
| break; | ||
| } | ||
| parentKey = parent.parentKey; | ||
| } | ||
| } | ||
|
|
||
| // eslint-disable-next-line max-depth | ||
| if (first != null) { | ||
| state.selectionManager.setFocusedKey(first); | ||
|
|
@@ -312,7 +332,7 @@ export function useDroppableCollection(props: DroppableCollectionOptions, state: | |
|
|
||
| state.selectionManager.setFocused(true); | ||
| } | ||
| }, [localState]); | ||
| }, [localState, ref]); | ||
|
|
||
| let onDrop = useCallback((e: DropEvent, target: DropTarget) => { | ||
| let {state} = localState; | ||
|
|
@@ -445,6 +465,25 @@ export function useDroppableCollection(props: DroppableCollectionOptions, state: | |
| key = item.parentKey; | ||
| } | ||
|
|
||
| if (item?.type === 'content') { | ||
| let parentKey = item.parentKey; | ||
| while (parentKey) { | ||
| let parent = localState.state.collection.getItem(parentKey); | ||
| if (parent?.type !== 'item') { | ||
| parentKey = parent?.parentKey; | ||
| continue; | ||
| } | ||
| // If an item doesn't exist in the collection, | ||
| // it's because its parent is collapsed, and we should focus its parent instead. | ||
| let item = getItemElement(ref, parentKey); | ||
| if (item) { | ||
| key = parentKey; | ||
| break; | ||
| } | ||
| parentKey = parent.parentKey; | ||
| } | ||
| } | ||
|
|
||
| // If the focused item is also selected, the default drop target is after the last selected item. | ||
| // But if the focused key is the first selected item, then default to before the first selected item. | ||
| // This is to make reordering lists slightly easier. If you select top down, we assume you want to | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
would you mind commenting the code here?