Skip to content

Commit 9aafc54

Browse files
committed
Add getItemPathAsItems helper function
1 parent 2e7649e commit 9aafc54

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

src/react-headless-nested-menu.tsx

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,15 @@ interface ToggleAction {
2222
*/
2323
interface OpenPathAction {
2424
type: 'open-path'
25-
id: string
25+
item: MenuItem
2626
}
2727

2828
/**
2929
* @ignore
3030
*/
3131
interface ClosePathAction {
3232
type: 'close-path'
33-
id: string
33+
item: MenuItem
3434
}
3535

3636
type Action = ToggleAction | OpenPathAction | ClosePathAction
@@ -44,6 +44,7 @@ interface NestedMenuState {
4444
items: Items
4545
isOpen: boolean
4646
currentPath: string[]
47+
currentPathItems: MenuItem[]
4748
placement: Placement
4849
}
4950

@@ -55,13 +56,15 @@ const reducer = produce((draft: Draft<NestedMenuState>, action: Action) => {
5556
case 'toggle':
5657
draft.isOpen = !draft.isOpen
5758
draft.currentPath = []
59+
draft.currentPathItems = []
5860
break
5961

6062
case 'open-path':
61-
draft.currentPath.push(action.id)
63+
draft.currentPath.push(action.item.id)
64+
draft.currentPathItems.push(action.item)
6265
break
6366
case 'close-path':
64-
const index = draft.currentPath.indexOf(action.id)
67+
const index = draft.currentPath.indexOf(action.item.id)
6568
draft.currentPath.splice(index)
6669
break
6770
default:
@@ -100,6 +103,7 @@ export const useNestedMenu = ({
100103
items,
101104
isOpen,
102105
currentPath: defaultOpenPath,
106+
currentPathItems: [],
103107
placement,
104108
})
105109

@@ -140,7 +144,7 @@ export const useNestedMenu = ({
140144
if (item.subMenu) {
141145
dispatch({
142146
type: 'open-path',
143-
id: item.id,
147+
item,
144148
})
145149
}
146150
}
@@ -149,7 +153,7 @@ export const useNestedMenu = ({
149153
if (item.subMenu) {
150154
dispatch({
151155
type: 'close-path',
152-
id: item.id,
156+
item,
153157
})
154158
}
155159
}
@@ -283,6 +287,7 @@ export const useNestedMenu = ({
283287
})
284288

285289
const getItemPath = (item: MenuItem) => [...state.currentPath, item.id]
290+
const getItemPathAsItems = (item: MenuItem) => [...state.currentPathItems, item]
286291

287292
// still not working properly
288293
const anchorRef = React.useRef<HTMLElement>()
@@ -297,6 +302,7 @@ export const useNestedMenu = ({
297302
getCloseTriggerProps,
298303
getToggleTriggerProps,
299304
getItemPath,
305+
getItemPathAsItems,
300306
isOpen: state.isOpen,
301307
currentPath: state.currentPath,
302308
openPath,

0 commit comments

Comments
 (0)