Skip to content

Commit d978363

Browse files
alirezamirianAlireza Miriandannify
authored
fix(@react-stately/date): update parentKey when a tree node is moved to another parent (#1988)
closes #1987 Co-authored-by: Alireza Mirian <[email protected]> Co-authored-by: Danni <[email protected]>
1 parent 5ca2175 commit d978363

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

packages/@react-stately/data/src/useTreeData.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,13 +316,19 @@ export function useTreeData<T extends object>(options: TreeOptions<T>): TreeData
316316
}
317317

318318
items = updateTree(items, key, () => null);
319+
320+
const movedNode = {
321+
...node,
322+
parentKey: toParentKey
323+
};
324+
319325
return updateTree(items, toParentKey, parentNode => ({
320326
key: parentNode.key,
321327
parentKey: parentNode.parentKey,
322328
value: parentNode.value,
323329
children: [
324330
...parentNode.children.slice(0, index),
325-
node,
331+
movedNode,
326332
...parentNode.children.slice(index)
327333
]
328334
}));

packages/@react-stately/data/test/useTreeData.test.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -528,11 +528,23 @@ describe('useTreeData', function () {
528528
expect(result.current.items[0].children[0]).toBe(initialResult.items[0].children[0]);
529529
expect(result.current.items[0].children[1]).not.toBe(initialResult.items[0].children[1]);
530530
expect(result.current.items[0].children[1].children).toHaveLength(2);
531-
expect(result.current.items[0].children[1].children[0]).toBe(initialResult.items[0].children[1].children[1]);
531+
expect(result.current.items[0].children[1].children[0]).toEqual(initialResult.items[0].children[1].children[1]);
532532
expect(result.current.items[0].children[1].children[1]).toBe(initialResult.items[0].children[1].children[0]);
533533
expect(result.current.items[0].children[2]).toBe(initialResult.items[0].children[2]);
534534
});
535535

536+
it('update parentKey when a node is moved to another parent', function () {
537+
const {result} = renderHook(() => useTreeData({initialItems: initial, getChildren, getKey}));
538+
539+
act(() => {
540+
result.current.move('Brad', 'John', 0);
541+
});
542+
543+
const john = result.current.items[0].children[0];
544+
const brad = john.children[0];
545+
expect(brad.parentKey).toBe(john.key);
546+
});
547+
536548
it('should move an item to a different parent', function () {
537549
let {result} = renderHook(() => useTreeData({initialItems: initial, getChildren, getKey}));
538550
let initialResult = result.current;
@@ -547,7 +559,7 @@ describe('useTreeData', function () {
547559
expect(result.current.items[0].children).toHaveLength(3);
548560
expect(result.current.items[0].children[0]).not.toBe(initialResult.items[0].children[0]);
549561
expect(result.current.items[0].children[0].children).toHaveLength(2);
550-
expect(result.current.items[0].children[0].children[0]).toBe(initialResult.items[0].children[1].children[1]);
562+
expect(result.current.items[0].children[0].children[0].value).toBe(initialResult.items[0].children[1].children[1].value);
551563
expect(result.current.items[0].children[0].children[1]).toBe(initialResult.items[0].children[0].children[0]);
552564
expect(result.current.items[0].children[1]).not.toBe(initialResult.items[0].children[1]);
553565
expect(result.current.items[0].children[1].children).toHaveLength(1);

0 commit comments

Comments
 (0)