react-native-molecules 0.5.0-beta.12 → 0.5.0-beta.13

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": "react-native-molecules",
3
- "version": "0.5.0-beta.12",
3
+ "version": "0.5.0-beta.13",
4
4
  "author": "Thet Aung <thetaung.dev@gmail.com>",
5
5
  "license": "MIT",
6
6
  "main": "index.ts",
@@ -49,43 +49,69 @@ const getDocumentAsyncWeb = async ({
49
49
  throw error;
50
50
  }
51
51
 
52
- const input = document.createElement('input');
53
- input.style.display = 'none';
54
- input.setAttribute('type', 'file');
55
- // @ts-expect-error
56
- input.setAttribute('accept', Array.isArray(type) ? type.join(',') : type);
57
-
58
- if (multiple) {
59
- input.setAttribute('multiple', 'multiple');
60
- }
52
+ return new Promise((resolve, reject) => {
53
+ try {
54
+ const input = document.createElement('input');
55
+ input.style.display = 'none';
56
+ input.setAttribute('type', 'file');
57
+ // @ts-expect-error
58
+ input.setAttribute('accept', Array.isArray(type) ? type.join(',') : type);
59
+
60
+ if (multiple) {
61
+ input.setAttribute('multiple', 'multiple');
62
+ }
61
63
 
62
- document.body.appendChild(input);
64
+ document.body.appendChild(input);
63
65
 
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>[] = [];
69
-
70
- Array.from(input.files).forEach(file => response.push(resolveFileData(file)));
71
-
72
- const results = await Promise.all(response);
73
- resolve(results);
74
- } else {
75
- const error = new OperationCanceledError();
76
- onCancel?.();
66
+ const cleanup = () => {
67
+ try {
68
+ document.body.removeChild(input);
69
+ } catch (e) {
70
+ // Input already removed, ignore
71
+ }
72
+ };
73
+
74
+ input.addEventListener('change', async () => {
75
+ try {
76
+ if (input.files && input.files.length > 0) {
77
+ const response: Promise<DocumentResult>[] = [];
78
+
79
+ Array.from(input.files).forEach(file =>
80
+ response.push(resolveFileData(file)),
81
+ );
82
+
83
+ const results = await Promise.all(response);
84
+ resolve(results);
85
+ }
86
+ } catch (error) {
87
+ onError?.(error);
77
88
  reject(error);
89
+ } finally {
90
+ cleanup();
78
91
  }
79
- } catch (error) {
92
+ });
93
+
94
+ input.addEventListener('cancel', () => {
95
+ const error = new OperationCanceledError();
96
+ onCancel?.();
97
+ cleanup();
98
+ reject(error);
99
+ });
100
+
101
+ input.addEventListener('error', () => {
102
+ const error = new Error('File picker error occurred');
80
103
  onError?.(error);
104
+ cleanup();
81
105
  reject(error);
82
- } finally {
83
- document.body.removeChild(input);
84
- }
85
- });
106
+ });
86
107
 
87
- const event = new MouseEvent('click');
88
- input.dispatchEvent(event);
108
+ const event = new MouseEvent('click');
109
+ input.dispatchEvent(event);
110
+ } catch (error) {
111
+ // Handle errors from file picker setup or opening
112
+ onError?.(error);
113
+ reject(error);
114
+ }
89
115
  });
90
116
  };
91
117