Skip to content

Update MenuBuilder.tsx #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
68 changes: 50 additions & 18 deletions src/Builder/MenuBuilder.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,14 @@ interface Props {
style?: "bordered" | "shadow";
items: TreeItems;
setItems(items: ((items: any) => TreeItem[]) | TreeItems): void;
maxLevel?: number;
}

export function MenuBuilder({
style = "bordered",
items: itemsProps,
setItems,
maxLevel,
}: Props) {
const items = generateItemChildren(itemsProps);
const indentationWidth = 50;
Expand Down Expand Up @@ -290,25 +292,55 @@ export function MenuBuilder({
}

function handleDragEnd({ active, over }: DragEndEvent) {
resetState();

if (projected && over) {
const { depth, parentId } = projected;
const clonedItems: FlattenedItem[] = JSON.parse(
JSON.stringify(flattenTree(items))
);
const overIndex = clonedItems.findIndex(({ id }) => id === over.id);
const activeIndex = clonedItems.findIndex(({ id }) => id === active.id);
const activeTreeItem = clonedItems[activeIndex];

clonedItems[activeIndex] = { ...activeTreeItem, depth, parentId };

const sortedItems = arrayMove(clonedItems, activeIndex, overIndex);
const newItems = buildTree(sortedItems);

setItems(newItems);
resetState();

if (projected && over) {
const { depth, parentId } = projected;

// Helper function to calculate the new depth of an item and its children
const calculateDepthForSubItems = (item: FlattenedItem, baseDepth: number): boolean => {
if (maxLevel !== undefined && baseDepth >= maxLevel) {
return false; // Return false if any item exceeds maxLevel
}

if (item.children && item.children.length) {
return item.children.every((child) => {
const childItem = clonedItems.find(({ id }) => id === child.id);
if (childItem) {
return calculateDepthForSubItems(childItem, baseDepth + 1); // Recursively calculate depth for each child
}
return true;
});
}

return true;
};

const clonedItems: FlattenedItem[] = JSON.parse(
JSON.stringify(flattenTree(items))
);
const overIndex = clonedItems.findIndex(({ id }) => id === over.id);
const activeIndex = clonedItems.findIndex(({ id }) => id === active.id);
const activeTreeItem = clonedItems[activeIndex];

// Check if the depth exceeds the maxLevel for the dragged item and its children
const isValidDepth = calculateDepthForSubItems(activeTreeItem, depth);

if (!isValidDepth) {
alert('Level Exceed!');
return; // Block the drop if the new depth exceeds maxLevel
}

// Update the depth of the dragged item
clonedItems[activeIndex] = { ...activeTreeItem, depth, parentId };

// Adjust the order of the items
const sortedItems = arrayMove(clonedItems, activeIndex, overIndex);
const newItems = buildTree(sortedItems);

setItems(newItems);
}
}
}

function handleDragCancel() {
resetState();
Expand Down