2020-09-12 21:01:22 +03:00

61 lines
1.7 KiB
TypeScript

/* eslint-disable react/prop-types */
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
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 (
<a href={key}>
<img
key={key}
style={onClick ? { ...imgStyle, ...imgWithClick } : imgStyle}
{...photo}
onClick={onClick ? handleClick : null}
/>
</a>
);
};
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;