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.
@@ -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 | DocumentResult[]) => void): Promise<void> => {
18
- const omittedOptions = omitBy(options, isNil);
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
- [multiple, onCancel, onError, options],
32
+ [options],
43
33
  );
44
34
 
45
35
  return { openFilePicker };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-molecules",
3
- "version": "0.5.0-beta.11",
3
+ "version": "0.5.0-beta.12",
4
4
  "author": "Thet Aung <thetaung.dev@gmail.com>",
5
5
  "license": "MIT",
6
6
  "main": "index.ts",
@@ -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
- }: DocumentPickerOptions): Promise<DocumentResult | DocumentResult[]> => {
42
+ onCancel,
43
+ onError,
44
+ }: DocumentPickerOptions): Promise<DocumentResult[]> => {
35
45
  // SSR guard
36
46
  if (Platform.OS !== 'web') {
37
- return { type: 'cancel' };
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
- if (input.files) {
55
- const response: Promise<DocumentResult>[] = [];
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
- Array.from(input.files).forEach(file => response.push(resolveFileData(file)));
70
+ Array.from(input.files).forEach(file => response.push(resolveFileData(file)));
58
71
 
59
- return resolve(Promise.all(response));
60
- } else {
61
- resolve({ type: 'cancel' });
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: ({ type }: DocumentPickerOptions) => getDocumentAsyncWeb({ type, multiple: false }),
74
- pickMultiple: ({ type }: DocumentPickerOptions) =>
75
- getDocumentAsyncWeb({ type, multiple: true }),
99
+ pickSingle,
100
+ pickMultiple,
76
101
  };
@@ -12,7 +12,6 @@ export type DocumentPickerOptions = Omit<RNDocumentPickerOptions, 'allowMultiSel
12
12
  multiple?: boolean;
13
13
  /**
14
14
  * runs when the DocumentPicker is cancelled
15
- * currently, only supported on IOS and Android
16
15
  */
17
16
  onCancel?: () => void;
18
17
  /**