Last active
          October 7, 2018 01:54 
        
      - 
      
- 
        Save horaciosystem/e348f7d8e9a254d6717a438d932af9b3 to your computer and use it in GitHub Desktop. 
    React Grid component to render a specific amount of lines with cross screen width capability
  
        
  
    
      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 { withSize } from "react-sizeme" | |
| import { take } from "lodash/fp" | |
| import styled from "styled-components" | |
| const CONTAINER_SIDE_PADDING = 20 | |
| const CONTAINER_TOTAL_PADDING = CONTAINER_SIDE_PADDING * 2 | |
| const ListContainer = styled.div` | |
| display: flex; | |
| flex-wrap: ${({ isMultiLine }) => (isMultiLine ? "wrap" : "nowrap")}; | |
| padding: ${CONTAINER_SIDE_PADDING}px; | |
| justify-content: center; | |
| ` | |
| export function Grid({ | |
| items, | |
| gutterWidth, | |
| gutterHeight, | |
| cardComponent: Card, | |
| cardWidth = 272, | |
| lines = 1, | |
| size | |
| }) { | |
| const columnWidth = cardWidth + gutterWidth | |
| const amountToRenderPerLine = Math.floor( | |
| (size.width - CONTAINER_TOTAL_PADDING) / columnWidth | |
| ) | |
| const itemsToRender = take(amountToRenderPerLine * lines, items) | |
| const isMultiLine = lines > 1 | |
| return ( | |
| <ListContainer isMultiLine={isMultiLine}> | |
| {itemsToRender.map(item => ( | |
| <div | |
| style={{ | |
| marginRight: gutterWidth, | |
| marginBottom: gutterHeight | |
| }} | |
| key={item.id} | |
| > | |
| <Card item={item} /> | |
| </div> | |
| ))} | |
| </ListContainer> | |
| ) | |
| } | |
| export default withSize()(Grid) | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment
  
            
Tests: