-
Notifications
You must be signed in to change notification settings - Fork 310
/
Photo.js
57 lines (50 loc) · 1.55 KB
/
Photo.js
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
import React from 'react';
import PropTypes from 'prop-types';
const imgWithClick = { cursor: 'pointer' };
const Photo = ({ index, onClick, photo, margin, direction, top, left, key }) => {
const imgStyle = { margin: margin, display: 'block' };
if (direction === 'column') {
imgStyle.position = 'absolute';
imgStyle.left = left;
imgStyle.top = top;
}
const handleClick = event => {
onClick(event, { photo, index });
};
return (
<img
key={key}
style={onClick ? { ...imgStyle, ...imgWithClick } : imgStyle}
{...photo}
onClick={onClick ? handleClick : null}
/>
);
};
export const photoPropType = PropTypes.shape({
key: PropTypes.string,
src: PropTypes.string.isRequired,
width: PropTypes.number.isRequired,
height: PropTypes.number.isRequired,
alt: PropTypes.string,
title: PropTypes.string,
srcSet: PropTypes.oneOfType([PropTypes.string, PropTypes.array]),
sizes: PropTypes.oneOfType([PropTypes.string, PropTypes.array]),
});
Photo.propTypes = {
index: PropTypes.number.isRequired,
onClick: PropTypes.func,
photo: photoPropType.isRequired,
margin: PropTypes.number,
top: props => {
if (props.direction === 'column' && typeof props.top !== 'number') {
return new Error('top is a required number when direction is set to `column`');
}
},
left: props => {
if (props.direction === 'column' && typeof props.left !== 'number') {
return new Error('left is a required number when direction is set to `column`');
}
},
direction: PropTypes.string,
};
export default Photo;