Skip to content
Merged
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
8 changes: 7 additions & 1 deletion packages/@react-stately/data/src/useTreeData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -316,13 +316,19 @@ export function useTreeData<T extends object>(options: TreeOptions<T>): TreeData
}

items = updateTree(items, key, () => null);

const movedNode = {
...node,
parentKey: toParentKey
};

return updateTree(items, toParentKey, parentNode => ({
key: parentNode.key,
parentKey: parentNode.parentKey,
value: parentNode.value,
children: [
...parentNode.children.slice(0, index),
node,
movedNode,
...parentNode.children.slice(index)
]
}));
Expand Down
16 changes: 14 additions & 2 deletions packages/@react-stately/data/test/useTreeData.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -528,11 +528,23 @@ describe('useTreeData', function () {
expect(result.current.items[0].children[0]).toBe(initialResult.items[0].children[0]);
expect(result.current.items[0].children[1]).not.toBe(initialResult.items[0].children[1]);
expect(result.current.items[0].children[1].children).toHaveLength(2);
expect(result.current.items[0].children[1].children[0]).toBe(initialResult.items[0].children[1].children[1]);
expect(result.current.items[0].children[1].children[0]).toEqual(initialResult.items[0].children[1].children[1]);
expect(result.current.items[0].children[1].children[1]).toBe(initialResult.items[0].children[1].children[0]);
expect(result.current.items[0].children[2]).toBe(initialResult.items[0].children[2]);
});

it('update parentKey when a node is moved to another parent', function () {
const {result} = renderHook(() => useTreeData({initialItems: initial, getChildren, getKey}));

act(() => {
result.current.move('Brad', 'John', 0);
});

const john = result.current.items[0].children[0];
const brad = john.children[0];
expect(brad.parentKey).toBe(john.key);
});

it('should move an item to a different parent', function () {
let {result} = renderHook(() => useTreeData({initialItems: initial, getChildren, getKey}));
let initialResult = result.current;
Expand All @@ -547,7 +559,7 @@ describe('useTreeData', function () {
expect(result.current.items[0].children).toHaveLength(3);
expect(result.current.items[0].children[0]).not.toBe(initialResult.items[0].children[0]);
expect(result.current.items[0].children[0].children).toHaveLength(2);
expect(result.current.items[0].children[0].children[0]).toBe(initialResult.items[0].children[1].children[1]);
expect(result.current.items[0].children[0].children[0].value).toBe(initialResult.items[0].children[1].children[1].value);
expect(result.current.items[0].children[0].children[1]).toBe(initialResult.items[0].children[0].children[0]);
expect(result.current.items[0].children[1]).not.toBe(initialResult.items[0].children[1]);
expect(result.current.items[0].children[1].children).toHaveLength(1);
Expand Down