Skip to content

Commit dee7c31

Browse files
committed
Refactor: Immutable item enrichment in mobile-menu and nav-menu
Replace mutate-in-map with spread-in-map so enrichment helpers don't mutate the caller's input. Two callsites surfaced under freeze-on-set: - mobile-menu addNavIcons: was mutating item.navIcon on items from settings.menu, silently modifying the caller's menu tree - nav-menu addSelectedIndex: was mutating item.selectedIndex on items from settings.menu, same issue; inner .map passes now use the returned value instead of relying on side effects
1 parent 4d75655 commit dee7c31

2 files changed

Lines changed: 20 additions & 18 deletions

File tree

src/components/mobile-menu/mobile-menu.js

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -100,16 +100,13 @@ const createComponent = ({ tpl, settings, $, state, flush, afterFlush, dispatchE
100100

101101
// add -> icon if the menu has a sub menu
102102
addNavIcons(menu) {
103-
if (!settings.navIcon) {
103+
if (!settings.navIcon || !menu?.menu) {
104104
return menu;
105105
}
106-
(menu?.menu || []).map(item => {
107-
if (item.menu) {
108-
item.navIcon = settings.navIcon;
109-
}
110-
return item;
111-
});
112-
return menu;
106+
return {
107+
...menu,
108+
menu: menu.menu.map(item => item.menu ? { ...item, navIcon: settings.navIcon } : item),
109+
};
113110
},
114111
addTrailingSlash(url) {
115112
return (url.substr(-1) === '/')

src/components/nav-menu/nav-menu.js

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -147,25 +147,30 @@ const createComponent = ({ $, el, self, settings, state, reaction, isRendered })
147147
let firstMatch = false;
148148
const searchTermChanged = self.lastSearchTerm !== searchTerm;
149149
const addSelectedIndex = (item) => {
150-
if (item?.url) {
151-
selectedIndex++;
152-
item.selectedIndex = selectedIndex;
150+
if (!item?.url) {
151+
return item;
153152
}
153+
selectedIndex++;
154154
// reset selected index only when search term changes
155-
if (searchTermChanged && !firstMatch && item.highlight && item?.url) {
155+
if (searchTermChanged && !firstMatch && item.highlight) {
156156
state.selectedIndex.set(selectedIndex);
157157
firstMatch = true;
158158
}
159-
return item;
159+
return { ...item, selectedIndex };
160160
};
161161
// add selected index for each menu, pages and subpages (3 deep max)
162162
menu = menu.map(currentMenu => {
163-
currentMenu = addSelectedIndex(currentMenu);
164-
(currentMenu?.pages || []).map(page => {
165-
page = addSelectedIndex(page);
166-
(page?.pages || []).map(subPage => addSelectedIndex(subPage));
163+
const withIndex = addSelectedIndex(currentMenu);
164+
if (!withIndex?.pages) { return withIndex; }
165+
const pages = withIndex.pages.map(page => {
166+
const pageWithIndex = addSelectedIndex(page);
167+
if (!pageWithIndex?.pages) { return pageWithIndex; }
168+
return {
169+
...pageWithIndex,
170+
pages: pageWithIndex.pages.map(subPage => addSelectedIndex(subPage)),
171+
};
167172
});
168-
return currentMenu;
173+
return { ...withIndex, pages };
169174
});
170175
self.lastSearchTerm = searchTerm;
171176
state.maxIndex.set(selectedIndex);

0 commit comments

Comments
 (0)