-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathSelectionListOrderBy.js
114 lines (98 loc) · 3.05 KB
/
SelectionListOrderBy.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
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
import React, { useState, useCallback, useRef } from 'react'
import PropTypes from 'prop-types'
import { injectIntl, intlShape } from 'react-intl'
import classNames from 'classnames'
import { find, propEq } from 'ramda'
import useOutsideClick from '../hooks/useOutsideClick'
import { useRuntime, Link } from 'vtex.render-runtime'
import { IconCaret } from 'vtex.dreamstore-icons'
import searchResult from '../searchResult.css'
const SelectionListOrderBy = ({ orderBy, getLinkProps, options, intl }) => {
const [showDropdown, setShowDropdown] = useState(false)
const orderByRef = useRef(null)
const handleDropdownBtClick = useCallback(
() => setShowDropdown(!showDropdown),
[showDropdown]
)
const handleOutsideClick = useCallback(() => setShowDropdown(false), [])
useOutsideClick(orderByRef, handleOutsideClick, showDropdown)
const renderOptions = () => {
return options.map(option => {
const linkProps = getLinkProps({ ordenation: option.value })
return (
<Link
key={option.value}
page={linkProps.page}
query={linkProps.queryString}
params={linkProps.params}
className="c-on-base f5 ml-auto db no-underline pv4 ph5 hover-bg-muted-4"
>
{option.label}
</Link>
)
})
}
const getOptionTitle = useCallback(
option => find(propEq('value', option), options).label,
[options]
)
const {
hints: { mobile },
} = useRuntime()
const btClass = classNames(
'ph3 pv5 mv0 pointer flex justify-center items-center bg-base c-on-base t-action--small ml-auto bt br bl bb-0 br2 br--top bw1 w-100',
{
'b--muted-4 shadow-1': showDropdown && mobile,
'b--transparent pl1': !showDropdown,
}
)
const contentClass = classNames(
'z-1 absolute bg-base shadow-5 f5 w-100 b--muted-4 br2 ba bw1 br--bottom',
{
db: showDropdown,
dn: !showDropdown,
}
)
const dropdownSort = classNames(
searchResult.dropdownSort,
'relative pt1 dib',
{
'flex-auto justify-center w-100': mobile,
}
)
return (
<div className={dropdownSort} ref={orderByRef}>
<button onClick={handleDropdownBtClick} className={btClass}>
<span
className={`${
searchResult.filterPopupTitle
} c-on-base t-action--small ml-auto`}
>
{getOptionTitle(orderBy)}{' '}
</span>
<span className={`${searchResult.filterPopupArrowIcon} pt1 ml-auto`}>
<IconCaret orientation="down" size={10} />
</span>
</button>
<div className={contentClass}>{renderOptions()}</div>
</div>
)
}
SelectionListOrderBy.propTypes = {
/** Current Ordernation */
orderBy: PropTypes.string,
/** Get Properties to link */
getLinkProps: PropTypes.func,
/** Sort Options*/
options: PropTypes.arrayOf(
PropTypes.shape({
/** Label to Option */
label: PropTypes.string,
/** Value to value */
value: PropTypes.string,
})
),
/** Intl to translations */
intl: intlShape,
}
export default injectIntl(SelectionListOrderBy)