|
1
|
"use client" |
|
2
|
|
|
3
|
import clsx from "clsx" |
|
4
|
import Link from "next/link" |
|
5
|
import React, { useMemo } from "react" |
|
6
|
import { MenuItem, MenuItemLink, MenuItemSubMenu } from "types" |
|
7
|
import { SelectedMenu } from ".." |
|
8
|
import { TriangleRightMini } from "@medusajs/icons" |
|
9
|
|
|
10
|
type MainNavMobileSubMenuProps = { |
|
11
|
menu: MenuItem[] |
|
12
|
title: string |
|
13
|
setSelectedMenus: React.Dispatch<React.SetStateAction<SelectedMenu>> |
|
14
|
onOpenLink?: () => void |
|
15
|
} |
|
16
|
|
|
17
|
export const MainNavMobileSubMenu = ({ |
|
18
|
menu, |
|
19
|
title, |
|
20
|
setSelectedMenus, |
|
21
|
onOpenLink, |
|
22
|
}: MainNavMobileSubMenuProps) => { |
|
23
|
const filteredItems: (MenuItemLink | MenuItemSubMenu)[] = useMemo(() => { |
|
24
|
return menu.filter( |
|
25
|
(item) => item.type === "link" || item.type === "sub-menu" |
|
26
|
) as (MenuItemLink | MenuItemSubMenu)[] |
|
27
|
}, [menu]) |
|
28
|
return ( |
|
29
|
<div className="flex flex-col gap-[23px] max-h-[90%]"> |
|
30
|
<span |
|
31
|
className="text-compact-small-plus text-medusa-fg-muted uppercase" |
|
32
|
data-testid="menu-title" |
|
33
|
> |
|
34
|
{title} |
|
35
|
</span> |
|
36
|
<ul className="flex flex-col gap-[18px] max-h-full overflow-auto"> |
|
37
|
{filteredItems.map((item, index) => ( |
|
38
|
<li |
|
39
|
key={index} |
|
40
|
className={clsx( |
|
41
|
"text-h1 text-medusa-fg-base cursor-pointer", |
|
42
|
"flex justify-between gap-docs_1" |
|
43
|
)} |
|
44
|
> |
|
45
|
{item.type === "link" && ( |
|
46
|
<Link |
|
47
|
href={item.link} |
|
48
|
className="block w-full" |
|
49
|
onClick={() => onOpenLink?.()} |
|
50
|
data-testid="link-item" |
|
51
|
> |
|
52
|
{item.title} |
|
53
|
</Link> |
|
54
|
)} |
|
55
|
{item.type === "sub-menu" && ( |
|
56
|
<div |
|
57
|
className="w-full flex justify-between gap-docs_1" |
|
58
|
onClick={() => |
|
59
|
setSelectedMenus((prev) => [ |
|
60
|
...prev, |
|
61
|
{ |
|
62
|
title: item.title, |
|
63
|
menu: item.items, |
|
64
|
}, |
|
65
|
]) |
|
66
|
} |
|
67
|
data-testid="sub-menu-item" |
|
68
|
> |
|
69
|
<span>{item.title}</span> |
|
70
|
<TriangleRightMini /> |
|
71
|
</div> |
|
72
|
)} |
|
73
|
</li> |
|
74
|
))} |
|
75
|
</ul> |
|
76
|
</div> |
|
77
|
) |
|
78
|
} |