forked from mu6m/convert-now
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupload.tsx
103 lines (100 loc) · 2.79 KB
/
upload.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
import { IMAGE_EXTENSIONS } from "@/util/convert";
import { Button } from "./ui/button";
import {
Select,
SelectContent,
SelectGroup,
SelectItem,
SelectLabel,
SelectTrigger,
SelectValue,
} from "./ui/select";
import { Trash2, Paperclip } from "lucide-react";
const config = {
maxFiles: 20,
maxSize: 20 * 1024 * 1024,
};
export function Fileupload({ setFiles }: any) {
const handleFileChange = (event: React.ChangeEvent<HTMLInputElement>) => {
const fileList = event.target.files;
if (fileList) {
const newFiles = Array.from(fileList).map((file) => ({
file,
format: IMAGE_EXTENSIONS[0],
}));
setFiles((prev: any) => [...prev, ...newFiles]);
}
event.target.value = "";
};
return (
<div>
<Label htmlFor="files">Files</Label>
<Input
className="file:hidden"
id="files"
type="file"
accept={IMAGE_EXTENSIONS.join(",")}
max={config.maxFiles}
size={config.maxSize}
onChange={handleFileChange}
multiple
/>
<p className="mt-1 text-sm text-gray-500" id="file_input_help">
Images only (MAX. 20MB)
</p>
</div>
);
}
export function Fileitem({ item, setFiles, index }: any) {
return (
<div className="flex flex-col md:flex-row items-center gap-4 justify-between">
<div>
<p>{item.file.name}</p>
</div>
<div className="flex gap-2 items-start flex-col md:flex-row ">
<Select
value={item.format}
onValueChange={(newFormat: any) => {
setFiles((prevFiles: any) =>
prevFiles.map((item: any, i: number) =>
i === index ? { ...item, format: newFormat } : item
)
);
}}
>
<SelectTrigger className="w-[180px]">
<SelectValue placeholder="Select Format" />
</SelectTrigger>
<SelectContent>
<SelectGroup>
<SelectLabel>Formats</SelectLabel>
{IMAGE_EXTENSIONS.map((item) => {
return (
<SelectItem key={item} value={item}>
{item}
</SelectItem>
);
})}
</SelectGroup>
</SelectContent>
</Select>
<Button
type="button"
className="w-10 h-10"
size={"icon"}
variant={"outline"}
onClick={() => {
setFiles((prevFiles: any) =>
prevFiles.filter((_: any, i: number) => i !== index)
);
}}
>
<span className="sr-only">remove item {item.file.name}</span>
<Trash2 className="w-4 h-4" />
</Button>
</div>
</div>
);
}