@aws-amplify/ui-react-core 3.0.28 → 3.0.30

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.
@@ -1,6 +1,7 @@
1
1
  import { useState } from 'react';
2
2
  import { isFunction } from '@aws-amplify/ui';
3
3
  import { filterAllowedFiles } from '../utils/filterAllowedFiles.mjs';
4
+ import { processDroppedItems } from '../utils/processDroppedItems.mjs';
4
5
 
5
6
  function useDropZone({ onDropComplete, onDragEnter: _onDragEnter, onDragLeave: _onDragLeave, onDragOver: _onDragOver, onDragStart: _onDragStart, onDrop: _onDrop, acceptedFileTypes = [], }) {
6
7
  const [dragState, setDragState] = useState('inactive');
@@ -43,13 +44,21 @@ function useDropZone({ onDropComplete, onDragEnter: _onDragEnter, onDragLeave: _
43
44
  event.preventDefault();
44
45
  event.stopPropagation();
45
46
  setDragState('inactive');
46
- const files = Array.from(event.dataTransfer.files);
47
- const { acceptedFiles, rejectedFiles } = filterAllowedFiles(files, acceptedFileTypes);
47
+ const { files, items } = event.dataTransfer;
48
48
  if (isFunction(_onDrop)) {
49
49
  _onDrop(event);
50
50
  }
51
- if (isFunction(onDropComplete)) {
52
- onDropComplete({ acceptedFiles, rejectedFiles });
51
+ const completeDrop = (files) => {
52
+ const { acceptedFiles, rejectedFiles } = filterAllowedFiles(files, acceptedFileTypes);
53
+ if (isFunction(onDropComplete)) {
54
+ onDropComplete({ acceptedFiles, rejectedFiles });
55
+ }
56
+ };
57
+ if (!items) {
58
+ completeDrop(Array.from(files));
59
+ }
60
+ else {
61
+ processDroppedItems(Array.from(items)).then(completeDrop);
53
62
  }
54
63
  };
55
64
  return {
@@ -0,0 +1,66 @@
1
+ // Helper function to convert FileSystemFileEntry to File
2
+ const getFileFromEntry = (fileEntry) => {
3
+ return new Promise((resolve) => {
4
+ fileEntry.file(resolve);
5
+ });
6
+ };
7
+ // Helper function to read all entries in a directory
8
+ const readAllDirectoryEntries = async (dirReader) => {
9
+ const entries = [];
10
+ let readBatch = [];
11
+ do {
12
+ readBatch = await new Promise((resolve, reject) => {
13
+ try {
14
+ dirReader.readEntries(resolve, reject);
15
+ }
16
+ catch (error) {
17
+ reject(error);
18
+ }
19
+ });
20
+ entries.push(...readBatch);
21
+ } while (readBatch.length > 0);
22
+ return entries;
23
+ };
24
+ // Helper function to process files and folder contents
25
+ async function processDroppedItems(dataTransferItems) {
26
+ const files = [];
27
+ const processFileSystemEntry = async (entry) => {
28
+ if (entry.isFile) {
29
+ const file = await getFileFromEntry(entry);
30
+ // drag and dropped files do not have a webkitRelativePath property,
31
+ // but they do have a fullPath property which has the same information
32
+ // https://github.com/ant-design/ant-design/issues/16426
33
+ if (entry.fullPath && !file.webkitRelativePath) {
34
+ Object.defineProperties(file, {
35
+ webkitRelativePath: {
36
+ writable: true,
37
+ },
38
+ });
39
+ // intentionally overwriting webkitRelativePath
40
+ // @ts-expect-error
41
+ file.webkitRelativePath = entry.fullPath.replace(/^\//, '');
42
+ Object.defineProperties(file, {
43
+ webkitRelativePath: {
44
+ writable: false,
45
+ },
46
+ });
47
+ }
48
+ files.push(file);
49
+ }
50
+ else if (entry.isDirectory) {
51
+ const dirReader = entry.createReader();
52
+ const dirEntries = await readAllDirectoryEntries(dirReader);
53
+ await Promise.all(dirEntries.map(processFileSystemEntry));
54
+ }
55
+ };
56
+ // Filter out and process files from the data transfer items
57
+ await Promise.all(dataTransferItems
58
+ .reduce((acc, item) => {
59
+ const entry = item.webkitGetAsEntry();
60
+ return item.kind === 'file' && entry ? [...acc, entry] : acc;
61
+ }, [])
62
+ .map(processFileSystemEntry));
63
+ return files;
64
+ }
65
+
66
+ export { processDroppedItems };
package/dist/index.js CHANGED
@@ -726,6 +726,71 @@ function filterAllowedFiles(files, acceptedFileTypes) {
726
726
  return { acceptedFiles, rejectedFiles };
727
727
  }
728
728
 
729
+ // Helper function to convert FileSystemFileEntry to File
730
+ const getFileFromEntry = (fileEntry) => {
731
+ return new Promise((resolve) => {
732
+ fileEntry.file(resolve);
733
+ });
734
+ };
735
+ // Helper function to read all entries in a directory
736
+ const readAllDirectoryEntries = async (dirReader) => {
737
+ const entries = [];
738
+ let readBatch = [];
739
+ do {
740
+ readBatch = await new Promise((resolve, reject) => {
741
+ try {
742
+ dirReader.readEntries(resolve, reject);
743
+ }
744
+ catch (error) {
745
+ reject(error);
746
+ }
747
+ });
748
+ entries.push(...readBatch);
749
+ } while (readBatch.length > 0);
750
+ return entries;
751
+ };
752
+ // Helper function to process files and folder contents
753
+ async function processDroppedItems(dataTransferItems) {
754
+ const files = [];
755
+ const processFileSystemEntry = async (entry) => {
756
+ if (entry.isFile) {
757
+ const file = await getFileFromEntry(entry);
758
+ // drag and dropped files do not have a webkitRelativePath property,
759
+ // but they do have a fullPath property which has the same information
760
+ // https://github.com/ant-design/ant-design/issues/16426
761
+ if (entry.fullPath && !file.webkitRelativePath) {
762
+ Object.defineProperties(file, {
763
+ webkitRelativePath: {
764
+ writable: true,
765
+ },
766
+ });
767
+ // intentionally overwriting webkitRelativePath
768
+ // @ts-expect-error
769
+ file.webkitRelativePath = entry.fullPath.replace(/^\//, '');
770
+ Object.defineProperties(file, {
771
+ webkitRelativePath: {
772
+ writable: false,
773
+ },
774
+ });
775
+ }
776
+ files.push(file);
777
+ }
778
+ else if (entry.isDirectory) {
779
+ const dirReader = entry.createReader();
780
+ const dirEntries = await readAllDirectoryEntries(dirReader);
781
+ await Promise.all(dirEntries.map(processFileSystemEntry));
782
+ }
783
+ };
784
+ // Filter out and process files from the data transfer items
785
+ await Promise.all(dataTransferItems
786
+ .reduce((acc, item) => {
787
+ const entry = item.webkitGetAsEntry();
788
+ return item.kind === 'file' && entry ? [...acc, entry] : acc;
789
+ }, [])
790
+ .map(processFileSystemEntry));
791
+ return files;
792
+ }
793
+
729
794
  function useDropZone({ onDropComplete, onDragEnter: _onDragEnter, onDragLeave: _onDragLeave, onDragOver: _onDragOver, onDragStart: _onDragStart, onDrop: _onDrop, acceptedFileTypes = [], }) {
730
795
  const [dragState, setDragState] = React.useState('inactive');
731
796
  const onDragStart = (event) => {
@@ -767,13 +832,21 @@ function useDropZone({ onDropComplete, onDragEnter: _onDragEnter, onDragLeave: _
767
832
  event.preventDefault();
768
833
  event.stopPropagation();
769
834
  setDragState('inactive');
770
- const files = Array.from(event.dataTransfer.files);
771
- const { acceptedFiles, rejectedFiles } = filterAllowedFiles(files, acceptedFileTypes);
835
+ const { files, items } = event.dataTransfer;
772
836
  if (ui.isFunction(_onDrop)) {
773
837
  _onDrop(event);
774
838
  }
775
- if (ui.isFunction(onDropComplete)) {
776
- onDropComplete({ acceptedFiles, rejectedFiles });
839
+ const completeDrop = (files) => {
840
+ const { acceptedFiles, rejectedFiles } = filterAllowedFiles(files, acceptedFileTypes);
841
+ if (ui.isFunction(onDropComplete)) {
842
+ onDropComplete({ acceptedFiles, rejectedFiles });
843
+ }
844
+ };
845
+ if (!items) {
846
+ completeDrop(Array.from(files));
847
+ }
848
+ else {
849
+ processDroppedItems(Array.from(items)).then(completeDrop);
777
850
  }
778
851
  };
779
852
  return {
@@ -0,0 +1 @@
1
+ export declare function processDroppedItems(dataTransferItems: DataTransferItem[]): Promise<File[]>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aws-amplify/ui-react-core",
3
- "version": "3.0.28",
3
+ "version": "3.0.30",
4
4
  "main": "dist/index.js",
5
5
  "module": "dist/esm/index.mjs",
6
6
  "react-native": "src/index.ts",
@@ -40,14 +40,14 @@
40
40
  "typecheck": "tsc --noEmit"
41
41
  },
42
42
  "dependencies": {
43
- "@aws-amplify/ui": "6.6.4",
43
+ "@aws-amplify/ui": "6.6.6",
44
44
  "@xstate/react": "^3.2.2",
45
45
  "lodash": "4.17.21",
46
46
  "react-hook-form": "^7.43.5",
47
47
  "xstate": "^4.33.6"
48
48
  },
49
49
  "peerDependencies": {
50
- "aws-amplify": "^6.6.0",
50
+ "aws-amplify": "^6.6.5",
51
51
  "react": "^16.14.0 || ^17.0 || ^18.0"
52
52
  },
53
53
  "sideEffects": false
@@ -1,6 +1,7 @@
1
1
  import { useState } from 'react';
2
2
  import { isFunction } from '@aws-amplify/ui';
3
3
  import { filterAllowedFiles } from '../utils/filterAllowedFiles';
4
+ import { processDroppedItems } from '../utils/processDroppedItems';
4
5
 
5
6
  interface DragEvents {
6
7
  onDragStart: (event: React.DragEvent<HTMLDivElement>) => void;
@@ -85,17 +86,27 @@ export default function useDropZone({
85
86
  event.preventDefault();
86
87
  event.stopPropagation();
87
88
  setDragState('inactive');
88
- const files = Array.from(event.dataTransfer.files);
89
- const { acceptedFiles, rejectedFiles } = filterAllowedFiles<File>(
90
- files,
91
- acceptedFileTypes
92
- );
89
+
90
+ const { files, items } = event.dataTransfer;
93
91
 
94
92
  if (isFunction(_onDrop)) {
95
93
  _onDrop(event);
96
94
  }
97
- if (isFunction(onDropComplete)) {
98
- onDropComplete({ acceptedFiles, rejectedFiles });
95
+
96
+ const completeDrop = (files: File[]) => {
97
+ const { acceptedFiles, rejectedFiles } = filterAllowedFiles<File>(
98
+ files,
99
+ acceptedFileTypes
100
+ );
101
+ if (isFunction(onDropComplete)) {
102
+ onDropComplete({ acceptedFiles, rejectedFiles });
103
+ }
104
+ };
105
+
106
+ if (!items) {
107
+ completeDrop(Array.from(files));
108
+ } else {
109
+ processDroppedItems(Array.from(items)).then(completeDrop);
99
110
  }
100
111
  };
101
112
 
@@ -0,0 +1,79 @@
1
+ // Helper function to convert FileSystemFileEntry to File
2
+ const getFileFromEntry = (fileEntry: FileSystemFileEntry): Promise<File> => {
3
+ return new Promise((resolve) => {
4
+ fileEntry.file(resolve);
5
+ });
6
+ };
7
+
8
+ // Helper function to read all entries in a directory
9
+ const readAllDirectoryEntries = async (
10
+ dirReader: FileSystemDirectoryReader
11
+ ): Promise<FileSystemEntry[]> => {
12
+ const entries: FileSystemEntry[] = [];
13
+
14
+ let readBatch: FileSystemEntry[] = [];
15
+ do {
16
+ readBatch = await new Promise<FileSystemEntry[]>((resolve, reject) => {
17
+ try {
18
+ dirReader.readEntries(resolve, reject);
19
+ } catch (error) {
20
+ reject(error);
21
+ }
22
+ });
23
+ entries.push(...readBatch);
24
+ } while (readBatch.length > 0);
25
+
26
+ return entries;
27
+ };
28
+
29
+ // Helper function to process files and folder contents
30
+ export async function processDroppedItems(
31
+ dataTransferItems: DataTransferItem[]
32
+ ): Promise<File[]> {
33
+ const files: File[] = [];
34
+
35
+ const processFileSystemEntry = async (
36
+ entry: FileSystemEntry
37
+ ): Promise<void> => {
38
+ if (entry.isFile) {
39
+ const file = await getFileFromEntry(entry as FileSystemFileEntry);
40
+ // drag and dropped files do not have a webkitRelativePath property,
41
+ // but they do have a fullPath property which has the same information
42
+ // https://github.com/ant-design/ant-design/issues/16426
43
+ if (entry.fullPath && !file.webkitRelativePath) {
44
+ Object.defineProperties(file, {
45
+ webkitRelativePath: {
46
+ writable: true,
47
+ },
48
+ });
49
+
50
+ // intentionally overwriting webkitRelativePath
51
+ // @ts-expect-error
52
+ file.webkitRelativePath = entry.fullPath.replace(/^\//, '');
53
+
54
+ Object.defineProperties(file, {
55
+ webkitRelativePath: {
56
+ writable: false,
57
+ },
58
+ });
59
+ }
60
+ files.push(file);
61
+ } else if (entry.isDirectory) {
62
+ const dirReader = (entry as FileSystemDirectoryEntry).createReader();
63
+ const dirEntries = await readAllDirectoryEntries(dirReader);
64
+ await Promise.all(dirEntries.map(processFileSystemEntry));
65
+ }
66
+ };
67
+
68
+ // Filter out and process files from the data transfer items
69
+ await Promise.all(
70
+ dataTransferItems
71
+ .reduce<FileSystemEntry[]>((acc, item) => {
72
+ const entry = item.webkitGetAsEntry();
73
+ return item.kind === 'file' && entry ? [...acc, entry] : acc;
74
+ }, [])
75
+ .map(processFileSystemEntry)
76
+ );
77
+
78
+ return files;
79
+ }