forked from huanghanzhilian/c-shopping
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSidebar.jsx
150 lines (139 loc) · 6.38 KB
/
Sidebar.jsx
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
'use client'
import { useEffect } from 'react'
import Link from 'next/link'
import { Disclosure } from '@headlessui/react'
import { Icons, SidebarSkeleton, LogoH } from 'components'
import { useDisclosure } from 'hooks'
import { useGetCategoriesQuery } from '@/store/services'
export default function Sidebar() {
//? Assets
const [isSidebar, sidebarHandlers] = useDisclosure()
//? Get Categories Query
const { categoriesList, isLoading } = useGetCategoriesQuery(undefined, {
selectFromResult: ({ data, isLoading }) => ({
categoriesList: data?.data?.categoriesList,
isLoading,
}),
})
//? Handlers
const handleClose = () => sidebarHandlers.close()
//? Re-Renders
//* prevent scroll
useEffect(() => {
if (isSidebar) document.body.style.overflow = 'hidden'
else document.body.style.overflow = 'unset'
}, [isSidebar])
//? Render(s)
return (
<>
<button className="p-1 lg:hidden" type="button" onClick={sidebarHandlers.open}>
<Icons.Bars className="icon" />
</button>
<div
className={`w-full h-screen fixed duration-200 z-10 top-0 lg:hidden ${
isSidebar ? 'right-0' : '-right-full'
} `}
>
<div
className={`${
isSidebar ? 'opacity-100 visible duration-300 delay-200' : 'opacity-0 invisible '
} bg-gray-100/50 z-10 w-full h-full`}
onClick={sidebarHandlers.close}
/>
<div className="overflow-y-auto absolute py-4 top-0 right-0 z-20 w-3/4 h-screen max-w-sm space-y-4 bg-white">
<LogoH className="h-10 ml-3 w-28" />
<h5 className="p-3 border-t-2 border-gray-200">商品分类</h5>
{isLoading ? (
<SidebarSkeleton />
) : categoriesList ? (
<div>
{categoriesList.children &&
categoriesList.children.map(category => (
<Disclosure key={category._id}>
{({ open }) => (
<>
<Disclosure.Button className="flex items-center justify-between px-4 py-2 w-full !mt-0">
<span
className={`pl-3 font-semibold tracking-wide ${
open ? 'text-red-400' : 'text-gray-600'
}`}
>
{category.name}
</span>
<Icons.ArrowDown
className={` ${
open ? 'rotate-180 transform text-red-400 ' : 'text-gray-700'
} w-7 h-7 bg-gray-50 rounded-2xl`}
/>
</Disclosure.Button>
<Disclosure.Panel className=" text-sm bg-gray-100 text-gray-500 !mt-0">
<Link
href={`/main/${category.slug}`}
className="py-2 text-gray-500 inline-flex items-center text-sm max-w-max pl-7"
onClick={handleClose}
>
此类别所有分类
<Icons.ArrowRight2 className="text-gray-500 icon" />
</Link>
{category?.children &&
category.children.map(category => (
<Disclosure key={category._id}>
{({ open }) => (
<>
<Disclosure.Button className="flex items-center justify-between px-4 py-2 w-full !mt-0 pl-7">
<span
className={`font-medium text-md ${
open ? 'text-red-400' : 'text-gray-600'
}`}
>
{category.name}
</span>
<Icons.ArrowDown
className={` ${
open
? 'rotate-180 transform text-red-400 '
: 'text-gray-700'
} w-7 h-7 bg-gray-50 rounded-2xl`}
/>
</Disclosure.Button>
<Disclosure.Panel
className={`px-4 pt-2 pb-1 text-sm text-gray-500 !mt-0
${open ? 'border-b border-gray-50' : ''}
`}
>
<Link
href={`/products?category=${category.slug}`}
className="py-2 text-gray-500 inline-flex items-center text-sm max-w-max pl-9"
onClick={handleClose}
>
此类别所有分类
<Icons.ArrowRight2 className="text-gray-500 icon" />
</Link>
{category.children &&
category.children.map(category => (
<Link
key={category._id}
href={`/products?category=${category.slug}`}
className="pl-9 py-2.5 my-2 font-normal tracking-wide block"
onClick={handleClose}
>
{category.name}
</Link>
))}
</Disclosure.Panel>
</>
)}
</Disclosure>
))}
</Disclosure.Panel>
</>
)}
</Disclosure>
))}
</div>
) : null}
</div>
</div>
</>
)
}