Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 40 additions & 1 deletion packages/@react-aria/dnd/src/useDroppableCollection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
clearGlobalDnDState,
DIRECTORY_DRAG_TYPE,
droppableCollectionMap,
getItemElement,
getTypes,
globalDndState,
isInternalDropOperation,
Expand Down Expand Up @@ -273,6 +274,25 @@ export function useDroppableCollection(props: DroppableCollectionOptions, state:
first = item.parentKey;
}

if (item?.type === 'content') {
Copy link
Member

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?

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);
Copy link
Member

Choose a reason for hiding this comment

The 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.
Is it in case you drop an item inside another?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions packages/@react-aria/dnd/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -383,3 +383,12 @@ export let globalAllowedDropOperations: DROP_OPERATION = DROP_OPERATION.none;
export function setGlobalAllowedDropOperations(o: DROP_OPERATION): void {
globalAllowedDropOperations = o;
}

export function getItemElement(collectionRef: RefObject<HTMLElement | null>, key: Key): Element | null | undefined {
let selector = `[data-key="${CSS.escape(String(key))}"]`;
let collection = collectionRef.current?.dataset.collection;
if (collection) {
selector = `[data-collection="${CSS.escape(collection)}"]${selector}`;
}
return collectionRef.current?.querySelector(selector);
}
57 changes: 56 additions & 1 deletion packages/react-aria-components/test/Tree.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ import {useTreeData} from 'react-stately';

let {
EmptyTreeStaticStory: EmptyLoadingTree,
LoadingStoryDepOnTopStory: LoadingMoreTree
LoadingStoryDepOnTopStory: LoadingMoreTree,
TreeWithDragAndDrop
} = composeStories(stories);

let onSelectionChange = jest.fn();
Expand Down Expand Up @@ -1924,6 +1925,60 @@ describe('Tree', () => {
expect(onRootDrop).toHaveBeenCalledTimes(1);
});

it('should automatically focus the newly added dropped item', async () => {
const {getAllByRole} = render(<TreeWithDragAndDrop />);

const trees = getAllByRole('treegrid');
const firstTreeRows = within(trees[0]).getAllByRole('row');
const dataTransfer = new DataTransfer();

fireEvent(firstTreeRows[1], new DragEvent('dragstart', {dataTransfer, clientX: 5, clientY: 5}));
act(() => jest.runAllTimers());

fireEvent(trees[1], new DragEvent('dragenter', {dataTransfer, clientX: 50, clientY: 50}));
fireEvent(trees[1], new DragEvent('dragover', {dataTransfer, clientX: 50, clientY: 50}));
expect(trees[1]).toHaveAttribute('data-drop-target', 'true');

// ¯\_(ツ)_/¯
await act(async () => fireEvent(trees[1], new DragEvent('drop', {dataTransfer, clientX: 50, clientY: 50})));
act(() => jest.runAllTimers());

let secondTreeRows = within(trees[1]).getAllByRole('row');

expect(secondTreeRows).toHaveLength(1);
// The newly added row in the second tree should be the active element
expect(secondTreeRows[0]).toBe(document.activeElement);

await user.click(document.body);
act(() => jest.runAllTimers());

await user.tab();
await user.keyboard('{ArrowRight}'); // expand the projects item
await user.keyboard('{ArrowRight}');
await user.keyboard('{Enter}');
act(() => jest.runAllTimers());

await user.tab();
act(() => jest.runAllTimers());

secondTreeRows = within(trees[1]).getAllByRole('row');
expect(secondTreeRows).toHaveLength(4);
expect(within(secondTreeRows[3]).getAllByRole('button')[0]).toHaveAttribute('aria-label', 'Insert after Reports');
expect(document.activeElement).toBe(within(secondTreeRows[3]).getAllByRole('button')[0]);
expect(secondTreeRows[3]).toHaveAttribute('data-drop-target', 'true');

await user.keyboard('{ArrowUp}');
expect(within(secondTreeRows[2]).getAllByRole('button')[0]).toHaveAttribute('aria-label', 'Drop on Reports');
expect(document.activeElement).toBe(within(secondTreeRows[2]).getAllByRole('button')[0]);

await user.keyboard('{Enter}');
act(() => jest.runAllTimers());

secondTreeRows = within(trees[1]).getAllByRole('row');
expect(secondTreeRows).toHaveLength(1);
expect(secondTreeRows[0]).toBe(document.activeElement);
});

it('should support disabled drag and drop', async () => {
let {getByRole, queryAllByRole} = render(
<DraggableTree isDisabled />
Expand Down
Loading