Commit c2fe3a8
fix: use path-based menuId to prevent collisions in nested submenus (#3762)
Closes:
#3606
## Summary
- `prepareMenu` passes `menuIndex` as `index` when recursing into
submenus, losing the original bar button item index
- This causes `menuId` collisions between actions in different submenus
and sibling toolbar menus at the same position
- Fix threads a `path` string through recursion to track nesting depth,
while preserving the original bar button item index
## Problem
Given two toolbar menu buttons — a filter menu at bar button index 1
containing submenus, and a sort menu at bar button index 2:
```
Toolbar:
[+] button (index 0)
[filter] menu (index 1) ← contains Location, Item Type, Quality submenus
[sort] menu (index 2) ← contains Title, Gear Type, Date Added actions
```
When `prepareMenu` recurses into the Quality submenu (at `menuIndex` 2
inside the filter menu), it calls:
```ts
prepareMenu(menuItem, menuIndex, side) // menuIndex=2, replacing index=1
```
The first action inside Quality then gets `menuId: "0-2-right"`, which
collides with the first action in the sort menu (`"0-2-right"`).
Selecting a quality filter fires the sort handler instead.
## Fix
Add a `path` parameter that tracks position through nesting, while
keeping the original bar button `index`:
```ts
const prepareMenu = (
menu, index, side,
path = '', // NEW
) => {
return {
...menu,
items: menu.items.map((menuItem, menuIndex) => {
const currentPath = path ? `${path}.${menuIndex}` : `${menuIndex}`;
// ...
if (menuItem.type === 'submenu') {
return {
...menuItem,
...prepareMenu(menuItem, index, side, currentPath),
// ^^^^^ preserved (was menuIndex)
};
}
return {
...menuItem,
menuId: `${currentPath}-${index}-${side}`,
// ^^^^^^^^^^^^ path-based (was menuIndex)
};
}),
};
};
```
**Before:** Quality "All" = `0-2-right` (collides with Sort "Title" =
`0-2-right`)
**After:** Quality "All" = `2.0-1-right`, Sort "Title" = `0-2-right`
(unique)
## Before & after - visual documentation
| Before | After |
| --- | --- |
| <video
src="https://github.com/user-attachments/assets/44420c7e-7850-45be-b4b2-558c38da7b1a"
/> | <video
src="https://github.com/user-attachments/assets/a0deac60-ac7e-4197-91fe-6943a875d06c"
/> |
## Verified
- Toolbar with multiple menu buttons — actions in each menu fire the
correct handler
- Nested submenus (inline sections) within a toolbar menu — no
cross-talk with sibling toolbar menus
- Single menu button with no submenus — no regression (path defaults to
menuIndex)
---------
Co-authored-by: Tomasz Boroń <tomasz.boron@swmansion.com>1 parent d4f3d71 commit c2fe3a8
3 files changed
Lines changed: 141 additions & 2 deletions
File tree
- apps/src/tests/issue-tests
- src/components/helpers
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
179 | 179 | | |
180 | 180 | | |
181 | 181 | | |
| 182 | + | |
182 | 183 | | |
183 | 184 | | |
184 | 185 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
8 | 8 | | |
9 | 9 | | |
10 | 10 | | |
| 11 | + | |
11 | 12 | | |
12 | 13 | | |
13 | 14 | | |
14 | 15 | | |
| 16 | + | |
15 | 17 | | |
16 | 18 | | |
17 | 19 | | |
| |||
32 | 34 | | |
33 | 35 | | |
34 | 36 | | |
35 | | - | |
| 37 | + | |
36 | 38 | | |
37 | 39 | | |
38 | 40 | | |
| |||
41 | 43 | | |
42 | 44 | | |
43 | 45 | | |
44 | | - | |
| 46 | + | |
45 | 47 | | |
46 | 48 | | |
47 | 49 | | |
| |||
0 commit comments