|
| 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 | + disableElevation={true} |
| 46 | + sx={(theme) => ({ |
| 47 | + borderRadius: 1, |
| 48 | + height: 128, |
| 49 | + width: "100%", |
| 50 | + backgroundColor: "#fff", |
| 51 | + color: "#757575", |
| 52 | + border: "1px solid", |
| 53 | + borderColor: theme.palette.grey[400], |
| 54 | + marginTop: 2, |
| 55 | + })} |
| 56 | + > |
| 57 | + <FileUploadIcon /> |
| 58 | + Click to upload images. The maximum image size accepted is 5MB. |
| 59 | + <FileUploadInput |
| 60 | + type="file" |
| 61 | + accept="image/png,image/jpeg" |
| 62 | + onChange={(event) => handleImageUpload(event)} |
| 63 | + multiple |
| 64 | + /> |
| 65 | + </Button> |
| 66 | + ); |
| 67 | + } |
| 68 | + |
| 69 | + return ( |
| 70 | + <> |
| 71 | + <ImageList cols={7} rowHeight={128} sx={{ paddingTop: 2 }}> |
| 72 | + {uploadedImagesUrl.map((image) => ( |
| 73 | + <QuestionImage key={image} url={image} handleClickOpen={handleClickOpen} /> |
| 74 | + ))} |
| 75 | + |
| 76 | + <ImageListItem sx={{ width: 128, height: 128 }}> |
| 77 | + <Button |
| 78 | + component="label" |
| 79 | + variant="contained" |
| 80 | + disableElevation={true} |
| 81 | + sx={(theme) => ({ |
| 82 | + borderRadius: 1, |
| 83 | + height: 128, |
| 84 | + width: 128, |
| 85 | + backgroundColor: "#fff", |
| 86 | + color: "#757575", |
| 87 | + border: "1px solid", |
| 88 | + borderColor: theme.palette.grey[400], |
| 89 | + textAlign: "center", |
| 90 | + })} |
| 91 | + > |
| 92 | + <FileUploadIcon /> |
| 93 | + Upload images |
| 94 | + <FileUploadInput |
| 95 | + type="file" |
| 96 | + accept="image/png,image/jpeg" |
| 97 | + onChange={(event) => handleImageUpload(event)} |
| 98 | + multiple |
| 99 | + /> |
| 100 | + </Button> |
| 101 | + </ImageListItem> |
| 102 | + </ImageList> |
| 103 | + |
| 104 | + <QuestionImageDialog value={selectedValue} open={open} handleClose={handleClose} /> |
| 105 | + </> |
| 106 | + ); |
| 107 | +}; |
| 108 | + |
| 109 | +export default QuestionImageContainer; |
0 commit comments