Skip to content

Commit

Permalink
chore(@clayui/core): add test for nested items
Browse files Browse the repository at this point in the history
  • Loading branch information
Julien Castelain committed Sep 16, 2021
1 parent 8cfd75c commit a118a2a
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 26 deletions.
5 changes: 5 additions & 0 deletions packages/clay-core/src/tree-view/DragLayer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ function getItemStyles(

return {
WebkitTransform: transform,
backgroundColor: 'white',
border: '1px solid aliceblue',
maxWidth: '140px',
padding: '2px',
textAlign: 'center',
transform,
};
}
Expand Down
46 changes: 46 additions & 0 deletions packages/clay-core/src/tree-view/__tests__/useTree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,50 @@ describe('useTree', () => {

expect(result).toMatchObject(newTree);
});

it('can move nested items', () => {
const tree = [
{
children: [{name: 'Blogs'}, {name: 'Documents and Media'}],
name: 'Liferay Drive',
},
{
children: [{name: 'Blogs'}, {name: 'Documents and Media'}],
name: 'Repositories',
},
{
children: [{name: 'PDF'}, {name: 'Word'}],
name: 'Documents and Media',
},
];

const newTree = [
{
children: [
{name: 'Blogs'},
{
children: [{name: 'PDF'}],
name: 'Documents and Media',
},
],
name: 'Liferay Drive',
},
{
children: [{name: 'Blogs'}, {name: 'Documents and Media'}],
name: 'Repositories',
},
{
children: [{name: 'Word'}],
name: 'Documents and Media',
},
];

const immutableTree = createImmutableTree(tree, 'children');

immutableTree.produce([2, 0], [0, 1]);

const result = immutableTree.applyPatches();

expect(result).toMatchObject(newTree);
});
});
3 changes: 2 additions & 1 deletion packages/clay-core/src/tree-view/useItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export function ItemContextProvider({children, value}: Props) {
preview(getEmptyImage(), {captureDraggingState: true});
}, [preview]);

const [, drop] = useDrop({
const [{overTarget}, drop] = useDrop({
accept: 'treeViewItem',
collect: (monitor) => ({
canDrop: monitor.canDrop(),
Expand All @@ -81,6 +81,7 @@ export function ItemContextProvider({children, value}: Props) {
return (
<ItemContext.Provider value={item}>
{React.cloneElement(children as JSX.Element, {
overTarget,
ref: childRef,
})}
</ItemContext.Provider>
Expand Down
32 changes: 7 additions & 25 deletions packages/clay-core/src/tree-view/useTree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,33 +178,15 @@ export function createImmutableTree<T extends Array<Record<string, any>>>(

const pathToAdd = nodeByPath(path);

if (pathToAdd.parent) {
if (!Array.isArray(pathToAdd.parent[nestedKey])) {
pathToAdd.parent[nestedKey] = [];
}

pathToAdd.parent[nestedKey].splice(pathToAdd.index, 1);
pathToAdd.parent[nestedKey].splice(
pathToAdd.index,
0,
nodeToRemove.item
);
pathToAdd.parent[nestedKey].splice(
pathToAdd.index + 1,
0,
pathToAdd.item
);
} else {
if (!Array.isArray(pathToAdd.item[nestedKey])) {
pathToAdd.item[nestedKey] = [];
}

pathToAdd.item[nestedKey] = [
...pathToAdd.item[nestedKey],
nodeToRemove.item,
];
if (!Array.isArray(pathToAdd.item[nestedKey])) {
pathToAdd.item[nestedKey] = [];
}

pathToAdd.item[nestedKey] = [
...pathToAdd.item[nestedKey],
nodeToRemove.item,
];

break;
}
default:
Expand Down

0 comments on commit a118a2a

Please sign in to comment.