Advertisement
Guest User

Untitled

a guest
Apr 29th, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React, { Component } from 'react';
  2. import DATA from './data';
  3.  
  4. class ProductsWrapper extends Component {
  5.   constructor(props) {
  6.     super(props);
  7.     this.state = {};
  8.     this.renderProducts = this.renderProducts.bind(this);
  9.  
  10.   }
  11.  
  12.   renderProducts(n) {
  13.     const numberOfColumns = n;
  14.     const numberOfPiecesInOneColumn = Math.ceil(DATA.length / numberOfColumns);
  15.     const arr = [];
  16.     for(var i = 0; i < numberOfColumns; i++) {
  17.       arr.push(null);
  18.     }
  19.     arr.map((_, j) => {
  20.       const data = [];
  21.       for (let i = 0; i < numberOfPiecesInOneColumn; i++) {
  22.         if (DATA[j + i * numberOfColumns]) {
  23.           data.push(DATA[j + i * numberOfColumns]);
  24.         }
  25.       }
  26.       arr[j] = data;
  27.     });
  28.  
  29.     const cols = arr.map((d, i) => (
  30.       <div key={`${i}parent`} className="flex-col" style={{ width: `${100/numberOfColumns}%` }}>
  31.         {d.map((k, j) => (
  32.           <div key={`${j}child`} className="product">
  33.             <div className="inner">
  34.               <img src={`${k.imgAddress}`} alt={`${k.imgName}`} />
  35.               <div className="caption">
  36.                 <p className="product-name">{k.name}</p>
  37.                 <p className="product-author">{k.author}</p>
  38.                 <p className="product-location">{k.location}</p>
  39.               </div>
  40.             </div>
  41.           </div>
  42.         ))}
  43.       </div>
  44.     ));
  45.     return cols;
  46.   }
  47.  
  48.   render() {
  49.     return (
  50.       <div style={{ display: 'flex', padding: '8px', flexDirection: 'row', flexWrap: 'wrap' }}>
  51.         {this.renderProducts(5)}
  52.       </div>
  53.     );
  54.   }
  55. }
  56.  
  57. ProductsWrapper.propTypes = {};
  58.  
  59. export default ProductsWrapper;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement