Form Simple File Upload
src / Demo.tsx
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657import React, { ReactElement } from "react";
import { Button } from "@react-md/button";
import { useFileUpload, FileInput, Form } from "@react-md/form";
import { GridList } from "@react-md/utils";
import PreviewFile from "./PreviewFile";
import ErrorModal from "./ErrorModal";
const extensions = [
"svg",
"jpeg",
"jpg",
"png",
"apng",
"mkv",
"mp4",
"mpeg",
"mpg",
"webm",
"mov",
];
const FOUR_HUNDRED_MB = 400 * 1024 * 1024;
const maxFiles = 4;
export default function Demo(): ReactElement | null {
const { stats, errors, onChange, clearErrors, reset, remove, accept } =
useFileUpload({
maxFiles,
maxFileSize: FOUR_HUNDRED_MB,
extensions,
});
return (
<Form>
<FileInput
id="simple-file-upload-input"
accept={accept}
onChange={onChange}
multiple={maxFiles > 1}
/>
{stats.length > 0 && <Button onClick={reset}>Remove all files</Button>}
<ErrorModal errors={errors} clearErrors={clearErrors} />
<GridList maxCellSize={500}>
{stats.map(({ key, ...uploadStat }, i) => (
<PreviewFile
id={`simple-file-upload-preivew-${i + 1}`}
{...uploadStat}
key={key}
fileKey={key}
remove={remove}
/>
))}
</GridList>
</Form>
);
}