Skip to content

Commit 8dcafe9

Browse files
committed
feat: add preview for current image (sunting)
1 parent 7bd0c45 commit 8dcafe9

File tree

1 file changed

+31
-11
lines changed

1 file changed

+31
-11
lines changed

components/DropZone.tsx

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ import { useController } from "react-hook-form";
44
import { useDropzone } from "react-dropzone";
55
import imageCompression from "browser-image-compression";
66
import Swal from "sweetalert2";
7-
import axios from "axios";
7+
8+
const validateImg = (url: string) => {
9+
return url.match(/(https?:\/\/.*\.(?:png|jpg|jpeg|gif))/) != null;
10+
};
811

912
const DropZone = ({
1013
control,
@@ -14,8 +17,8 @@ const DropZone = ({
1417
}: {
1518
control: any;
1619
name: string;
17-
rules: any;
18-
defaultValue?: any;
20+
rules?: any;
21+
defaultValue?: string;
1922
}) => {
2023
// yep ini buat nge hook ke react-hook-form
2124
const {
@@ -25,17 +28,12 @@ const DropZone = ({
2528
control,
2629
name,
2730
rules,
28-
defaultValue: defaultValue
29-
? axios
30-
.get(defaultValue)
31-
.then((resp) => resp.data)
32-
.catch((err) => defaultValue)
33-
: undefined,
3431
});
3532

3633
const { getRootProps, getInputProps } = useDropzone({
3734
maxFiles: 1,
38-
accept: { "image/jpeg": [], "image/jpg": [], "image/png": [] },
35+
// "image/jpg": [".jpg"],
36+
accept: { "image/jpeg": [".jpeg", ".jpg"], "image/png": [".png"] },
3937
onDrop: (acceptedFiles: File[]) => {
4038
if (acceptedFiles.length) {
4139
imageCompression(acceptedFiles[0], {
@@ -88,6 +86,28 @@ const DropZone = ({
8886
</Box>
8987
);
9088

89+
const before = defaultValue && validateImg(defaultValue) && (
90+
<Box
91+
display={"inline-flex"}
92+
borderRadius={4}
93+
border={"2px solid #eaeaea"}
94+
mt={"16px"}
95+
mx={1}
96+
w={"auto"}
97+
h={"auto"}
98+
p={1}
99+
boxSizing={"border-box"}
100+
>
101+
<Box display={"flex"} w={"auto"} h={"100%"}>
102+
<Image
103+
src={defaultValue}
104+
style={{ display: "block", width: "auto", height: "100%" }}
105+
alt={"before"}
106+
/>
107+
</Box>
108+
</Box>
109+
);
110+
91111
return (
92112
<>
93113
<Center
@@ -106,7 +126,7 @@ const DropZone = ({
106126
</Text>
107127
</Center>
108128
<Box display={"flex"} flexDirection={"row"} flexWrap={"wrap"}>
109-
{thumb}
129+
{value ? thumb : before}
110130
</Box>
111131
</>
112132
);

0 commit comments

Comments
 (0)