2021-05-14 08:15:15 +00:00
|
|
|
import { Typography } from "@material-ui/core";
|
2019-06-19 14:40:52 +00:00
|
|
|
import { fade } from "@material-ui/core/styles/colorManipulator";
|
2022-01-28 12:34:20 +00:00
|
|
|
import { ImageIcon, makeStyles } from "@saleor/macaw-ui";
|
2019-06-19 14:40:52 +00:00
|
|
|
import classNames from "classnames";
|
2019-08-09 10:26:22 +00:00
|
|
|
import React from "react";
|
2019-08-26 21:54:03 +00:00
|
|
|
import { FormattedMessage } from "react-intl";
|
2020-05-14 09:30:32 +00:00
|
|
|
|
2019-06-19 14:40:52 +00:00
|
|
|
import Dropzone from "../Dropzone";
|
|
|
|
|
|
|
|
interface ImageUploadProps {
|
|
|
|
children?: (props: { isDragActive: boolean }) => React.ReactNode;
|
|
|
|
className?: string;
|
|
|
|
disableClick?: boolean;
|
|
|
|
isActiveClassName?: string;
|
|
|
|
iconContainerClassName?: string;
|
|
|
|
iconContainerActiveClassName?: string;
|
2020-09-17 11:31:09 +00:00
|
|
|
hideUploadIcon?: boolean;
|
2019-11-28 15:17:39 +00:00
|
|
|
onImageUpload: (file: FileList) => void;
|
2019-06-19 14:40:52 +00:00
|
|
|
}
|
|
|
|
|
2019-12-03 15:28:40 +00:00
|
|
|
const useStyles = makeStyles(
|
|
|
|
theme => ({
|
|
|
|
backdrop: {
|
|
|
|
background: fade(theme.palette.primary.main, 0.1),
|
2022-06-21 09:36:55 +00:00
|
|
|
color: theme.palette.primary.main,
|
2019-12-03 15:28:40 +00:00
|
|
|
},
|
|
|
|
fileField: {
|
2022-06-21 09:36:55 +00:00
|
|
|
display: "none",
|
2019-12-03 15:28:40 +00:00
|
|
|
},
|
|
|
|
imageContainer: {
|
|
|
|
background: "#ffffff",
|
|
|
|
border: "1px solid #eaeaea",
|
|
|
|
borderRadius: theme.spacing(),
|
|
|
|
height: 148,
|
|
|
|
justifySelf: "start",
|
|
|
|
overflow: "hidden",
|
|
|
|
padding: theme.spacing(2),
|
|
|
|
position: "relative",
|
|
|
|
transition: theme.transitions.duration.standard + "s",
|
2022-06-21 09:36:55 +00:00
|
|
|
width: 148,
|
2019-12-03 15:28:40 +00:00
|
|
|
},
|
|
|
|
photosIcon: {
|
2022-01-28 12:34:20 +00:00
|
|
|
height: 32,
|
2019-12-03 15:28:40 +00:00
|
|
|
margin: "0 auto",
|
2022-06-21 09:36:55 +00:00
|
|
|
width: 32,
|
2019-12-03 15:28:40 +00:00
|
|
|
},
|
|
|
|
photosIconContainer: {
|
|
|
|
padding: theme.spacing(5, 0),
|
2022-06-21 09:36:55 +00:00
|
|
|
textAlign: "center",
|
2019-12-03 15:28:40 +00:00
|
|
|
},
|
|
|
|
uploadText: {
|
|
|
|
color: theme.typography.body1.color,
|
|
|
|
fontSize: 12,
|
|
|
|
fontWeight: 600,
|
2022-06-21 09:36:55 +00:00
|
|
|
textTransform: "uppercase",
|
|
|
|
},
|
2019-12-03 15:28:40 +00:00
|
|
|
}),
|
2022-06-21 09:36:55 +00:00
|
|
|
{ name: "ImageUpload" },
|
2019-12-03 15:28:40 +00:00
|
|
|
);
|
2019-06-19 14:40:52 +00:00
|
|
|
|
2019-10-30 14:34:24 +00:00
|
|
|
export const ImageUpload: React.FC<ImageUploadProps> = props => {
|
|
|
|
const {
|
2019-06-19 14:40:52 +00:00
|
|
|
children,
|
|
|
|
className,
|
|
|
|
disableClick,
|
|
|
|
iconContainerActiveClassName,
|
|
|
|
iconContainerClassName,
|
2019-11-28 15:17:39 +00:00
|
|
|
isActiveClassName,
|
2020-09-17 11:31:09 +00:00
|
|
|
hideUploadIcon,
|
2022-06-21 09:36:55 +00:00
|
|
|
onImageUpload,
|
2019-10-30 14:34:24 +00:00
|
|
|
} = props;
|
|
|
|
|
|
|
|
const classes = useStyles(props);
|
|
|
|
|
|
|
|
return (
|
2019-11-28 15:17:39 +00:00
|
|
|
<Dropzone disableClick={disableClick} onDrop={onImageUpload}>
|
2019-06-19 14:40:52 +00:00
|
|
|
{({ isDragActive, getInputProps, getRootProps }) => (
|
|
|
|
<>
|
|
|
|
<div
|
|
|
|
{...getRootProps()}
|
2019-11-28 15:17:39 +00:00
|
|
|
className={classNames(className, classes.photosIconContainer, {
|
|
|
|
[classes.backdrop]: isDragActive,
|
2022-06-21 09:36:55 +00:00
|
|
|
[isActiveClassName]: isDragActive,
|
2019-06-19 14:40:52 +00:00
|
|
|
})}
|
|
|
|
>
|
2020-09-17 11:31:09 +00:00
|
|
|
{!hideUploadIcon && (
|
|
|
|
<div
|
|
|
|
className={classNames(iconContainerClassName, {
|
2022-06-21 09:36:55 +00:00
|
|
|
[iconContainerActiveClassName]: isDragActive,
|
2020-09-17 11:31:09 +00:00
|
|
|
})}
|
|
|
|
>
|
|
|
|
<input
|
|
|
|
{...getInputProps()}
|
|
|
|
className={classes.fileField}
|
|
|
|
accept="image/*"
|
2019-08-26 21:54:03 +00:00
|
|
|
/>
|
2020-09-17 11:31:09 +00:00
|
|
|
<ImageIcon className={classes.photosIcon} />
|
|
|
|
<Typography className={classes.uploadText}>
|
|
|
|
<FormattedMessage
|
2022-05-05 07:54:28 +00:00
|
|
|
id="NxeDbG"
|
2020-09-17 11:31:09 +00:00
|
|
|
defaultMessage="Drop here to upload"
|
|
|
|
description="image upload"
|
|
|
|
/>
|
|
|
|
</Typography>
|
|
|
|
</div>
|
|
|
|
)}
|
2019-06-19 14:40:52 +00:00
|
|
|
</div>
|
|
|
|
{children && children({ isDragActive })}
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
</Dropzone>
|
2019-10-30 14:34:24 +00:00
|
|
|
);
|
|
|
|
};
|
2019-06-19 14:40:52 +00:00
|
|
|
ImageUpload.displayName = "ImageUpload";
|
|
|
|
export default ImageUpload;
|