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
11 changes: 11 additions & 0 deletions packages/react-stately/src/data/useTreeData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,14 @@ export function useTreeData<T extends object>(options: TreeOptions<T>): TreeData
},
move(key: Key, toParentKey: Key | null, index: number) {
setItems(({items, nodeMap: originalMap}) => {
let current = toParentKey;
while (current != null) {
if (current === key) {
throw new Error('Cannot move an item to be a child of itself.');
}
current = originalMap.get(current)?.parentKey ?? null;
}

let node = originalMap.get(key);
if (!node) {
return {items, nodeMap: originalMap};
Expand Down Expand Up @@ -497,6 +505,9 @@ function moveItems<T extends object>(
}
parent = nodeMap.get(parent.parentKey!) ?? null;
}
if (parent != null && removeKeys.has(parent.key)) {
throw new Error('Cannot move an item to be a child of itself.');
}

let originalToIndex = toIndex;

Expand Down
53 changes: 47 additions & 6 deletions packages/react-stately/test/data/useTreeData.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -903,7 +903,7 @@ describe('useTreeData', function () {
expect(result.current.items[0].children[0].children.length).toEqual(3);
});

describe('moveBefore error', function () {
describe('move errors', function () {
const consoleError = console.error;
beforeEach(() => {
console.error = jest.fn();
Expand All @@ -913,15 +913,56 @@ describe('useTreeData', function () {
console.error = consoleError;
});

it('cannot move an item within itself', function () {
const initialItems = [...initial, {name: 'Emily'}, {name: 'Eli'}];

let {result} = renderHook(() => useTreeData({initialItems, getChildren, getKey}));
let expectToThrow = cb => {
let didThrow = false;
try {
act(() => result.current.moveBefore('Suzie', ['John', 'Sam', 'Eli']));
cb();
} catch (e) {
didThrow = true;
expect(e.toString()).toContain('Cannot move an item to be a child of itself.');
}

if (!didThrow) {
expect(console.error).toHaveBeenCalled();
const errorString = console.error.mock.calls.flat().join(' ');

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added additional Regression test for safety Tell me if you dont want these.

expect(errorString).toContain('Cannot move an item to be a child of itself.');
}
};

it('cannot move an item before itself', function () {
const initialItems = [...initial, {name: 'Emily'}, {name: 'Eli'}];
let {result} = renderHook(() => useTreeData({initialItems, getChildren, getKey}));
expectToThrow(() => act(() => result.current.moveBefore('Suzie', ['John', 'Sam', 'Eli'])));
});

it('cannot move a root-level item before itself', function () {
const initialItems = [...initial, {name: 'Emily'}, {name: 'Eli'}];
let {result} = renderHook(() => useTreeData({initialItems, getChildren, getKey}));
expectToThrow(() => act(() => result.current.moveBefore('Suzie', ['David'])));
});

it('cannot move an item after itself', function () {
const initialItems = [...initial, {name: 'Emily'}, {name: 'Eli'}];
let {result} = renderHook(() => useTreeData({initialItems, getChildren, getKey}));
expectToThrow(() => act(() => result.current.moveAfter('Suzie', ['John'])));
});

it('cannot move a root-level item after itself', function () {
const initialItems = [...initial, {name: 'Emily'}, {name: 'Eli'}];
let {result} = renderHook(() => useTreeData({initialItems, getChildren, getKey}));
expectToThrow(() => act(() => result.current.moveAfter('Suzie', ['David'])));
});

it('cannot move an item into itself', function () {
const initialItems = [...initial, {name: 'Emily'}, {name: 'Eli'}];
let {result} = renderHook(() => useTreeData({initialItems, getChildren, getKey}));
expectToThrow(() => act(() => result.current.move('John', 'Suzie', 0)));
});

it('cannot move a root-level item into itself', function () {
const initialItems = [...initial, {name: 'Emily'}, {name: 'Eli'}];
let {result} = renderHook(() => useTreeData({initialItems, getChildren, getKey}));
expectToThrow(() => act(() => result.current.move('David', 'Suzie', 0)));
});
});

Expand Down