react-native-molecules 0.5.0-beta.11 → 0.5.0-beta.12
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/hooks/useFilePicker.tsx
CHANGED
|
@@ -7,15 +7,11 @@ import {
|
|
|
7
7
|
} from '../utils/DocumentPicker';
|
|
8
8
|
import { isNil, omitBy } from '../utils/lodash';
|
|
9
9
|
|
|
10
|
-
export const useFilePicker = ({
|
|
11
|
-
multiple,
|
|
12
|
-
onCancel,
|
|
13
|
-
onError,
|
|
14
|
-
...options
|
|
15
|
-
}: DocumentPickerOptions) => {
|
|
10
|
+
export const useFilePicker = (options: DocumentPickerOptions) => {
|
|
16
11
|
const openFilePicker = useCallback(
|
|
17
|
-
async (callback: (response: DocumentResult
|
|
18
|
-
const
|
|
12
|
+
async (callback: (response: DocumentResult[]) => void) => {
|
|
13
|
+
const { multiple, ...rest } = options;
|
|
14
|
+
const omittedOptions = omitBy(rest, isNil);
|
|
19
15
|
|
|
20
16
|
try {
|
|
21
17
|
let response;
|
|
@@ -30,16 +26,10 @@ export const useFilePicker = ({
|
|
|
30
26
|
} catch (e: any) {
|
|
31
27
|
// eslint-disable-next-line no-console
|
|
32
28
|
console.log('FilePicker Error', e, e?.code);
|
|
33
|
-
|
|
34
|
-
if (e?.code === 'DOCUMENT_PICKER_CANCELED.') {
|
|
35
|
-
onCancel?.();
|
|
36
|
-
return;
|
|
37
|
-
}
|
|
38
|
-
// It might result in an error.
|
|
39
|
-
onError?.(e);
|
|
29
|
+
// Error and cancel callbacks are handled by DocumentPicker itself
|
|
40
30
|
}
|
|
41
31
|
},
|
|
42
|
-
[
|
|
32
|
+
[options],
|
|
43
33
|
);
|
|
44
34
|
|
|
45
35
|
return { openFilePicker };
|
package/package.json
CHANGED
|
@@ -2,6 +2,14 @@ import { Platform } from 'react-native';
|
|
|
2
2
|
|
|
3
3
|
import type { DocumentPickerOptions, DocumentResult } from './types';
|
|
4
4
|
|
|
5
|
+
class OperationCanceledError extends Error {
|
|
6
|
+
code = 'OPERATION_CANCELED';
|
|
7
|
+
constructor() {
|
|
8
|
+
super('user canceled the document picker');
|
|
9
|
+
this.name = 'OperationCanceledError';
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
|
|
5
13
|
const resolveFileData = (file: File): Promise<DocumentResult> => {
|
|
6
14
|
return new Promise((resolve, reject) => {
|
|
7
15
|
const mimeType = file.type;
|
|
@@ -31,10 +39,14 @@ const resolveFileData = (file: File): Promise<DocumentResult> => {
|
|
|
31
39
|
const getDocumentAsyncWeb = async ({
|
|
32
40
|
type = '*/*',
|
|
33
41
|
multiple = false,
|
|
34
|
-
|
|
42
|
+
onCancel,
|
|
43
|
+
onError,
|
|
44
|
+
}: DocumentPickerOptions): Promise<DocumentResult[]> => {
|
|
35
45
|
// SSR guard
|
|
36
46
|
if (Platform.OS !== 'web') {
|
|
37
|
-
|
|
47
|
+
const error = new OperationCanceledError();
|
|
48
|
+
onCancel?.();
|
|
49
|
+
throw error;
|
|
38
50
|
}
|
|
39
51
|
|
|
40
52
|
const input = document.createElement('input');
|
|
@@ -49,19 +61,27 @@ const getDocumentAsyncWeb = async ({
|
|
|
49
61
|
|
|
50
62
|
document.body.appendChild(input);
|
|
51
63
|
|
|
52
|
-
return new Promise(resolve => {
|
|
53
|
-
input.addEventListener('change', () => {
|
|
54
|
-
|
|
55
|
-
|
|
64
|
+
return new Promise((resolve, reject) => {
|
|
65
|
+
input.addEventListener('change', async () => {
|
|
66
|
+
try {
|
|
67
|
+
if (input.files && input.files.length > 0) {
|
|
68
|
+
const response: Promise<DocumentResult>[] = [];
|
|
56
69
|
|
|
57
|
-
|
|
70
|
+
Array.from(input.files).forEach(file => response.push(resolveFileData(file)));
|
|
58
71
|
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
72
|
+
const results = await Promise.all(response);
|
|
73
|
+
resolve(results);
|
|
74
|
+
} else {
|
|
75
|
+
const error = new OperationCanceledError();
|
|
76
|
+
onCancel?.();
|
|
77
|
+
reject(error);
|
|
78
|
+
}
|
|
79
|
+
} catch (error) {
|
|
80
|
+
onError?.(error);
|
|
81
|
+
reject(error);
|
|
82
|
+
} finally {
|
|
83
|
+
document.body.removeChild(input);
|
|
62
84
|
}
|
|
63
|
-
|
|
64
|
-
document.body.removeChild(input);
|
|
65
85
|
});
|
|
66
86
|
|
|
67
87
|
const event = new MouseEvent('click');
|
|
@@ -69,8 +89,13 @@ const getDocumentAsyncWeb = async ({
|
|
|
69
89
|
});
|
|
70
90
|
};
|
|
71
91
|
|
|
92
|
+
const pickSingle = (options: DocumentPickerOptions = {}) =>
|
|
93
|
+
getDocumentAsyncWeb({ ...options, multiple: false });
|
|
94
|
+
|
|
95
|
+
const pickMultiple = (options: DocumentPickerOptions = {}) =>
|
|
96
|
+
getDocumentAsyncWeb({ ...options, multiple: true });
|
|
97
|
+
|
|
72
98
|
export default {
|
|
73
|
-
pickSingle
|
|
74
|
-
pickMultiple
|
|
75
|
-
getDocumentAsyncWeb({ type, multiple: true }),
|
|
99
|
+
pickSingle,
|
|
100
|
+
pickMultiple,
|
|
76
101
|
};
|