Skip to content

Commit 4e57855

Browse files
committed
docs(hooks): added useFileUpload docs and examples
1 parent 6cd8f70 commit 4e57855

File tree

14 files changed

+1199
-9
lines changed

14 files changed

+1199
-9
lines changed

apps/docs/src/app/(main)/(markdown)/(demos)/components/box/page.mdx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,9 @@ To create rows with an equal height, enable the `gridAutoRows` restrict
108108
the height through CSS. This is most useful when handling unknown sized items
109109
(like file uploads).
110110

111+
> !Success! Check out the [useFileUpload](/hooks/use-file-upload) for another
112+
> example using auto rows.
113+
111114
```demo source="./GridAutoRowsExample.tsx"
112115
113116
```
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
@use "everything";
2+
3+
.container {
4+
@include everything.box-set-var(item-min-size, 18rem);
5+
@include everything.box-set-var(auto-rows-height, 18rem * 3);
6+
@include everything.box-set-var(row-max-height, 18rem);
7+
8+
width: 100%;
9+
}
10+
11+
.progress {
12+
padding: everything.$box-padding;
13+
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
"use client";
2+
3+
import { Box } from "@react-md/core/box/Box";
4+
import { Button } from "@react-md/core/button/Button";
5+
import { FileInput } from "@react-md/core/files/FileInput";
6+
import {
7+
type FileUploadOptions,
8+
useFileUpload,
9+
} from "@react-md/core/files/useFileUpload";
10+
import { Form } from "@react-md/core/form/Form";
11+
import { FormMessage } from "@react-md/core/form/FormMessage";
12+
import { FormMessageCounter } from "@react-md/core/form/FormMessageCounter";
13+
import { LinearProgress } from "@react-md/core/progress/LinearProgress";
14+
import { type ReactElement } from "react";
15+
16+
import { FilePreviewCard } from "@/components/FilePreview/FilePreviewCard.jsx";
17+
import { FileUploadErrorModal } from "@/components/FileUploadErrorModal/FileUploadErrorModal.jsx";
18+
19+
import styles from "./FileUploadExample.module.scss";
20+
21+
export const EXTENSIONS = [
22+
"svg",
23+
"jpeg",
24+
"jpg",
25+
"png",
26+
"apng",
27+
"mkv",
28+
"mp4",
29+
"mpeg",
30+
"mpg",
31+
"webm",
32+
"mov",
33+
] as const;
34+
35+
export const FOUR_HUNDRED_MB = 400 * 1024 * 1024;
36+
export const MAX_FILES = 10;
37+
38+
export type FileUploadExampleProps = Partial<FileUploadOptions<HTMLElement>>;
39+
40+
export default function FileUploadExample(
41+
props: FileUploadExampleProps
42+
): ReactElement {
43+
const {
44+
maxFiles = MAX_FILES,
45+
maxFileSize = FOUR_HUNDRED_MB,
46+
extensions = EXTENSIONS,
47+
} = props;
48+
const { stats, errors, onChange, clearErrors, reset, remove, accept } =
49+
useFileUpload({
50+
...props,
51+
maxFiles,
52+
maxFileSize,
53+
extensions,
54+
});
55+
56+
return (
57+
<Form className={styles.container}>
58+
<FileUploadErrorModal errors={errors} clearErrors={clearErrors} />
59+
<Box>
60+
<FileInput
61+
accept={accept}
62+
onChange={onChange}
63+
multiple={maxFiles > 1}
64+
/>
65+
<Button onClick={reset} disabled={!stats.length}>
66+
Remove all files
67+
</Button>
68+
</Box>
69+
<div className={styles.progress}>
70+
<LinearProgress
71+
aria-label="Upload size limit"
72+
min={0}
73+
max={maxFiles}
74+
value={stats.length}
75+
/>
76+
<FormMessage theme="none">
77+
<FormMessageCounter>
78+
{stats.length} of {maxFiles}
79+
</FormMessageCounter>
80+
</FormMessage>
81+
</div>
82+
<Box
83+
grid
84+
gridColumns="fill"
85+
gridAutoRows
86+
align="stretch"
87+
className={styles.grid}
88+
>
89+
{stats.map(({ key, ...uploadStatus }) => (
90+
<FilePreviewCard
91+
key={key}
92+
{...uploadStatus}
93+
fileKey={key}
94+
remove={remove}
95+
/>
96+
))}
97+
</Box>
98+
</Form>
99+
);
100+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
@use "everything";
2+
3+
.card {
4+
max-width: 30rem;
5+
}
6+
7+
.list {
8+
@include everything.interaction-outline();
9+
position: relative;
10+
}
11+
12+
.dragging {
13+
@include everything.interaction-focus-styles($disable-background: true);
14+
}
15+
16+
.dragover {
17+
@include everything.interaction-set-var(
18+
focus-color,
19+
everything.theme-get-var(success-color)
20+
);
21+
}
22+
23+
.upload {
24+
margin: 1rem 0;
25+
width: 100%;
26+
}
27+
28+
.phoneHidden {
29+
@include everything.phone-media {
30+
display: none;
31+
}
32+
}
33+
34+
.progress {
35+
@include everything.progress-set-var(color, currentcolor);
36+
}
37+
38+
.uploadProgress {
39+
margin-top: 1rem;
40+
}

0 commit comments

Comments
 (0)