react-dropzone 14.3.3 → 14.3.5
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 +13 -0
- package/dist/es/index.js +3 -1
- package/dist/es/utils/index.js +22 -8
- package/dist/index.js +2 -2
- package/package.json +1 -1
- package/src/index.js +2 -0
- package/src/index.spec.js +11 -2
- package/src/utils/index.js +21 -10
- package/src/utils/index.spec.js +46 -8
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -703,6 +703,7 @@ export function useDropzone(props = {}) {
|
|
|
703
703
|
dispatch({
|
|
704
704
|
acceptedFiles,
|
|
705
705
|
fileRejections,
|
|
706
|
+
isDragReject: fileRejections.length > 0,
|
|
706
707
|
type: "setFiles",
|
|
707
708
|
});
|
|
708
709
|
|
|
@@ -1025,6 +1026,7 @@ function reducer(state, action) {
|
|
|
1025
1026
|
...state,
|
|
1026
1027
|
acceptedFiles: action.acceptedFiles,
|
|
1027
1028
|
fileRejections: action.fileRejections,
|
|
1029
|
+
isDragReject: action.isDragReject,
|
|
1028
1030
|
};
|
|
1029
1031
|
case "reset":
|
|
1030
1032
|
return {
|
package/src/index.spec.js
CHANGED
|
@@ -2510,7 +2510,7 @@ describe("useDropzone() hook", () => {
|
|
|
2510
2510
|
expect(onDropSpy).toHaveBeenCalledWith(files, [], expect.anything());
|
|
2511
2511
|
});
|
|
2512
2512
|
|
|
2513
|
-
it("sets {acceptedFiles, fileRejections}", async () => {
|
|
2513
|
+
it("sets {acceptedFiles, fileRejections, isDragReject}", async () => {
|
|
2514
2514
|
const FileList = ({ files = [] }) => (
|
|
2515
2515
|
<ul>
|
|
2516
2516
|
{files.map((file) => (
|
|
@@ -2558,11 +2558,18 @@ describe("useDropzone() hook", () => {
|
|
|
2558
2558
|
"image/*": [],
|
|
2559
2559
|
}}
|
|
2560
2560
|
>
|
|
2561
|
-
{({
|
|
2561
|
+
{({
|
|
2562
|
+
getRootProps,
|
|
2563
|
+
getInputProps,
|
|
2564
|
+
acceptedFiles,
|
|
2565
|
+
fileRejections,
|
|
2566
|
+
isDragReject,
|
|
2567
|
+
}) => (
|
|
2562
2568
|
<div {...getRootProps()}>
|
|
2563
2569
|
<input {...getInputProps()} />
|
|
2564
2570
|
<FileList files={acceptedFiles} />
|
|
2565
2571
|
<RejectedFileList fileRejections={fileRejections} />
|
|
2572
|
+
{isDragReject && "dragReject"}
|
|
2566
2573
|
</div>
|
|
2567
2574
|
)}
|
|
2568
2575
|
</Dropzone>
|
|
@@ -2574,6 +2581,7 @@ describe("useDropzone() hook", () => {
|
|
|
2574
2581
|
const acceptedFileList = getAcceptedFiles(dropzone);
|
|
2575
2582
|
expect(acceptedFileList).toHaveLength(images.length);
|
|
2576
2583
|
expect(matchToFiles(acceptedFileList, images)).toBe(true);
|
|
2584
|
+
expect(dropzone).not.toHaveTextContent("dragReject");
|
|
2577
2585
|
|
|
2578
2586
|
await act(() => fireEvent.drop(dropzone, createDtWithFiles(files)));
|
|
2579
2587
|
|
|
@@ -2585,6 +2593,7 @@ describe("useDropzone() hook", () => {
|
|
|
2585
2593
|
expect(matchToErrorCode(rejectedFileErrorList, "file-invalid-type")).toBe(
|
|
2586
2594
|
true
|
|
2587
2595
|
);
|
|
2596
|
+
expect(dropzone).toHaveTextContent("dragReject");
|
|
2588
2597
|
});
|
|
2589
2598
|
|
|
2590
2599
|
it("resets {isDragActive, isDragAccept, isDragReject}", async () => {
|
package/src/utils/index.js
CHANGED
|
@@ -15,15 +15,18 @@ export const ErrorCode = {
|
|
|
15
15
|
TooManyFiles: TOO_MANY_FILES,
|
|
16
16
|
};
|
|
17
17
|
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
18
|
+
/**
|
|
19
|
+
*
|
|
20
|
+
* @param {string} accept
|
|
21
|
+
*/
|
|
22
|
+
export const getInvalidTypeRejectionErr = (accept = "") => {
|
|
23
|
+
const acceptArr = accept.split(",");
|
|
24
|
+
const msg =
|
|
25
|
+
acceptArr.length > 1 ? `one of ${acceptArr.join(", ")}` : acceptArr[0];
|
|
26
|
+
|
|
24
27
|
return {
|
|
25
28
|
code: FILE_INVALID_TYPE,
|
|
26
|
-
message: `File type must be ${
|
|
29
|
+
message: `File type must be ${msg}`,
|
|
27
30
|
};
|
|
28
31
|
};
|
|
29
32
|
|
|
@@ -50,8 +53,16 @@ export const TOO_MANY_FILES_REJECTION = {
|
|
|
50
53
|
message: "Too many files",
|
|
51
54
|
};
|
|
52
55
|
|
|
53
|
-
|
|
54
|
-
|
|
56
|
+
/**
|
|
57
|
+
* Check if file is accepted.
|
|
58
|
+
*
|
|
59
|
+
* Firefox versions prior to 53 return a bogus MIME type for every file drag,
|
|
60
|
+
* so dragovers with that MIME type will always be accepted.
|
|
61
|
+
*
|
|
62
|
+
* @param {File} file
|
|
63
|
+
* @param {string} accept
|
|
64
|
+
* @returns
|
|
65
|
+
*/
|
|
55
66
|
export function fileAccepted(file, accept) {
|
|
56
67
|
const isAcceptable =
|
|
57
68
|
file.type === "application/x-moz-file" || accepts(file, accept);
|
|
@@ -82,7 +93,7 @@ function isDefined(value) {
|
|
|
82
93
|
*
|
|
83
94
|
* @param {object} options
|
|
84
95
|
* @param {File[]} options.files
|
|
85
|
-
* @param {string
|
|
96
|
+
* @param {string} [options.accept]
|
|
86
97
|
* @param {number} [options.minSize]
|
|
87
98
|
* @param {number} [options.maxSize]
|
|
88
99
|
* @param {boolean} [options.multiple]
|
package/src/utils/index.spec.js
CHANGED
|
@@ -269,7 +269,7 @@ describe("fileAccepted()", () => {
|
|
|
269
269
|
|
|
270
270
|
it("accepts file when multiple accept criteria", () => {
|
|
271
271
|
const file = createFile("hamster.pdf", 100, "application/pdf");
|
|
272
|
-
expect(utils.fileAccepted(file,
|
|
272
|
+
expect(utils.fileAccepted(file, ".pdf,.png")).toEqual([true, null]);
|
|
273
273
|
});
|
|
274
274
|
|
|
275
275
|
it("rejects file when single accept criteria", () => {
|
|
@@ -282,7 +282,7 @@ describe("fileAccepted()", () => {
|
|
|
282
282
|
|
|
283
283
|
it("rejects file when multiple accept criteria", () => {
|
|
284
284
|
const file = createFile("hamster.pdf", 100, "application/pdf");
|
|
285
|
-
expect(utils.fileAccepted(file,
|
|
285
|
+
expect(utils.fileAccepted(file, ".gif,.png")).toEqual([
|
|
286
286
|
false,
|
|
287
287
|
{
|
|
288
288
|
code: "file-invalid-type",
|
|
@@ -290,13 +290,51 @@ describe("fileAccepted()", () => {
|
|
|
290
290
|
},
|
|
291
291
|
]);
|
|
292
292
|
});
|
|
293
|
+
});
|
|
293
294
|
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
295
|
+
describe("getTooLargeRejectionErr()", () => {
|
|
296
|
+
/**
|
|
297
|
+
* @constant
|
|
298
|
+
* @type {import('./index')}
|
|
299
|
+
*/
|
|
300
|
+
let utils;
|
|
301
|
+
beforeEach(async () => {
|
|
302
|
+
utils = await import("./index");
|
|
303
|
+
});
|
|
304
|
+
|
|
305
|
+
it("prints byte when maxSize is 1", () => {
|
|
306
|
+
expect(utils.getTooLargeRejectionErr(1).message).toEqual(
|
|
307
|
+
"File is larger than 1 byte"
|
|
308
|
+
);
|
|
309
|
+
});
|
|
310
|
+
|
|
311
|
+
it("prints bytes when maxSize > 1", () => {
|
|
312
|
+
expect(utils.getTooLargeRejectionErr(100).message).toEqual(
|
|
313
|
+
"File is larger than 100 bytes"
|
|
314
|
+
);
|
|
315
|
+
});
|
|
316
|
+
});
|
|
317
|
+
|
|
318
|
+
describe("getTooSmallRejectionErr()", () => {
|
|
319
|
+
/**
|
|
320
|
+
* @constant
|
|
321
|
+
* @type {import('./index')}
|
|
322
|
+
*/
|
|
323
|
+
let utils;
|
|
324
|
+
beforeEach(async () => {
|
|
325
|
+
utils = await import("./index");
|
|
326
|
+
});
|
|
327
|
+
|
|
328
|
+
it("prints byte when minSize is 1", () => {
|
|
329
|
+
expect(utils.getTooSmallRejectionErr(1).message).toEqual(
|
|
330
|
+
"File is smaller than 1 byte"
|
|
331
|
+
);
|
|
332
|
+
});
|
|
333
|
+
|
|
334
|
+
it("prints bytes when minSize > 1", () => {
|
|
335
|
+
expect(utils.getTooSmallRejectionErr(100).message).toEqual(
|
|
336
|
+
"File is smaller than 100 bytes"
|
|
337
|
+
);
|
|
300
338
|
});
|
|
301
339
|
});
|
|
302
340
|
|