Skip to content

Commit 9edb152

Browse files
committed
fix(FileInput): issue with paths
1 parent 2a3063e commit 9edb152

3 files changed

Lines changed: 37 additions & 3 deletions

File tree

.changeset/slow-swans-tan.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@ultraviolet/ui": patch
3+
---
4+
5+
`FileInput`: keep file path when dropping folders

packages/ui/src/components/FileInput/__stories__/AllowDirs.stories.tsx

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { useState } from 'react'
33
import { FileInput } from '..'
44
import { Button } from '../../Button'
55
import { Stack } from '../../Stack'
6+
import { Text } from '../../Text'
67
import type { FilesType } from '../types'
78

89
export const AllowDirectories: StoryFn<typeof FileInput> = args => {
@@ -27,6 +28,18 @@ export const AllowDirectories: StoryFn<typeof FileInput> = args => {
2728
</>
2829
)}
2930
</FileInput>
31+
{files ? (
32+
<Text as="div" variant="body">
33+
File{files?.length > 1 ? 's' : ''} (with path):
34+
<Text as="ul" variant="body">
35+
{files?.map(file => (
36+
<Text key={file.webkitRelativePath} as="li" variant="body">
37+
<strong>{file.name}</strong>: {file.webkitRelativePath}
38+
</Text>
39+
))}
40+
</Text>
41+
</Text>
42+
) : null}
3043
</Stack>
3144
)
3245
}

packages/ui/src/components/FileInput/helpers.ts

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,25 @@ const isDirectoryEntry = (entry: FileSystemEntry): entry is FileSystemDirectoryE
6161

6262
const convertEntryToFile = (entry: FileSystemFileEntry): Promise<File> =>
6363
new Promise((resolve, reject) => {
64-
entry.file(resolve, reject)
64+
entry.file(file => {
65+
const cleanPath = entry.fullPath?.startsWith('/') ? entry.fullPath.slice(1) : entry.fullPath
66+
67+
// If cleanPath === file.name => the file was NOT dropped from a folder, so we don't need to correct the webkitRelativePath
68+
// If cleanPath === file.webkitRelativePath => no issue in webkitRelativePath so no need to correct it
69+
if (cleanPath !== file.webkitRelativePath && cleanPath !== file.name) {
70+
Object.defineProperty(file, 'webkitRelativePath', {
71+
value: cleanPath,
72+
writable: true,
73+
enumerable: true,
74+
configurable: true,
75+
})
76+
}
77+
78+
resolve(file)
79+
}, reject)
6580
})
6681

67-
async function readEntriesInDirectory(directory: FileSystemDirectoryEntry) {
82+
const readEntriesInDirectory = async (directory: FileSystemDirectoryEntry) => {
6883
const reader = directory.createReader()
6984
const nestedEntries = []
7085

@@ -82,8 +97,9 @@ async function readEntriesInDirectory(directory: FileSystemDirectoryEntry) {
8297
/**
8398
* Recursive function to read all the files in a list of FileSystemEntry objects (files or directories)
8499
*/
85-
async function readEntries(entries: FileSystemEntry[]): Promise<File[]> {
100+
const readEntries = async (entries: FileSystemEntry[]): Promise<File[]> => {
86101
const readFilesPromises = entries.filter(entry => isFileEntry(entry)).map(entry => convertEntryToFile(entry))
102+
87103
const files = await Promise.all(readFilesPromises)
88104

89105
const readFoldersPromises = entries

0 commit comments

Comments
 (0)