(['0-0', '0-0-0', '0-0-0-0']);
const onDragEnter: TreeProps['onDragEnter'] = (info) => {
console.log(info);
// expandedKeys, set it when controlled is needed
setExpandedKeys(info.expandedKeys);
};
const onDrop: TreeProps['onDrop'] = (info) => {
console.log(info);
const dropKey = info.node.key as number;
const dragKey = info.dragNode.key as number;
const dropPos = info.node.pos.split('-');
const dropPosition = info.dropPosition - Number(dropPos[dropPos.length - 1]);
const loop = (
data: TreeDataNode[],
key: number,
callback: (item: TreeDataNode, index: number, err: TreeDataNode[]) => void,
): void => {
for (let i = 0; i < data.length; i++) {
if (data[i].key === key) {
return callback(data[i], i, data);
}
if (data[i].children) {
loop(data[i].children!, key, callback);
}
}
};
const data = [...gData];
// Find dragObject
let dragObj: TreeDataNode;
loop(data, dragKey, (item, index, arr) => {
arr.splice(index, 1);
dragObj = item;
});
if (!info.dropToGap) {
// Drop on the content
loop(data, dropKey, (item) => {
item.children = item.children || [];
// where to insert. New item was inserted to the end of the array in this example, but can be anywhere
item.children.push(dragObj);
});
} else if (
((info.node as any).props.children || []).length > 0 && // Has children
(info.node as any).props.expanded && // Is expanded
dropPosition === 1 // On the bottom gap
) {
loop(data, dropKey, (item) => {
item.children = item.children || [];
// where to insert. New item was inserted to the start of the array in this example, but can be anywhere
item.children.unshift(dragObj);
});
} else {
let ar: TreeDataNode[];
let i: number;
loop(data, dropKey, (_, index, arr) => {
ar = arr;
i = index;
});
if (dropPosition === -1) {
ar!.splice(i!, 0, dragObj!);
} else {
ar!.splice(i! + 1, 0, dragObj!);
}
}
setGData(data);
};
const innerSetShowLine = (showLine: boolean) => {
if (showLine) {
if (showLeafIcon) {
setShowLine({ showLeafIcon: true });
} else {
setShowLine(true);
}
} else {
setShowLine(false);
}
};
const innerSetShowLeafIcon = (showLeafIcon: boolean) => {
setShowLeafIcon(showLeafIcon);
setShowLine({ showLeafIcon });
};
return (
<>
showLine:
showIcon: setShowIcon(showIcon)} />
showLeafIcon:
>
);
};
export default App;