Skip to content

Commit 82275db

Browse files
authored
fix(web): Cancelling image picker does not resolve the promise (react-native-image-picker#2319)
The `cancel` input event is not handled, so if the user attempts to cancel the picking process, the promise that the library returns will never be resolved. Most likely this will result in app code that is stuck waiting for the user to make a selection.
1 parent 41c9096 commit 82275db

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

src/platforms/web.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ export function imageLibrary(
6161
document.body.appendChild(input);
6262

6363
return new Promise((resolve) => {
64-
input.addEventListener('change', async () => {
64+
const inputChangeHandler = async () => {
6565
if (input.files) {
6666
if (options.selectionLimit! <= 1) {
6767
const img = await readFile(input.files[0], {
@@ -90,8 +90,22 @@ export function imageLibrary(
9090
resolve(result);
9191
}
9292
}
93+
cleanup();
94+
};
95+
96+
const inputCancelHandler = async () => {
97+
resolve({didCancel: true});
98+
cleanup();
99+
};
100+
101+
const cleanup = () => {
102+
input.removeEventListener('change', inputChangeHandler);
103+
input.removeEventListener('cancel', inputCancelHandler);
93104
document.body.removeChild(input);
94-
});
105+
};
106+
107+
input.addEventListener('change', inputChangeHandler);
108+
input.addEventListener('cancel', inputCancelHandler);
95109

96110
const event = new MouseEvent('click');
97111
input.dispatchEvent(event);

0 commit comments

Comments
 (0)