From b4b17be7bf3e18c15fd9e81e1714f98b6f9ab17e Mon Sep 17 00:00:00 2001 From: Batuhan Tomo <91488737+Rekl0w@users.noreply.github.com> Date: Thu, 18 Apr 2024 15:20:12 +0300 Subject: [PATCH] Fix#6415: Megamenu single array fix (#6416) * Fix#6415 * Update MegaMenu.js * Update MegaMenu.js --------- Co-authored-by: Melloware --- components/lib/megamenu/MegaMenu.js | 45 +++++++++++++++-------------- 1 file changed, 24 insertions(+), 21 deletions(-) diff --git a/components/lib/megamenu/MegaMenu.js b/components/lib/megamenu/MegaMenu.js index 29334d1879..6a035b9fe6 100644 --- a/components/lib/megamenu/MegaMenu.js +++ b/components/lib/megamenu/MegaMenu.js @@ -738,26 +738,29 @@ export const MegaMenu = React.memo( }; const createProcessedItems = (items, level = 0, parent = {}, parentKey = '', columnIndex) => { - const _processedItems = []; - - items && - items.forEach((item, index) => { - const key = (parentKey !== '' ? parentKey + '_' : '') + (columnIndex !== undefined ? columnIndex + '_' : '') + index; - const newItem = { - item, - index, - level, - key, - parent, - parentKey, - columnIndex: columnIndex !== undefined ? columnIndex : parent && parent.columnIndex !== undefined ? parent.columnIndex : index - }; - - newItem.items = level === 0 && item.items && item.items.length > 0 ? item.items.map((_items, _index) => createProcessedItems(_items, level + 1, newItem, key, _index)) : createProcessedItems(item.items, level + 1, newItem, key); - _processedItems.push(newItem); - }); + if (!items) return []; + + // #6415 if passed a single array[] instead of a matrix [][] + if (!Array.isArray(items)) { + items = [items]; + } + + return items.map((item, index) => { + const key = `${parentKey !== '' ? parentKey + '_' : ''}${columnIndex !== undefined ? columnIndex + '_' : ''}${index}`; + const newItem = { + item, + index, + level, + key, + parent, + parentKey, + columnIndex: columnIndex !== undefined ? columnIndex : parent && parent.columnIndex !== undefined ? parent.columnIndex : index + }; + + newItem.items = level === 0 && item.items && item.items.length > 0 ? item.items.map((_items, _index) => createProcessedItems(_items, level + 1, newItem, key, _index)) : createProcessedItems(item.items, level + 1, newItem, key); - return _processedItems; + return newItem; + }); }; const createSeparator = (index) => { @@ -1054,7 +1057,7 @@ export const MegaMenu = React.memo( .p-megamenu[${selector}] .p-menuitem > .p-menuitem-content > .p-menuitem-link > .p-submenu-icon { margin-left: auto; } - + .p-megamenu[${selector}] .p-submenu-list .p-menuitem-content .p-menuitem-link { padding-left: 2.25rem; } @@ -1071,7 +1074,7 @@ export const MegaMenu = React.memo( ${ vertical - ? ` + ? ` .p-megamenu[${selector}] .p-menuitem > .p-menuitem-content > .p-menuitem-link > .p-submenu-icon { transform: rotate(90deg); }