forked from vtex-apps/search-result
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGallery.js
97 lines (82 loc) · 2.41 KB
/
Gallery.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
import React from 'react'
import classNames from 'classnames'
import PropTypes from 'prop-types'
import { map, pluck, addIndex } from 'ramda'
import { useRuntime } from 'vtex.render-runtime'
import { LAYOUT_MODE } from './components/LayoutModeSwitcher'
import { productShape } from './constants/propTypes'
import GalleryItem from './components/GalleryItem'
import withResizeDetector from './components/withResizeDetector'
import searchResult from './searchResult.css'
/** Layout with two column */
const TWO_COLUMN_ITEMS = 2
/**
* Canonical gallery that displays a list of given products.
*/
const Gallery = ({
products = [],
mobileLayoutMode = LAYOUT_MODE[0].value,
maxItemsPerRow = 5,
minItemWidth = 240,
width,
summary,
showingFacets,
}) => {
const runtime = useRuntime()
const layoutMode = runtime.hints.mobile ? mobileLayoutMode : 'normal'
const getItemsPerRow = () => {
const maxItems = Math.floor(width / minItemWidth)
return maxItemsPerRow <= maxItems ? maxItemsPerRow : maxItems
}
const renderItem = (item, index) => {
const itemsPerRow =
layoutMode === 'small'
? TWO_COLUMN_ITEMS
: getItemsPerRow() || maxItemsPerRow
const style = {
flexBasis: `${100 / itemsPerRow}%`,
maxWidth: `${100 / itemsPerRow}%`,
}
return (
<div
key={item.productId}
style={style}
className={classNames(searchResult.galleryItem, 'pa4')}
>
<GalleryItem
item={item}
summary={summary}
positionList={index + 1}
displayMode={layoutMode}
/>
</div>
)
}
const galleryClasses = classNames(
searchResult.gallery,
'flex flex-row flex-wrap items-stretch bn ph1 na4',
{
'justify-center': !showingFacets,
'pl9-l': showingFacets,
}
)
const mapWithIndex = addIndex(map)
return (
<div className={galleryClasses}>{mapWithIndex(renderItem, products)}</div>
)
}
Gallery.propTypes = {
/** Container width */
width: PropTypes.number,
/** Products to be displayed. */
products: PropTypes.arrayOf(productShape),
/** ProductSummary props. */
summary: PropTypes.any,
/** Max Items per Row */
maxItemsPerRow: PropTypes.number,
/** Layout mode of the gallery in mobile view */
mobileLayoutMode: PropTypes.oneOf(pluck('value', LAYOUT_MODE)),
/** Min Item Width. */
minItemWidth: PropTypes.number,
}
export default withResizeDetector(Gallery)