generated from algolia/pwa-ecom-ui-template
-
Notifications
You must be signed in to change notification settings - Fork 14
/
rating-selector.tsx
66 lines (60 loc) · 2.06 KB
/
rating-selector.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
import StarFillIcon from '@material-design-icons/svg/outlined/star.svg'
import StarOulineIcon from '@material-design-icons/svg/outlined/star_outline.svg'
import classNames from 'classnames'
import { memo, useMemo, useState } from 'react'
import isEqual from 'react-fast-compare'
import type { RatingMenuProvided } from 'react-instantsearch-core'
import { connectRange } from 'react-instantsearch-dom'
import { Button } from '@/components/@ui/button/button'
import { Icon } from '@/components/@ui/icon/icon'
export type RatingSelectorProps = RatingMenuProvided
function RatingSelectorComponent({
currentRefinement,
min,
max,
count,
refine,
}: RatingSelectorProps) {
const [currentHoverIdx, setCurrentHoverIdx] = useState(-1)
const currRefinement = currentRefinement.min
const currCount = useMemo(
() => count.find((v) => v.value === currRefinement?.toString())?.count || 0,
[count, currRefinement]
)
if (typeof min === 'undefined' || typeof max === 'undefined') return null
return (
<div className="flex items-center">
<span className="mr-1 font-bold text-lg">≥</span>
{[...Array(max)].map((_, i) => (
<Button
key={i} // eslint-disable-line react/no-array-index-key
className={classNames({
'text-neutral-dark': currentHoverIdx >= i,
'hover:text-brand-black': i + 1 === currRefinement,
})}
disabled={i + 1 === currRefinement}
onClick={() => {
setCurrentHoverIdx(-1)
refine({ min: i + 1 })
}}
onMouseEnter={() => {
if (i + 1 !== currRefinement) setCurrentHoverIdx(i)
}}
onMouseLeave={() => setCurrentHoverIdx(-1)}
>
<Icon
icon={
(currRefinement || 0) >= i + 1 ? StarFillIcon : StarOulineIcon
}
/>
</Button>
))}
<div className="small-bold pt-px text-neutral-dark ml-auto pl-4">
{currCount}
</div>
</div>
)
}
export const RatingSelector = connectRange(
memo(RatingSelectorComponent, isEqual)
)