-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathcomplex-menu.js
84 lines (78 loc) · 2.05 KB
/
complex-menu.js
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
import React from "react"
import { useStaticQuery, graphql, Link } from "gatsby"
const ComplexMenu = () => {
const data = useStaticQuery(graphql`
{
kontentItemNavigationItem(
system: { codename: { eq: "website_root_navigation" } }
) {
url
elements {
title {
value
}
subitems {
value {
... on kontent_item_navigation_item {
url
elements {
title {
value
}
subitems {
value {
... on kontent_item_navigation_item {
url
elements {
title {
value
}
subitems {
value {
... on kontent_item_navigation_item {
url
elements {
title {
value
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
`)
const rootMenu = data.kontentItemNavigationItem
const constructMenu = menuItem => {
const url = menuItem.url
const title = menuItem.elements.title.value
const itemHtml = (
<li key={url}>
<Link to={url}>{title}</Link>
</li>
)
if (!menuItem.elements.subitems) {
return itemHtml
} else {
const subMenu = menuItem.elements.subitems.value.map(item =>
constructMenu(item)
)
return [itemHtml, <ul key={`submenu-${url}`}>{subMenu}</ul>]
}
}
return (
<nav>
<ul>{constructMenu(rootMenu)}</ul>
</nav>
)
}
export default ComplexMenu