|
| 1 | +import { useState } from "react"; |
| 2 | +import { styled } from "@mui/material/styles"; |
| 3 | +import { Button, ImageList, ImageListItem } from "@mui/material"; |
| 4 | +import FileUploadIcon from "@mui/icons-material/FileUpload"; |
| 5 | +import "react-toastify/dist/ReactToastify.css"; |
| 6 | + |
| 7 | +import QuestionImage from "./QuestionImage"; |
| 8 | +import QuestionImageDialog from "./QuestionImageDialog"; |
| 9 | + |
| 10 | +const FileUploadInput = styled("input")({ |
| 11 | + height: 1, |
| 12 | + overflow: "hidden", |
| 13 | + position: "absolute", |
| 14 | + bottom: 0, |
| 15 | + left: 0, |
| 16 | + width: 1, |
| 17 | +}); |
| 18 | + |
| 19 | +interface QuestionImageContainerProps { |
| 20 | + handleImageUpload: (event: React.ChangeEvent<HTMLInputElement>) => void; |
| 21 | + uploadedImagesUrl: string[]; |
| 22 | +} |
| 23 | + |
| 24 | +const QuestionImageContainer: React.FC<QuestionImageContainerProps> = ({ |
| 25 | + handleImageUpload, |
| 26 | + uploadedImagesUrl, |
| 27 | +}) => { |
| 28 | + const [open, setOpen] = useState<boolean>(false); |
| 29 | + const [selectedValue, setSelectedValue] = useState<string>(""); |
| 30 | + |
| 31 | + const handleClickOpen = (url: string) => { |
| 32 | + setOpen(true); |
| 33 | + setSelectedValue(url); |
| 34 | + }; |
| 35 | + |
| 36 | + const handleClose = () => { |
| 37 | + setOpen(false); |
| 38 | + }; |
| 39 | + |
| 40 | + if (uploadedImagesUrl.length === 0) { |
| 41 | + return ( |
| 42 | + <Button |
| 43 | + component="label" |
| 44 | + variant="contained" |
| 45 | + sx={{ |
| 46 | + borderRadius: 3, |
| 47 | + height: 128, |
| 48 | + width: "100%", |
| 49 | + backgroundColor: "rgba(0, 0, 0, 0.01)", |
| 50 | + color: "#757575", |
| 51 | + border: "1px grey", |
| 52 | + marginTop: 2, |
| 53 | + }} |
| 54 | + > |
| 55 | + <FileUploadIcon /> |
| 56 | + Click to upload images. The maximum image size accepted is 5MB. |
| 57 | + <FileUploadInput |
| 58 | + type="file" |
| 59 | + accept="image/png,image/jpeg" |
| 60 | + onChange={(event) => handleImageUpload(event)} |
| 61 | + multiple |
| 62 | + /> |
| 63 | + </Button> |
| 64 | + ); |
| 65 | + } |
| 66 | + |
| 67 | + return ( |
| 68 | + <> |
| 69 | + <ImageList cols={7} rowHeight={128} sx={{ paddingTop: 2 }}> |
| 70 | + {uploadedImagesUrl.map((image) => ( |
| 71 | + <QuestionImage key={image} url={image} handleClickOpen={handleClickOpen} /> |
| 72 | + ))} |
| 73 | + |
| 74 | + <ImageListItem sx={{ width: 128, height: 128 }}> |
| 75 | + <Button |
| 76 | + component="label" |
| 77 | + variant="contained" |
| 78 | + sx={{ |
| 79 | + borderRadius: 3, |
| 80 | + height: 128, |
| 81 | + width: 128, |
| 82 | + backgroundColor: "rgba(0, 0, 0, 0.01)", |
| 83 | + color: "#757575", |
| 84 | + border: "1px grey", |
| 85 | + textAlign: "center", |
| 86 | + }} |
| 87 | + > |
| 88 | + <FileUploadIcon /> |
| 89 | + Upload images |
| 90 | + <FileUploadInput |
| 91 | + type="file" |
| 92 | + accept="image/png,image/jpeg" |
| 93 | + onChange={(event) => handleImageUpload(event)} |
| 94 | + multiple |
| 95 | + /> |
| 96 | + </Button> |
| 97 | + </ImageListItem> |
| 98 | + </ImageList> |
| 99 | + |
| 100 | + <QuestionImageDialog value={selectedValue} open={open} handleClose={handleClose} /> |
| 101 | + </> |
| 102 | + ); |
| 103 | +}; |
| 104 | + |
| 105 | +export default QuestionImageContainer; |
0 commit comments