-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathUseImperativeFilePicker.tsx
More file actions
95 lines (91 loc) · 3.35 KB
/
UseImperativeFilePicker.tsx
File metadata and controls
95 lines (91 loc) · 3.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import React, { useState } from 'react';
import { useImperativeFilePicker } from 'use-file-picker';
import { PersistentFileAmountLimitValidator } from 'use-file-picker/validators';
export const UseImperativeFilePicker = () => {
// for imperative file picker, if you want to limit amount of files selected by user, you need to pass persistent validator
const validators = React.useMemo(() => [new PersistentFileAmountLimitValidator({ min: 2, max: 3 })], []);
const [selectionMode, setSelectionMode] = useState<'file' | 'dir'>('file');
const { openFilePicker, filesContent, loading, errors, plainFiles, clear, removeFileByIndex, removeFileByReference } =
useImperativeFilePicker({
multiple: true,
readAs: 'DataURL',
readFilesContent: true,
validators,
initializeWithCustomParameters(inputElement) {
inputElement.webkitdirectory = selectionMode === 'dir';
},
onFilesSelected: ({ plainFiles, filesContent, errors }) => {
// this callback is always called, even if there are errors
console.log('onFilesSelected', plainFiles, filesContent, errors);
},
onFilesRejected: ({ errors }) => {
// this callback is called when there were validation errors
console.log('onFilesRejected', errors);
},
onFilesSuccessfullySelected: ({ plainFiles, filesContent }) => {
// this callback is called when there were no validation errors
console.log('onFilesSuccessfullySelected', plainFiles, filesContent);
},
onFileRemoved(file, removedIndex) {
// this callback is called when file is removed
console.log('onFileRemoved', file, removedIndex);
},
});
if (loading) {
return <div>Loading...</div>;
}
return (
<div>
<button onClick={() => setSelectionMode(selectionMode === 'file' ? 'dir' : 'file')}>
{selectionMode === 'file' ? 'FILE' : 'DIR'}
</button>
<button
onClick={async () => {
const f = await openFilePicker();
console.log(f);
}}
>
Select file
</button>
<button onClick={() => clear()}>Clear</button>
<br />
Amount of selected files:
{plainFiles.length}
<br />
Amount of filesContent:
{filesContent.length}
<br />
{!!errors.length && (
<>
Errors:
{errors.map((error, index) => (
<div key={error.name}>
<span>{index + 1}.</span>
{Object.entries(error).map(([key, value]) => (
<div key={key}>
{key}: {typeof value === 'string' ? value : Array.isArray(value) ? value.join(', ') : null}
</div>
))}
</div>
))}
</>
)}
<br />
{plainFiles.map((file, i) => (
<div key={crypto.randomUUID()}>
<div style={{ display: 'flex', alignItems: 'center' }} key={file.name}>
<div>{file.name}</div>
<button style={{ marginLeft: 24 }} onClick={() => removeFileByReference(file)}>
Remove by reference
</button>
<button style={{ marginLeft: 24 }} onClick={() => removeFileByIndex(i)}>
Remove by index
</button>
</div>
<div>{filesContent[i]?.content}</div>
</div>
))}
</div>
);
};
export default UseImperativeFilePicker;