generated from algolia/pwa-ecom-ui-template
-
Notifications
You must be signed in to change notification settings - Fork 14
/
expandable-panel.tsx
92 lines (84 loc) · 2.89 KB
/
expandable-panel.tsx
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
import AddIcon from '@material-design-icons/svg/outlined/add.svg'
import RemoveIcon from '@material-design-icons/svg/outlined/remove.svg'
import classNames from 'classnames'
import { useAtomValue } from 'jotai/utils'
import type { MouseEventHandler } from 'react'
import { memo } from 'react'
import isEqual from 'react-fast-compare'
import type {
CurrentRefinementsProvided,
SearchResults,
} from 'react-instantsearch-core'
import { connectCurrentRefinements } from 'react-instantsearch-dom'
import { withDebugLayer } from '@/components/@dev/debug-layer/debug-layer'
import { Collapse } from '@/components/@ui/collapse/collapse'
import { Count } from '@/components/@ui/count/count'
import { useCurrentRefinementCount } from '@instantsearch/hooks/useCurrentRefinementCount'
import { useHasRefinements } from '@instantsearch/hooks/useHasRefinements'
import { searchResultsAtom } from '@instantsearch/widgets/virtual-state-results/virtual-state-results'
import { Button } from '@ui/button/button'
import { Icon } from '@ui/icon/icon'
export type ExpandablePanelProps = CurrentRefinementsProvided & {
children: React.ReactNode
className?: string
header?: React.ReactNode | string
footer?: string
attributes?: string[]
isOpened?: boolean
onToggle?: MouseEventHandler
}
function ExpandablePanelComponent({
children,
className,
items,
header,
footer,
attributes = [],
isOpened = false,
onToggle,
}: ExpandablePanelProps) {
const searchResults = useAtomValue(searchResultsAtom) as SearchResults
const hasRefinements = useHasRefinements(searchResults, attributes)
const currentRefinementCount = useCurrentRefinementCount(items, attributes)
return (
<div
className={classNames(
'py-3.5 laptop:py-5 laptop:border-t laptop:border-neutral-light',
{
hidden: !hasRefinements,
},
className
)}
>
<Button
className="w-full flex items-center justify-between gap-3 group"
aria-expanded={isOpened}
onClick={(e) => {
if (typeof onToggle === 'function') {
onToggle(e)
}
}}
>
<div className="flex items-center w-full subhead laptop:small-bold laptop:uppercase">
{header || attributes[0]}
{currentRefinementCount > 0 && (
<Count className="ml-auto">{currentRefinementCount}</Count>
)}
</div>
<div className="text-neutral-dark can-hover:transition-colors can-hover:group-hover:text-neutral-light">
{isOpened ? <Icon icon={RemoveIcon} /> : <Icon icon={AddIcon} />}
</div>
</Button>
<Collapse isCollapsed={!isOpened}>
<div className="mt-4">{children}</div>
{footer && <div>{footer}</div>}
</Collapse>
</div>
)
}
export const ExpandablePanel = connectCurrentRefinements<any>(
memo(
withDebugLayer(ExpandablePanelComponent, 'ExpandablePanelWidget'),
isEqual
)
)