-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreact-image-grid.jsx
More file actions
94 lines (78 loc) · 2.15 KB
/
react-image-grid.jsx
File metadata and controls
94 lines (78 loc) · 2.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import React from 'react';
import ReactDOM from 'react-dom';
export default class ImageGrid extends React.Component {
constructor() {
super();
this.state = { containerWidth: 0 };
}
componentDidMount() {
this.setState({
containerWidth: Math.floor(ReactDOM.findDOMNode(this).clientWidth)
});
}
componentDidUpdate() {
if (ReactDOM.findDOMNode(this).clientWidth !== this.state.containerWidth) {
this.setState({
containerWidth: Math.floor(ReactDOM.findDOMNode(this).clientWidth)
});
}
}
componentWillUnmount() {
}
render() {
var limit = 1;
if (this.state.containerWidth >= 480) {
limit = 2;
} else if (this.state.containerWidth >= 1024) {
limit = 3;
}
var containerWidth = this.state.containerWidth - (limit * 4);
var maxHeight = containerWidth;
var images = [];
for (var i = 0; i < this.props.photos.length; i += limit) {
for (var j = i; j < i + limit; j++) {
if (j == this.props.photos.length) { break; }
var src = this.props.photos[j].src;
var width = this.props.photos[j].width;
var height = this.props.photos[j].height;
images.push(
<div key={j} style={imageStyle}>
<img src={src} style={{display:'block', border:0}} height={maxHeight/5} width={''} alt="" />
</div>
);
}
}
return (
this.renderImageGrid(images)
);
}
renderImageGrid(images) {
return (
<div id="image-grid">
{images}
</div>
);
}
};
ImageGrid.displayName = 'ImageGrid';
ImageGrid.defaultProps = {}
ImageGrid.propTypes = {
photos: function(props, propName, componentName) {
shape = {
src: React.PropTypes.string.isRequired,
width: React.PropTypes.number.isRequired
}
properties = React.PropTypes.shape(shape)
return React.PropTypes.arrayOf(properties).isRequired.apply(this,arguments);
}
};
const imageStyle = {
float: 'left',
display: 'block',
backgroundColor: '#ffffff',
margin: '24px 0px 12px 24px',
padding: 12,
border: '2px solid #ddd',
'border-radius': 8,
'box-shadow': '4px 2px 16px 0px rgba(155,155,155,0.50)'
}