11import React , { Component } from 'react' ;
2- import { View , Image , TouchableHighlight } from 'react-native' ;
2+ import { View , Image , TouchableHighlight , FlatList } from 'react-native' ;
33import styles from '../styles/main' ;
44import PropTypes from 'prop-types' ;
55import Brick from './Brick' ;
@@ -12,6 +12,12 @@ export default class Column extends Component {
1212 parentDimensions : PropTypes . object ,
1313 columnKey : PropTypes . string ,
1414 imageContainerStyle : PropTypes . object ,
15+ customImageComponent : PropTypes . func ,
16+ customImageProps : PropTypes . object
17+ } ;
18+
19+ static defaultProps = {
20+ imageContainerStyle : { }
1521 } ;
1622
1723 constructor ( props ) {
@@ -53,7 +59,7 @@ export default class Column extends Component {
5359
5460 // Resize image while maintain aspect ratio
5561 // _resizeByColumns :: ImgDimensions , parentDimensions, nColumns -> AdjustedDimensions
56- _resizeByColumns ( imgDimensions , parentDimensions , nColumns = 2 ) {
62+ _resizeByColumns ( imgDimensions = { width : 0 , height : 0 } , parentDimensions , nColumns = 2 ) {
5763 const { height, width } = parentDimensions ;
5864
5965 // The gutter is 1% of the available view width
@@ -78,30 +84,54 @@ export default class Column extends Component {
7884 }
7985
8086 // Renders the "bricks" within the columns
81- // _renderBricks :: [images] -> [TouchableTag || ImageTag...]
82- _renderBricks ( bricks ) {
83- return bricks . map ( ( brick , index ) => {
84- const gutter = ( index === 0 ) ? 0 : brick . gutter ;
85- const key = `RN-MASONRY-BRICK-${ brick . column } -${ index } ` ;
86- const { imageContainerStyle } = this . props ;
87- const props = { ...brick , gutter, key, imageContainerStyle } ;
87+ // _renderBrick :: images -> [TouchableTag || ImageTag...]
88+ _renderBrick = ( data ) => {
89+ // Example Data Structure
90+ // {
91+ // "item": {
92+ // "uri": "https://img.buzzfeed.com/buzzfeed-static/static/2016-01/14/20/campaign_images/webdr15/which-delicious-mexican-food-item-are-you-based-o-2-20324-1452822970-1_dblbig.jpg",
93+ // "column": 0,
94+ // "dimensions": {
95+ // "width": 625,
96+ // "height": 415
97+ // },
98+ // "width": 180.675,
99+ // "height": 119.96820000000001,
100+ // "gutter": 3.65
101+ // },
102+ // "index": 9
103+ // }
104+ const brick = data . item ;
105+ const gutter = ( data . index === 0 ) ? 0 : brick . gutter ;
106+ const key = `RN-MASONRY-BRICK-${ brick . column } -${ data . index } ` ;
107+ const { imageContainerStyle, customImageComponent, customImageProps } = this . props ;
108+ const props = { ...brick , gutter, key, imageContainerStyle, customImageComponent, customImageProps } ;
88109
89110 return (
90111 < Brick
91112 { ...props } />
92113 ) ;
93- } ) ;
94114 }
115+
116+ // _keyExtractor :: item -> id
117+ _keyExtractor = ( item ) => ( item . id || item . key ) ;
95118
96119 render ( ) {
97120 return (
98121 < View
99- key = { this . props . columnKey }
100122 style = { [
101- { width : this . state . columnWidth } ,
123+ {
124+ width : this . state . columnWidth ,
125+ overflow : 'hidden'
126+ } ,
102127 styles . masonry__column
103128 ] } >
104- { this . _renderBricks ( this . state . images ) }
129+ < FlatList
130+ key = { this . props . columnKey }
131+ data = { this . state . images }
132+ keyExtractor = { this . _keyExtractor }
133+ renderItem = { this . _renderBrick }
134+ />
105135 </ View >
106136 )
107137 }
0 commit comments