@mainframework/dropzone 1.0.27 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +52 -5
- package/dist/index.d.ts +2 -57
- package/dist/index.js +555 -369
- package/dist/index.js.map +1 -1
- package/dist/shared/components/FileSelector/FileSelector.d.ts +6 -0
- package/dist/shared/components/FileSelector/index.d.ts +1 -0
- package/dist/shared/hooks/useFileSelector.d.ts +20 -0
- package/dist/shared/types/types.d.ts +46 -0
- package/dist/shared/utils/dragAndDrop.d.ts +2 -0
- package/dist/shared/utils/mergeStyles.d.ts +2 -0
- package/dist/shared/utils/processUploadedFiles.d.ts +23 -0
- package/package.json +28 -22
package/README.md
CHANGED
|
@@ -26,10 +26,32 @@ The `useFileSelector` hook accepts optional configuration:
|
|
|
26
26
|
const { ... } = useFileSelector({
|
|
27
27
|
maximumUploadCount: 5, // Default: 30
|
|
28
28
|
maximumFileSize: 5e6, // Default: 5 MB
|
|
29
|
-
acceptedTypes: defaultTypeExtensions, // MIME type map, see below
|
|
29
|
+
acceptedTypes: defaultTypeExtensions, // MIME type -> extension map, see below
|
|
30
30
|
});
|
|
31
31
|
```
|
|
32
32
|
|
|
33
|
+
## Minimal Usage
|
|
34
|
+
|
|
35
|
+
```tsx
|
|
36
|
+
import { useFileSelector } from "@mainframework/dropzone";
|
|
37
|
+
|
|
38
|
+
export function Uploader() {
|
|
39
|
+
const { FileSelector, validFiles, invalidFiles, clearCache } = useFileSelector();
|
|
40
|
+
|
|
41
|
+
return (
|
|
42
|
+
<>
|
|
43
|
+
<FileSelector messageParagraph="Drop files here or click to select" />
|
|
44
|
+
|
|
45
|
+
{invalidFiles.length > 0 && <p role="alert">Some files were rejected.</p>}
|
|
46
|
+
|
|
47
|
+
<button type="button" onClick={clearCache} disabled={validFiles.length === 0}>
|
|
48
|
+
Clear
|
|
49
|
+
</button>
|
|
50
|
+
</>
|
|
51
|
+
);
|
|
52
|
+
}
|
|
53
|
+
```
|
|
54
|
+
|
|
33
55
|
## Hook Exports
|
|
34
56
|
|
|
35
57
|
The hook returns the following:
|
|
@@ -70,12 +92,11 @@ The hook returns the following:
|
|
|
70
92
|
|
|
71
93
|
### FileSelector props (`FileSelectorViewProps`)
|
|
72
94
|
|
|
73
|
-
The `FileSelector` from the hook accepts **view-only** props (styling, copy, accessibility
|
|
95
|
+
The `FileSelector` from the hook accepts **view-only** props (styling, copy, accessibility). Do not pass `onChange` or drag handlers; the hook supplies those. When rendering it, you can pass:
|
|
74
96
|
|
|
75
97
|
| Prop | Type | Default | Description |
|
|
76
98
|
| --------------------------- | -------- | --------------------------------------------------------- | -------------------------------------------- |
|
|
77
99
|
| `inputId` | `string` | auto-generated | ID for the file input (for `aria-controls`). |
|
|
78
|
-
| `acceptTypes` | `string` | `.png, .jpg, .jpeg, .pdf, .svg, ...` | `accept` attribute for the file input. |
|
|
79
100
|
| `messageParagraph` | `string` | "Drag 'n' drop some files here, or click to select files" | Text shown in the dropzone. |
|
|
80
101
|
| `inputClassName` | `string` | `"hiddenInput"` | CSS classes for the hidden input. |
|
|
81
102
|
| `clickableAreaClassName` | `string` | Tailwind dropzone styles | CSS classes for the clickable area. |
|
|
@@ -84,7 +105,6 @@ The `FileSelector` from the hook accepts **view-only** props (styling, copy, acc
|
|
|
84
105
|
| `ariaLabel` | `string` | "File upload drop zone" | Accessible name for the drop zone. |
|
|
85
106
|
| `ariaDescribedBy` | `string` | — | ID of element that describes the drop zone. |
|
|
86
107
|
| `ariaLabelButton` | `string` | "Choose files to upload" | Accessible label for the button. |
|
|
87
|
-
| `ariaLabelledBy` | `string` | — | ID of element that labels the button. |
|
|
88
108
|
|
|
89
109
|
## Default Accepted Types
|
|
90
110
|
|
|
@@ -130,7 +150,7 @@ export const App = () => {
|
|
|
130
150
|
|
|
131
151
|
return (
|
|
132
152
|
<>
|
|
133
|
-
<FileSelector messageParagraph="Drop files here or click to select"
|
|
153
|
+
<FileSelector messageParagraph="Drop files here or click to select" />
|
|
134
154
|
|
|
135
155
|
{maxUploadError.status && <p role="alert">{maxUploadError.message}</p>}
|
|
136
156
|
{maxFileSizeError.status && <p role="alert">{maxFileSizeError.message}</p>}
|
|
@@ -149,6 +169,33 @@ export const App = () => {
|
|
|
149
169
|
};
|
|
150
170
|
```
|
|
151
171
|
|
|
172
|
+
## Cleanup / memory
|
|
173
|
+
|
|
174
|
+
> **Warning: memory management is the caller's responsibility.** The hook does **not** automatically revoke blob URLs on unmount. You **must** call `clearBlobs()` or `clearCache()` yourself — for example in a `useEffect` cleanup, on cancel, or after a successful upload — to prevent memory leaks.
|
|
175
|
+
|
|
176
|
+
```tsx
|
|
177
|
+
useEffect(() => {
|
|
178
|
+
return () => {
|
|
179
|
+
clearBlobs(); // revoke all blob URLs when the component unmounts
|
|
180
|
+
};
|
|
181
|
+
}, [clearBlobs]);
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
- Use `clearCache()` / `onCancel()` to clear files **and** revoke blob URLs.
|
|
185
|
+
- Use `onRemoveFile(index)` to remove one file and revoke its blob URL.
|
|
186
|
+
- Use `clearBlobs()` to revoke blob URLs without clearing the file arrays.
|
|
187
|
+
- Mutating the `validFiles` array directly (for example `validFiles.length = 0`) is not a supported way to clear files/blobs; use `clearCache()` instead so blob URLs are properly revoked.
|
|
188
|
+
|
|
189
|
+
## Exported types
|
|
190
|
+
|
|
191
|
+
If you need types externally, the package exports:
|
|
192
|
+
|
|
193
|
+
- `FileData`
|
|
194
|
+
- `ErrorMessage`
|
|
195
|
+
- `IFileUploaderProps`
|
|
196
|
+
- `FileSelectorViewProps`
|
|
197
|
+
- `FileSelectorHandlerProps`
|
|
198
|
+
|
|
152
199
|
## Manipulating validFiles in a Preview Component
|
|
153
200
|
|
|
154
201
|
As an example, when building a preview component (e.g. `PreviewImages`), use the hook’s methods to update `validFiles` based on user actions:
|
package/dist/index.d.ts
CHANGED
|
@@ -1,57 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
/** Props for styling, copy, and accessibility on the dropzone. Used by the `FileSelector` returned from `useFileSelector`. */
|
|
4
|
-
interface FileSelectorViewProps {
|
|
5
|
-
inputId?: string;
|
|
6
|
-
acceptTypes?: string;
|
|
7
|
-
messageParagraph?: string;
|
|
8
|
-
inputClassName?: string;
|
|
9
|
-
clickableAreaClassName?: string;
|
|
10
|
-
dropZoneWrapperClassName?: string;
|
|
11
|
-
messageParagraphClassName?: string;
|
|
12
|
-
/** Accessible name for the drop zone region (default: "File upload") */
|
|
13
|
-
ariaLabel?: string;
|
|
14
|
-
/** ID of element that describes the drop zone (e.g. help text) */
|
|
15
|
-
ariaDescribedBy?: string;
|
|
16
|
-
/** Accessible label for the file input button when messageParagraph is not sufficient */
|
|
17
|
-
ariaLabelButton?: string;
|
|
18
|
-
ariaLabelledBy?: string;
|
|
19
|
-
}
|
|
20
|
-
interface IFileUploaderProps {
|
|
21
|
-
acceptTypes?: string;
|
|
22
|
-
maximumUploadCount?: number;
|
|
23
|
-
maximumFileSize?: number;
|
|
24
|
-
acceptedTypes?: Record<string, string>;
|
|
25
|
-
}
|
|
26
|
-
interface FileData {
|
|
27
|
-
id: string;
|
|
28
|
-
file: File | Blob;
|
|
29
|
-
url: string;
|
|
30
|
-
type: string;
|
|
31
|
-
}
|
|
32
|
-
interface ErrorMessage {
|
|
33
|
-
status: boolean;
|
|
34
|
-
message: string;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
declare const useFileSelector: ({ maximumUploadCount, maximumFileSize, acceptedTypes, }?: IFileUploaderProps) => {
|
|
38
|
-
validFiles: FileData[];
|
|
39
|
-
invalidFiles: File[];
|
|
40
|
-
clearCache: () => void;
|
|
41
|
-
getValidFileStreams: () => (File | Blob)[];
|
|
42
|
-
onCancel: () => void;
|
|
43
|
-
onIdChange: (index: number, id: string, files: FileData[]) => void;
|
|
44
|
-
onRemoveFile: (index: number) => void;
|
|
45
|
-
clearBlobs: () => void;
|
|
46
|
-
clearBlob: (file: File) => void;
|
|
47
|
-
maxUploadError: ErrorMessage;
|
|
48
|
-
maxFileSizeError: ErrorMessage;
|
|
49
|
-
setMaximumFileSizeExceeded: (status?: boolean) => void;
|
|
50
|
-
setMaximumUploadsExceeded: (status?: boolean, fileCount?: number, maximumUploads?: number) => void;
|
|
51
|
-
FileSelector: {
|
|
52
|
-
(props: FileSelectorViewProps): react_jsx_runtime.JSX.Element;
|
|
53
|
-
displayName: string;
|
|
54
|
-
};
|
|
55
|
-
};
|
|
56
|
-
|
|
57
|
-
export { useFileSelector };
|
|
1
|
+
export { useFileSelector } from "./shared/hooks/useFileSelector";
|
|
2
|
+
export type { FileData, ErrorMessage, IFileUploaderProps, FileSelectorViewProps, FileSelectorHandlerProps, } from "./shared/types/types";
|