@veracity/vui 2.23.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.23.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'
@@ -23,8 +23,9 @@ const DragAndDropBase = styled.labelBox`
23
23
  export const DragAndDrop = vui<'label', DragAndDropProps>((props, ref) => {
24
24
  const {
25
25
  accept,
26
- icon = defaultIcon,
27
26
  className,
27
+ icon = defaultIcon,
28
+ isFolderUpload,
28
29
  onFilesAdded,
29
30
  multiple = true,
30
31
  text = defaultText,
@@ -49,9 +50,51 @@ export const DragAndDrop = vui<'label', DragAndDropProps>((props, ref) => {
49
50
  }
50
51
  }
51
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
+
52
88
  const handleDrop = (e: DragEvent<HTMLElement>) => {
53
89
  e.preventDefault()
54
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
+
55
98
  const files = e.dataTransfer?.files ? processFiles(e.dataTransfer.files) : []
56
99
  if (files?.length) onFilesAdded?.(files)
57
100
  }
@@ -76,14 +119,15 @@ export const DragAndDrop = vui<'label', DragAndDropProps>((props, ref) => {
76
119
  {text}
77
120
  </T>
78
121
  )}
79
- <v.input
122
+ <input
80
123
  accept={accept}
81
124
  className="vui-drag-and-drop-hidden-input"
82
- display="none"
83
125
  multiple={multiple}
84
126
  onChange={handleFileInput}
85
127
  ref={fileInput}
128
+ style={{ display: 'none' }}
86
129
  type={inputType}
130
+ {...(isFolderUpload && { webkitdirectory: 'true', directory: 'true' })}
87
131
  />
88
132
  </DragAndDropBase>
89
133
  )
@@ -10,6 +10,8 @@ export type DragAndDropProps = Omit<BoxProps, 'variant'> &
10
10
  disabled?: boolean
11
11
  /** The message icon. @default `falCloudUpload` */
12
12
  icon?: IconProp
13
+ /** Allow folder upload @default `false` */
14
+ isFolderUpload?: boolean
13
15
  /** Allows to select multiple files when the drop zone is clicked.. @default `true` */
14
16
  multiple?: boolean
15
17
  /** The message text. @default `Click or drag files to this area` */