From 94b2cab6f2e231ee76ebaf170ab2c51a0ab6fa5f Mon Sep 17 00:00:00 2001 From: flavien Date: Thu, 21 Nov 2024 14:06:42 +0100 Subject: [PATCH] [TreeView] Remove state mutation in moveItemInTree --- .../useTreeViewItemsReordering.utils.ts | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/packages/x-tree-view-pro/src/internals/plugins/useTreeViewItemsReordering/useTreeViewItemsReordering.utils.ts b/packages/x-tree-view-pro/src/internals/plugins/useTreeViewItemsReordering/useTreeViewItemsReordering.utils.ts index 171d961838bc6..e466d7c66532f 100644 --- a/packages/x-tree-view-pro/src/internals/plugins/useTreeViewItemsReordering/useTreeViewItemsReordering.utils.ts +++ b/packages/x-tree-view-pro/src/internals/plugins/useTreeViewItemsReordering/useTreeViewItemsReordering.utils.ts @@ -166,11 +166,18 @@ export const moveItemInTree = ({ const itemMetaLookup = { ...prevState.itemMetaLookup }; // 3.1 Update the `expandable` property of the old and the new parent + function updateExpandable(itemId: string) { + const isExpandable = itemOrderedChildrenIds[itemId].length > 0; + if (itemMetaLookup[itemId].expandable !== isExpandable) { + itemMetaLookup[itemId] = { ...itemMetaLookup[itemId], expandable: isExpandable }; + } + } + if (oldParentId !== TREE_VIEW_ROOT_PARENT_ID && oldParentId !== newParentId) { - itemMetaLookup[oldParentId].expandable = itemOrderedChildrenIds[oldParentId].length > 0; + updateExpandable(oldParentId); } if (newParentId !== TREE_VIEW_ROOT_PARENT_ID && newParentId !== oldParentId) { - itemMetaLookup[newParentId].expandable = itemOrderedChildrenIds[newParentId].length > 0; + updateExpandable(newParentId); } // 3.2 Update the `parentId` and `depth` properties of the item to move