import { Picture } from "./picture"; import * as Model from "../model"; import * as React from "react"; export interface Props { images: Model.Image[]; onImageSelected: (image: Model.Image) => void; pageBottom: number; width: number; } export const ROW_HEIGHT = 200; export const MOBILE_ROW_HEIGHT = 100; interface Row { images: Model.Image[]; width: number; } export class Grid extends React.PureComponent { static displayName = "Grid"; private gridHeight = 0; render() { this.gridHeight = 0; let row: Model.Image[] = []; const rows: Row[] = []; let rowWidth = 0; this.props.images.forEach(image => { const newWidth = rowWidth + image.width / image.height; const height = this.props.width / newWidth; if (height < this._rowHeight()) { rows.push({ images: row, width: rowWidth }); row = []; rowWidth = image.width / image.height; } else { rowWidth = newWidth; } row.push(image); }); rows.push({ images: row, width: rowWidth }); const images = rows.map(row => { const height = this.props.width / row.width; const pics = row.images.map(image => { return ( this.props.onImageSelected(image)} key={image.src} width={(image.width / image.height) * height} defer={ this.gridHeight > this.props.pageBottom + 2 * this._rowHeight() } /> ); }); this.gridHeight += height; return (
image.src).join(",")} > {pics}
); }); return
{images}
; } private _rowHeight = (): number => this.props.width > 500 ? ROW_HEIGHT : MOBILE_ROW_HEIGHT; }