react-native-document-picker-macos
Version:
47 lines (45 loc) • 1.57 kB
JavaScript
;
import { NativeModules, Platform } from 'react-native';
const LINKING_ERROR = 'The package \'react-native-document-picker-macos\' doesn\'t seem to be linked. Make sure: \n\n' + '- You have run \'pod install\'\n' + '- You rebuilt the app after installing the package\n' + '- You are not using Expo Go\n';
const PLATFORM_ERROR = 'The package \'react-native-document-picker-macos\' only works on macOS';
const RNDocumentPicker = Platform.select({
macos: NativeModules.RNDocumentPicker ?? new Proxy({}, {
get() {
throw new Error(LINKING_ERROR);
}
}),
default: new Proxy({}, {
get() {
throw new Error(PLATFORM_ERROR);
}
})
});
/**
* Presents the native macOS file picker
* Resolves with an array of results; at most 1 element if `multiple` is false, or none if the user canceled the dialog
*/
export async function pickFile(options) {
return RNDocumentPicker.pick({
...options,
allowFileSelection: true,
allowDirectorySelection: false
}).catch(handleError);
}
/**
* Presents the native macOS directory picker
* Resolves with an array of results; at most 1 element if `multiple` is false, or none if the user canceled the dialog
*/
export async function pickDirectory(options) {
return RNDocumentPicker.pick({
...options,
allowDirectorySelection: true,
allowFileSelection: false
}).catch(handleError);
}
function handleError(error) {
if (error instanceof Error && 'code' in error && error.code === 'USER_CANCELLED') {
return [];
}
throw error;
}
//# sourceMappingURL=index.js.map