@availity/mui-file-selector 1.7.1 → 1.8.1
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/CHANGELOG.md +21 -0
- package/dist/index.d.mts +37 -7
- package/dist/index.d.ts +37 -7
- package/dist/index.js +214 -66
- package/dist/index.mjs +222 -74
- package/package.json +1 -1
- package/src/lib/Dropzone.tsx +101 -4
- package/src/lib/Dropzone2.tsx +91 -5
- package/src/lib/ErrorAlert.tsx +1 -0
- package/src/lib/FileSelector.tsx +21 -1
- package/src/lib/FileSelector2.tsx +8 -1
- package/src/lib/FileTypesMessage.tsx +14 -0
- package/src/lib/HeaderMessage.tsx +8 -3
package/src/lib/Dropzone2.tsx
CHANGED
|
@@ -12,7 +12,7 @@ import type { UploadOptions } from '@availity/upload-core';
|
|
|
12
12
|
import type { OnSuccessPayload } from 'tus-js-client';
|
|
13
13
|
|
|
14
14
|
import { FilePickerBtn } from './FilePickerBtn';
|
|
15
|
-
import { dedupeErrors } from './util';
|
|
15
|
+
import { dedupeErrors, formatBytes } from './util';
|
|
16
16
|
import { createCounter, DropzoneContainer, innerBoxStyles, outerBoxStyles } from './Dropzone';
|
|
17
17
|
import type { DropzoneProps } from './Dropzone';
|
|
18
18
|
|
|
@@ -59,6 +59,7 @@ export const Dropzone2 = ({
|
|
|
59
59
|
enableDropArea = true,
|
|
60
60
|
maxFiles,
|
|
61
61
|
maxSize,
|
|
62
|
+
maxTotalSize,
|
|
62
63
|
multiple,
|
|
63
64
|
name,
|
|
64
65
|
onChange,
|
|
@@ -93,6 +94,35 @@ export const Dropzone2 = ({
|
|
|
93
94
|
message: `Too many files. You may only upload ${maxFiles} file(s).`,
|
|
94
95
|
});
|
|
95
96
|
}
|
|
97
|
+
|
|
98
|
+
// Check for allowed file name characters
|
|
99
|
+
if (uploadOptions.allowedFileNameCharacters) {
|
|
100
|
+
const fileName = file.name.substring(0, file.name.lastIndexOf('.'));
|
|
101
|
+
const regExp = new RegExp(`([^${uploadOptions.allowedFileNameCharacters}])`, 'g');
|
|
102
|
+
|
|
103
|
+
if (fileName.match(regExp) !== null) {
|
|
104
|
+
errors.push({
|
|
105
|
+
code: 'invalid-file-name-characters',
|
|
106
|
+
message: 'File name contains characters not allowed',
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
// Explicit check for allowed file types
|
|
112
|
+
if (allowedFileTypes.length > 0) {
|
|
113
|
+
const fileName = file.name;
|
|
114
|
+
const fileExt = fileName.substring(fileName.lastIndexOf('.')).toLowerCase();
|
|
115
|
+
|
|
116
|
+
// Convert all file types to lowercase for comparison
|
|
117
|
+
const lowerCaseAllowedTypes = allowedFileTypes.map(type => type.toLowerCase());
|
|
118
|
+
|
|
119
|
+
if (!lowerCaseAllowedTypes.includes(fileExt)) {
|
|
120
|
+
errors.push({
|
|
121
|
+
code: 'file-invalid-type',
|
|
122
|
+
message: `File type ${fileExt} is not allowed`,
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
}
|
|
96
126
|
|
|
97
127
|
if (validator) {
|
|
98
128
|
const validatorErrors = validator(file);
|
|
@@ -107,7 +137,7 @@ export const Dropzone2 = ({
|
|
|
107
137
|
|
|
108
138
|
return errors.length > 0 ? dedupeErrors(errors) : null;
|
|
109
139
|
},
|
|
110
|
-
[maxFiles, validator]
|
|
140
|
+
[maxFiles, validator, uploadOptions.allowedFileNameCharacters, allowedFileTypes, watch, name]
|
|
111
141
|
);
|
|
112
142
|
|
|
113
143
|
const handleOnDrop = useCallback(
|
|
@@ -121,10 +151,66 @@ export const Dropzone2 = ({
|
|
|
121
151
|
|
|
122
152
|
const previous = watch(name) ?? [];
|
|
123
153
|
|
|
154
|
+
if (maxTotalSize) {
|
|
155
|
+
// Calculate current total size
|
|
156
|
+
const currentTotalSize = previous.reduce((sum: number, upload: Upload) => sum + upload.file.size, 0);
|
|
157
|
+
let newSize = 0;
|
|
158
|
+
|
|
159
|
+
const availableSize = Math.max(0, maxTotalSize - currentTotalSize);
|
|
160
|
+
let sizeCounter = 0;
|
|
161
|
+
|
|
162
|
+
// Find the index where we exceed the total size limit
|
|
163
|
+
const cutoffIndex = acceptedFiles.findIndex((file) => {
|
|
164
|
+
sizeCounter += file.size;
|
|
165
|
+
return sizeCounter > availableSize;
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
// If we found files that exceed the limit
|
|
169
|
+
if (cutoffIndex !== -1) {
|
|
170
|
+
// Files that fit within the size limit
|
|
171
|
+
const filesToAdd = acceptedFiles.slice(0, cutoffIndex === 0 ? 0 : cutoffIndex);
|
|
172
|
+
|
|
173
|
+
// Create rejection for excess files
|
|
174
|
+
fileRejections.push({
|
|
175
|
+
file: acceptedFiles[cutoffIndex],
|
|
176
|
+
errors: [
|
|
177
|
+
{
|
|
178
|
+
code: 'upload-too-large',
|
|
179
|
+
message: `Total upload size exceeds the limit of ${formatBytes(maxTotalSize)}.`,
|
|
180
|
+
},
|
|
181
|
+
],
|
|
182
|
+
id: counter.increment(),
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
// Update acceptedFiles to only include files that fit
|
|
186
|
+
acceptedFiles = filesToAdd;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
// Calculate size of accepted files for the state update
|
|
190
|
+
newSize = acceptedFiles.reduce((sum, file) => sum + file.size, 0);
|
|
191
|
+
setTotalSize((prev) => prev + newSize);
|
|
192
|
+
}
|
|
193
|
+
|
|
124
194
|
// Set accepted files to form context
|
|
125
|
-
const
|
|
195
|
+
const remainingSlots = maxFiles ? Math.max(0, maxFiles - previous.length) : acceptedFiles.length;
|
|
196
|
+
const filesToAdd = acceptedFiles.slice(0, remainingSlots);
|
|
197
|
+
const uploads = filesToAdd.map((file) => startUpload(file, uploadOptions));
|
|
126
198
|
setValue(name, previous.concat(await Promise.all(uploads)));
|
|
127
199
|
|
|
200
|
+
// Add rejections for excess files if needed
|
|
201
|
+
if (maxFiles && acceptedFiles.length > remainingSlots) {
|
|
202
|
+
fileRejections.push({
|
|
203
|
+
file: acceptedFiles[remainingSlots], // Use the first excess file
|
|
204
|
+
errors: [
|
|
205
|
+
{
|
|
206
|
+
code: 'too-many-files',
|
|
207
|
+
message: `Too many files. You may only upload ${maxFiles} file(s).`,
|
|
208
|
+
},
|
|
209
|
+
],
|
|
210
|
+
id: counter.increment(),
|
|
211
|
+
});
|
|
212
|
+
}
|
|
213
|
+
|
|
128
214
|
if (fileRejections.length > 0) {
|
|
129
215
|
const TOO_MANY_FILES_CODE = 'too-many-files';
|
|
130
216
|
let hasTooManyFiles = false;
|
|
@@ -159,7 +245,7 @@ export const Dropzone2 = ({
|
|
|
159
245
|
if (setFileRejections) setFileRejections(fileRejections);
|
|
160
246
|
if (onDrop) onDrop(acceptedFiles, fileRejections, event);
|
|
161
247
|
},
|
|
162
|
-
[setFileRejections]
|
|
248
|
+
[setFileRejections, setTotalSize, watch, name, maxTotalSize, maxFiles, uploadOptions, setValue, onDrop]
|
|
163
249
|
);
|
|
164
250
|
|
|
165
251
|
const { getRootProps, getInputProps } = useDropzone({
|
|
@@ -190,7 +276,7 @@ export const Dropzone2 = ({
|
|
|
190
276
|
|
|
191
277
|
const handleOnClick = (event: MouseEvent<HTMLButtonElement>) => {
|
|
192
278
|
if (!enableDropArea && rootProps.onClick) rootProps.onClick(event);
|
|
193
|
-
if (onClick) onClick;
|
|
279
|
+
if (onClick) onClick(event);
|
|
194
280
|
};
|
|
195
281
|
|
|
196
282
|
const getFieldValue = () => {
|
package/src/lib/ErrorAlert.tsx
CHANGED
|
@@ -5,6 +5,7 @@ import { formatBytes } from './util';
|
|
|
5
5
|
|
|
6
6
|
const codes: Record<string, string> = {
|
|
7
7
|
'file-too-large': 'File exceeds maximum size',
|
|
8
|
+
'upload-too-large': 'File causes maximum total upload size to be exceeded',
|
|
8
9
|
'file-invalid-type': 'File has an invalid type',
|
|
9
10
|
'file-too-small': 'File is smaller than minimum size',
|
|
10
11
|
'too-many-file': 'Too many files',
|
package/src/lib/FileSelector.tsx
CHANGED
|
@@ -59,6 +59,11 @@ export type FileSelectorProps = {
|
|
|
59
59
|
* Overrides the standard file size message
|
|
60
60
|
*/
|
|
61
61
|
customSizeMessage?: React.ReactNode;
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Overrides the standard total upload size message
|
|
65
|
+
*/
|
|
66
|
+
customTotalSizeMessage?: React.ReactNode;
|
|
62
67
|
/**
|
|
63
68
|
* Overrides the standard file types message
|
|
64
69
|
*/
|
|
@@ -98,10 +103,16 @@ export type FileSelectorProps = {
|
|
|
98
103
|
* Use Kibi or Mibibytes. eg: 1kb = 1024 bytes; 1mb = 1024kb
|
|
99
104
|
*/
|
|
100
105
|
maxSize: number;
|
|
106
|
+
/**
|
|
107
|
+
* Maximum size allowed for total upload in bytes
|
|
108
|
+
* Use Kibi or Mibibytes. eg: 1kb = 1024 bytes; 1mb = 1024kb
|
|
109
|
+
*/
|
|
110
|
+
maxTotalSize?: number;
|
|
101
111
|
/**
|
|
102
112
|
* Whether multiple file selection is allowed
|
|
103
113
|
* @default true
|
|
104
114
|
*/
|
|
115
|
+
|
|
105
116
|
multiple?: boolean;
|
|
106
117
|
/**
|
|
107
118
|
* Callback fired when files are selected
|
|
@@ -145,6 +156,7 @@ export const FileSelector = ({
|
|
|
145
156
|
clientId,
|
|
146
157
|
children,
|
|
147
158
|
customSizeMessage,
|
|
159
|
+
customTotalSizeMessage,
|
|
148
160
|
customTypesMessage,
|
|
149
161
|
customerId,
|
|
150
162
|
customFileRow,
|
|
@@ -155,6 +167,7 @@ export const FileSelector = ({
|
|
|
155
167
|
label = 'Upload file',
|
|
156
168
|
maxFiles,
|
|
157
169
|
maxSize,
|
|
170
|
+
maxTotalSize,
|
|
158
171
|
multiple = true,
|
|
159
172
|
onChange,
|
|
160
173
|
onDrop,
|
|
@@ -236,10 +249,12 @@ export const FileSelector = ({
|
|
|
236
249
|
<Dropzone
|
|
237
250
|
name={name}
|
|
238
251
|
allowedFileTypes={allowedFileTypes}
|
|
252
|
+
allowedFileNameCharacters={allowedFileNameCharacters}
|
|
239
253
|
disabled={disabled}
|
|
240
254
|
enableDropArea={enableDropArea}
|
|
241
255
|
maxFiles={maxFiles}
|
|
242
256
|
maxSize={maxSize}
|
|
257
|
+
maxTotalSize={maxTotalSize}
|
|
243
258
|
multiple={multiple}
|
|
244
259
|
onChange={onChange}
|
|
245
260
|
onDrop={onDrop}
|
|
@@ -250,7 +265,9 @@ export const FileSelector = ({
|
|
|
250
265
|
<FileTypesMessage
|
|
251
266
|
allowedFileTypes={allowedFileTypes}
|
|
252
267
|
maxFileSize={maxSize}
|
|
268
|
+
maxTotalSize={maxTotalSize}
|
|
253
269
|
customSizeMessage={customSizeMessage}
|
|
270
|
+
customTotalSizeMessage={customTotalSizeMessage}
|
|
254
271
|
customTypesMessage={customTypesMessage}
|
|
255
272
|
variant="caption"
|
|
256
273
|
/>
|
|
@@ -259,9 +276,10 @@ export const FileSelector = ({
|
|
|
259
276
|
) : (
|
|
260
277
|
<Grid container rowSpacing={3} flexDirection="column">
|
|
261
278
|
<Grid>
|
|
262
|
-
<HeaderMessage maxFiles={maxFiles} maxSize={maxSize} />
|
|
279
|
+
<HeaderMessage maxFiles={maxFiles} maxSize={maxSize} maxTotalSize={maxTotalSize} />
|
|
263
280
|
<FileTypesMessage
|
|
264
281
|
allowedFileTypes={allowedFileTypes}
|
|
282
|
+
customTotalSizeMessage={customTotalSizeMessage}
|
|
265
283
|
customSizeMessage={customSizeMessage}
|
|
266
284
|
customTypesMessage={customTypesMessage}
|
|
267
285
|
variant="body2"
|
|
@@ -272,10 +290,12 @@ export const FileSelector = ({
|
|
|
272
290
|
<Dropzone
|
|
273
291
|
name={name}
|
|
274
292
|
allowedFileTypes={allowedFileTypes}
|
|
293
|
+
allowedFileNameCharacters={allowedFileNameCharacters}
|
|
275
294
|
disabled={disabled}
|
|
276
295
|
enableDropArea={enableDropArea}
|
|
277
296
|
maxFiles={maxFiles}
|
|
278
297
|
maxSize={maxSize}
|
|
298
|
+
maxTotalSize={maxTotalSize}
|
|
279
299
|
multiple={multiple}
|
|
280
300
|
onChange={onChange}
|
|
281
301
|
onDrop={onDrop}
|
|
@@ -37,6 +37,7 @@ export const FileSelector2 = ({
|
|
|
37
37
|
clientId,
|
|
38
38
|
children,
|
|
39
39
|
customSizeMessage,
|
|
40
|
+
customTotalSizeMessage,
|
|
40
41
|
customTypesMessage,
|
|
41
42
|
customerId,
|
|
42
43
|
customFileRow,
|
|
@@ -47,6 +48,7 @@ export const FileSelector2 = ({
|
|
|
47
48
|
label = 'Upload file',
|
|
48
49
|
maxFiles,
|
|
49
50
|
maxSize,
|
|
51
|
+
maxTotalSize,
|
|
50
52
|
multiple = true,
|
|
51
53
|
onChange,
|
|
52
54
|
onDrop,
|
|
@@ -129,6 +131,7 @@ export const FileSelector2 = ({
|
|
|
129
131
|
enableDropArea={enableDropArea}
|
|
130
132
|
maxFiles={maxFiles}
|
|
131
133
|
maxSize={maxSize}
|
|
134
|
+
maxTotalSize={maxTotalSize}
|
|
132
135
|
multiple={multiple}
|
|
133
136
|
onChange={onChange}
|
|
134
137
|
onDrop={onDrop}
|
|
@@ -140,7 +143,9 @@ export const FileSelector2 = ({
|
|
|
140
143
|
<FileTypesMessage
|
|
141
144
|
allowedFileTypes={allowedFileTypes}
|
|
142
145
|
maxFileSize={maxSize}
|
|
146
|
+
maxTotalSize={maxTotalSize}
|
|
143
147
|
customSizeMessage={customSizeMessage}
|
|
148
|
+
customTotalSizeMessage={customTotalSizeMessage}
|
|
144
149
|
customTypesMessage={customTypesMessage}
|
|
145
150
|
variant="caption"
|
|
146
151
|
/>
|
|
@@ -149,10 +154,11 @@ export const FileSelector2 = ({
|
|
|
149
154
|
) : (
|
|
150
155
|
<Grid container rowSpacing={3} flexDirection="column">
|
|
151
156
|
<Grid>
|
|
152
|
-
<HeaderMessage maxFiles={maxFiles} maxSize={maxSize} />
|
|
157
|
+
<HeaderMessage maxFiles={maxFiles} maxSize={maxSize} maxTotalSize={maxTotalSize} />
|
|
153
158
|
<FileTypesMessage
|
|
154
159
|
allowedFileTypes={allowedFileTypes}
|
|
155
160
|
customSizeMessage={customSizeMessage}
|
|
161
|
+
customTotalSizeMessage={customTotalSizeMessage}
|
|
156
162
|
customTypesMessage={customTypesMessage}
|
|
157
163
|
variant="body2"
|
|
158
164
|
/>
|
|
@@ -166,6 +172,7 @@ export const FileSelector2 = ({
|
|
|
166
172
|
enableDropArea={enableDropArea}
|
|
167
173
|
maxFiles={maxFiles}
|
|
168
174
|
maxSize={maxSize}
|
|
175
|
+
maxTotalSize={maxTotalSize}
|
|
169
176
|
multiple={multiple}
|
|
170
177
|
onChange={onChange}
|
|
171
178
|
onDrop={onDrop}
|
|
@@ -11,6 +11,10 @@ export type FileTypesMessageProps = {
|
|
|
11
11
|
* Overrides the standard file size message
|
|
12
12
|
*/
|
|
13
13
|
customSizeMessage?: React.ReactNode;
|
|
14
|
+
/**
|
|
15
|
+
* Overrides the standard total upload size message
|
|
16
|
+
*/
|
|
17
|
+
customTotalSizeMessage?: React.ReactNode;
|
|
14
18
|
/**
|
|
15
19
|
* Overrides the standard file types message
|
|
16
20
|
*/
|
|
@@ -19,19 +23,28 @@ export type FileTypesMessageProps = {
|
|
|
19
23
|
* Maximum size per file in bytes. This will be formatted. eg: 1024 * 20 = 20 KB
|
|
20
24
|
*/
|
|
21
25
|
maxFileSize?: number;
|
|
26
|
+
/**
|
|
27
|
+
* Maximum size of the total upload in bytes. This will be formatted. eg: 1024 * 20 = 20 KB
|
|
28
|
+
*/
|
|
29
|
+
maxTotalSize?: number;
|
|
22
30
|
variant?: 'caption' | 'body2';
|
|
23
31
|
};
|
|
24
32
|
|
|
25
33
|
export const FileTypesMessage = ({
|
|
26
34
|
allowedFileTypes = [],
|
|
27
35
|
customSizeMessage,
|
|
36
|
+
customTotalSizeMessage,
|
|
28
37
|
customTypesMessage,
|
|
29
38
|
maxFileSize,
|
|
39
|
+
maxTotalSize,
|
|
30
40
|
variant = 'caption',
|
|
31
41
|
}: FileTypesMessageProps) => {
|
|
32
42
|
const fileSizeMsg =
|
|
33
43
|
customSizeMessage ||
|
|
34
44
|
(typeof maxFileSize === 'number' ? `Maximum file size is ${formatBytes(maxFileSize)}. ` : null);
|
|
45
|
+
const totalFileSizeMsg =
|
|
46
|
+
customTotalSizeMessage ||
|
|
47
|
+
(typeof maxTotalSize === 'number' ? `Maximum total upload size is ${formatBytes(maxTotalSize)}. ` : null);
|
|
35
48
|
const fileTypesMsg =
|
|
36
49
|
customTypesMessage ||
|
|
37
50
|
(allowedFileTypes.length > 0
|
|
@@ -40,6 +53,7 @@ export const FileTypesMessage = ({
|
|
|
40
53
|
return (
|
|
41
54
|
<Typography variant={variant}>
|
|
42
55
|
{fileSizeMsg}
|
|
56
|
+
{totalFileSizeMsg}
|
|
43
57
|
{fileTypesMsg}
|
|
44
58
|
</Typography>
|
|
45
59
|
);
|
|
@@ -8,15 +8,20 @@ export type HeaderMessageProps = {
|
|
|
8
8
|
*/
|
|
9
9
|
maxFiles: number;
|
|
10
10
|
/**
|
|
11
|
-
* Maximum
|
|
11
|
+
* Maximum size of each file
|
|
12
12
|
*/
|
|
13
13
|
maxSize: number;
|
|
14
|
+
/**
|
|
15
|
+
* Maximum combined total size of all files
|
|
16
|
+
*/
|
|
17
|
+
maxTotalSize?: number;
|
|
14
18
|
};
|
|
15
19
|
|
|
16
|
-
export const HeaderMessage = ({ maxFiles, maxSize }: HeaderMessageProps) => {
|
|
20
|
+
export const HeaderMessage = ({ maxFiles, maxSize, maxTotalSize }: HeaderMessageProps) => {
|
|
17
21
|
return (
|
|
18
22
|
<Typography variant="h6">
|
|
19
|
-
Attach up to {maxFiles} file(s), with a maximum individual size of {formatBytes(maxSize)}
|
|
23
|
+
Attach up to {maxFiles} file(s), with a maximum individual size of {formatBytes(maxSize)}{' '}
|
|
24
|
+
{maxTotalSize && `and a maximum total size of ${formatBytes(maxTotalSize)}`}
|
|
20
25
|
</Typography>
|
|
21
26
|
);
|
|
22
27
|
};
|