Skip to content

Commit 47e9a71

Browse files
committed
Update ImageUpload to MUI v6
1 parent 41dbe41 commit 47e9a71

1 file changed

Lines changed: 84 additions & 51 deletions

File tree

  • frontend/src/components/inputs/ImageUpload

frontend/src/components/inputs/ImageUpload/index.js

Lines changed: 84 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,54 @@
11
import React, { useState, useCallback } from 'react'
22
import Upload from 'antd/es/upload'
33
import { useDispatch, useSelector } from 'react-redux'
4+
45
import { Box, Typography, CircularProgress } from '@mui/material'
56
import DeleteIcon from '@mui/icons-material/Delete'
67
import VisibilityIcon from '@mui/icons-material/Visibility'
8+
import { styled } from '@mui/material/styles'
9+
710
import * as AuthSelectors from 'reducers/auth/selectors'
811
import * as SnackbarActions from 'reducers/snackbar/actions'
9-
import clsx from 'clsx'
12+
13+
const EmptyWrapper = styled('div')({
14+
position: 'absolute',
15+
top: 0,
16+
left: 0,
17+
width: '100%',
18+
height: '100%',
19+
display: 'flex',
20+
justifyContent: 'center',
21+
alignItems: 'center',
22+
})
23+
24+
const ButtonOverlay = styled('div')({
25+
position: 'absolute',
26+
top: 0,
27+
left: 0,
28+
width: '100%',
29+
height: '100%',
30+
background: 'rgba(0,0,0,0.6)',
31+
opacity: 0,
32+
display: 'flex',
33+
flexDirection: 'column',
34+
justifyContent: 'center',
35+
alignItems: 'center',
36+
transition: 'opacity 0.2s ease',
37+
'&:hover': {
38+
opacity: 1,
39+
},
40+
color: 'white',
41+
})
42+
43+
const ImageButton = styled('div')({
44+
padding: '8px',
45+
display: 'flex',
46+
alignItems: 'center',
47+
gap: '16px',
48+
'&:hover': {
49+
background: 'rgba(255,255,255,0.2)',
50+
},
51+
})
1052

1153
const ImageUpload = ({
1254
value,
@@ -75,65 +117,56 @@ const ImageUpload = ({
75117
)
76118

77119
const renderLoading = () => (
78-
<Box className="absolute top-0 left-0 w-full h-full flex flex-col justify-center items-center cursor-pointer bg-gray-200">
79-
<CircularProgress size={24} className="text-black" />
80-
</Box>
120+
<EmptyWrapper>
121+
<CircularProgress size={24} />
122+
</EmptyWrapper>
123+
)
124+
125+
const renderEmpty = () => (
126+
<EmptyWrapper>
127+
<Typography>Click or drag a file to upload</Typography>
128+
</EmptyWrapper>
81129
)
82130

83131
const renderImage = () => (
84-
<Box className="relative w-full h-full">
85-
<img
86-
className={clsx(
87-
'absolute top-0 left-0 w-full h-full object-contain',
88-
{
89-
'object-contain': resizeMode === 'contain',
90-
'object-cover': resizeMode === 'cover',
91-
},
92-
)}
132+
<>
133+
<Box
134+
component="img"
135+
sx={{
136+
position: 'absolute',
137+
top: 0,
138+
left: 0,
139+
width: '100%',
140+
height: '100%',
141+
objectFit: resizeMode,
142+
}}
93143
src={value.url}
94144
alt="upload"
95145
/>
96-
<Box className="absolute top-0 left-0 w-full h-full bg-black bg-opacity-60 flex flex-col justify-center items-center opacity-0 transition-opacity duration-200 hover:opacity-100 cursor-pointer">
97-
<Box
98-
className="flex flex-row items-center p-2 hover:bg-white hover:bg-opacity-20"
99-
onClick={handleRemove}
100-
>
101-
<DeleteIcon className="text-white" />
102-
<Box p={1} />
103-
<Typography
104-
variant="button"
105-
className="text-white select-none"
106-
>
107-
Remove image
108-
</Typography>
109-
</Box>
110-
<Box
111-
className="flex flex-row items-center p-2 hover:bg-white hover:bg-opacity-20"
112-
onClick={() => window.open(value.url, '_blank')}
113-
>
114-
<VisibilityIcon className="text-white" />
115-
<Box p={1} />
116-
<Typography
117-
variant="button"
118-
className="text-white select-none"
119-
>
120-
View original
121-
</Typography>
122-
</Box>
123-
</Box>
124-
</Box>
125-
)
126-
127-
const renderEmpty = () => (
128-
<Box className="absolute top-0 left-0 w-full h-full flex flex-col justify-center items-center cursor-pointer bg-gray-200">
129-
<Typography className="text-center text-black select-none">
130-
Click or drag a file to upload
131-
</Typography>
132-
</Box>
146+
<ButtonOverlay>
147+
<ImageButton onClick={handleRemove}>
148+
<DeleteIcon />
149+
<Typography variant="button">Remove image</Typography>
150+
</ImageButton>
151+
<ImageButton onClick={() => window.open(value.url, '_blank')}>
152+
<VisibilityIcon />
153+
<Typography variant="button">View original</Typography>
154+
</ImageButton>
155+
</ButtonOverlay>
156+
</>
133157
)
134158

135159
return (
136-
<Box className="relative w-full h-full bg-gray-100">
160+
<Box
161+
sx={{
162+
backgroundColor: '#f7fafc',
163+
width: '100%',
164+
height: '100%',
165+
position: 'relative',
166+
cursor: 'pointer',
167+
userSelect: 'none',
168+
}}
169+
>
137170
<Upload
138171
name="image"
139172
listType="picture"

0 commit comments

Comments
 (0)