forked from vtex-apps/search-result
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSelectionListOrderBy.js
113 lines (97 loc) · 3.01 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
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 { useRuntime } from 'vtex.render-runtime'
import { IconCaret } from 'vtex.store-icons'
import SelectionListItem from './SelectionListItem'
import useOutsideClick from '../hooks/useOutsideClick'
import searchResult from '../searchResult.css'
const SelectionListOrderBy = ({ intl, orderBy, options }) => {
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 {
hints: { mobile },
} = useRuntime()
const renderOptions = () => {
return options.map(option => {
return (
<SelectionListItem
key={option.value}
onItemClick={handleOutsideClick}
option={option}
/>
)
})
}
const getOptionTitle = useCallback(
option => find(propEq('value', option), options).label,
[options]
)
const btClass = classNames(
'ph3 pv5 mv0 pointer flex items-center justify-between bg-base c-on-base t-action--small 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 justify-center w-100 w-auto-ns center'
)
return (
<div className={dropdownSort} ref={orderByRef}>
<button onClick={handleDropdownBtClick} className={btClass}>
<span
className={classNames(
searchResult.filterPopupTitle,
'c-on-base t-action--small ml-auto-ns'
)}
>
<span
className={classNames('c-muted-2', {
'dn dib-ns': !orderBy.length,
})}
>
{intl.formatMessage({ id: 'store/ordenation.sort-by' })}
</span>{' '}
{getOptionTitle(orderBy)}
</span>
<span className={`${searchResult.filterPopupArrowIcon} ph5 pt1`}>
<IconCaret orientation="down" size={10} />
</span>
</button>
<div className={contentClass}>{renderOptions()}</div>
</div>
)
}
SelectionListOrderBy.propTypes = {
/** Current Ordernation */
orderBy: PropTypes.string,
/** 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)