Created
April 4, 2016 11:13
-
-
Save cryptoquick/e5ec383c6f7f903d388fb4410291b0b9 to your computer and use it in GitHub Desktop.
React Image Grouping Component for React Infinite.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react' | |
import Dimensions from 'react-dimensions' | |
import Infinite from 'react-infinite' | |
import ImageCard from './ImageCard' | |
const ImageGroups = ({ | |
items, | |
containerWidth, | |
itemWidth, | |
}) => { | |
const itemsAcross = Math.floor(containerWidth / itemWidth) | |
return ( | |
<Infinite | |
elementHeight={256} | |
useWindowAsScrollContainer={true} | |
preloadBatchSize={Infinite.containerHeightScaleFactor(2)} | |
> | |
{items.reduce((acc, item, i, arr) => { | |
if (!(i % itemsAcross)) { | |
let group = [] | |
for (let j = i; j < i + itemsAcross; j++) { | |
console.log(arr[j], j) | |
const item = arr[j] | |
if (item) { | |
group.push(<ImageCard image={item} key={`group-item-${j}`} />) | |
} | |
} | |
acc.push(<div className="ImageGroup" key={`image-group-${i}`}> | |
{group} | |
</div>) | |
return acc | |
} | |
else { | |
return acc | |
} | |
}, [])} | |
</Infinite> | |
) | |
} | |
ImageGroups.propTypes = { | |
items: React.PropTypes.array.isRequired | |
} | |
export default Dimensions()(ImageGroups) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment