This commit is contained in:
2018-12-28 15:18:48 -07:00
parent b3820ca43b
commit 53fa2be4fa
6 changed files with 219 additions and 196 deletions

View File

@@ -4,71 +4,76 @@ import * as Model from "../model";
import * as React from "react";
export interface Props {
images: Model.Image[];
onImageSelected: (image: Model.Image) => void;
width: number;
images: Model.Image[];
onImageSelected: (image: Model.Image) => void;
width: number;
}
export const ROW_HEIGHT = 200;
export const MOBILE_ROW_HEIGHT = 100;
interface Row {
images: Model.Image[];
width: number;
images: Model.Image[];
width: number;
}
export class Grid extends React.PureComponent<Props, {}> {
static displayName = "Grid";
static displayName = "Grid";
render() {
let row: Model.Image[] = [];
const rows: Row[] = [];
let rowWidth = 0;
render() {
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;
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);
});
if (height < this._rowHeight()) {
rows.push({
images: row,
width: rowWidth
images: row,
width: rowWidth
});
const images = rows.map(row => {
const height = this.props.width / row.width;
row = [];
rowWidth = image.width / image.height;
} else {
rowWidth = newWidth;
}
row.push(image);
});
rows.push({
images: row,
width: rowWidth
});
const pics = row.images.map(image => {
return <Picture
image={image}
onClick={() => this.props.onImageSelected(image)}
key={image.src}
width={image.width/image.height * height}
/>
});
return <div
className="Grid-row"
style={{height: height + "px"}}
key={row.images.map(image => image.src).join(",")}>
{pics}
</div>;
});
const images = rows.map(row => {
const height = this.props.width / row.width;
return <div className="Grid">{images}</div>;
}
const pics = row.images.map(image => {
return (
<Picture
image={image}
onClick={() => this.props.onImageSelected(image)}
key={image.src}
width={(image.width / image.height) * height}
/>
);
});
return (
<div
className="Grid-row"
style={{ height: height + "px" }}
key={row.images.map(image => image.src).join(",")}
>
{pics}
</div>
);
});
private _rowHeight = (): number =>
this.props.width > 500 ? ROW_HEIGHT : MOBILE_ROW_HEIGHT;
return <div className="Grid">{images}</div>;
}
private _rowHeight = (): number =>
this.props.width > 500 ? ROW_HEIGHT : MOBILE_ROW_HEIGHT;
}