@veracity/vui 2.24.0-beta.0 → 2.24.0-beta.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@veracity/vui",
3
- "version": "2.24.0-beta.0",
3
+ "version": "2.24.0-beta.1",
4
4
  "description": "Veracity UI is a React component library crafted for use within Veracity applications and pages. Based on Styled Components and @xstyled.",
5
5
  "module": "./dist/esm/index.js",
6
6
  "main": "./dist/cjs/index.js",
@@ -1,4 +1,4 @@
1
- import React, { ChangeEvent, DragEvent, useRef } from 'react'
1
+ import { ChangeEvent, DragEvent, useRef } from 'react'
2
2
 
3
3
  import { omitThemingProps, styled, useStyleConfig, v, vui } from '../core'
4
4
  import Icon, { IconProp } from '../icon'
@@ -36,10 +36,7 @@ export const DragAndDrop = vui<'label', DragAndDropProps>((props, ref) => {
36
36
  const fileInput = useRef<HTMLInputElement>(null)
37
37
  const styles = useStyleConfig('DragAndDrop', props)
38
38
 
39
- const processFiles = (files: FileList): File[] => {
40
- console.log('files', files)
41
- return Array.from(files).map(file => file)
42
- }
39
+ const processFiles = (files: FileList): File[] => Array.from(files).map(file => file)
43
40
 
44
41
  const handleFileInput = (e: ChangeEvent<HTMLInputElement>) => {
45
42
  e.preventDefault()
@@ -53,9 +50,51 @@ export const DragAndDrop = vui<'label', DragAndDropProps>((props, ref) => {
53
50
  }
54
51
  }
55
52
 
53
+ const getFilesWebkitDataTransferItems = (dataTransferItems: DataTransferItemList): Promise<File[]> => {
54
+ const traverseFileTreePromise = (item: FileSystemEntry, path: string = ''): Promise<File[]> =>
55
+ new Promise(resolve => {
56
+ if (item.isFile) {
57
+ const fileEntry = item as FileSystemFileEntry
58
+ fileEntry.file(file => {
59
+ ;(file as any).filepath = path + file.name // Save full path with type casting
60
+ files.push(file)
61
+ resolve([file])
62
+ })
63
+ } else if (item.isDirectory) {
64
+ const dirEntry = item as FileSystemDirectoryEntry
65
+ const dirReader = dirEntry.createReader()
66
+ dirReader.readEntries(entries => {
67
+ const entriesPromises: Promise<File[]>[] = entries.map(entry =>
68
+ traverseFileTreePromise(entry, path + dirEntry.name + '/'),
69
+ )
70
+ resolve(Promise.all(entriesPromises).then(entries => [].concat(...(entries as any[]))))
71
+ })
72
+ }
73
+ })
74
+
75
+ const files: File[] = []
76
+ return new Promise((resolve, reject) => {
77
+ const entriesPromises: Promise<File[]>[] = []
78
+ for (let i = 0; i < dataTransferItems.length; i++) {
79
+ const item = dataTransferItems[i].webkitGetAsEntry()
80
+ if (item) entriesPromises.push(traverseFileTreePromise(item))
81
+ }
82
+ Promise.all(entriesPromises)
83
+ .then(() => resolve(files))
84
+ .catch(reject)
85
+ })
86
+ }
87
+
56
88
  const handleDrop = (e: DragEvent<HTMLElement>) => {
57
89
  e.preventDefault()
58
90
  if (disabled) return undefined
91
+
92
+ if (isFolderUpload) {
93
+ const items = e.dataTransfer.items
94
+ getFilesWebkitDataTransferItems(items).then(files => files?.length && onFilesAdded?.(files))
95
+ return undefined
96
+ }
97
+
59
98
  const files = e.dataTransfer?.files ? processFiles(e.dataTransfer.files) : []
60
99
  if (files?.length) onFilesAdded?.(files)
61
100
  }