Skip to content

Commit 2d6571d

Browse files
authored
feat: remove pickMultiple (#651)
BREAKING CHANGE: pickMultiple removed
1 parent 5869ad0 commit 2d6571d

File tree

3 files changed

+46
-57
lines changed

3 files changed

+46
-57
lines changed

README.md

+42-44
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,17 @@ New architecture is supported.
2626

2727
- [react-native-document-picker](#react-native-document-picker)
2828
- [Installation](#installation)
29-
- [RN >= 0.63](#rn--063)
29+
- [RN >= 0.69](#rn--063)
3030
- [API](#api)
31-
- [DocumentPicker.pickMultiple(options) / DocumentPicker.pickSingle(options) / DocumentPicker.pick(options)](#documentpickerpickmultipleoptions--documentpickerpicksingleoptions--documentpickerpickoptions)
32-
- [DocumentPicker.pickDirectory()](#documentpickerpickdirectory)
33-
- [DocumentPicker.pick(options) and DocumentPicker.pickMultiple(options)](#documentpickerpickoptions-and-documentpickerpickmultipleoptions)
31+
- [pickSingle(options) / pick(options)](#picksingleoptions--pickoptions)
32+
- [pickDirectory()](#pickdirectory)
3433
- [Options](#options)
3534
- [allowMultiSelection:boolean](#allowmultiselectionboolean)
3635
- [type:string|Array<string>](#typestringarraystring)
36+
- [[iOS and Android only] copyTo:"cachesDirectory" | "documentDirectory"](#ios-and-android-only-copytocachesdirectory--documentdirectory)
3737
- [[iOS only] presentationStyle:'fullScreen' | 'pageSheet' | 'formSheet' | 'overFullScreen'](#ios-only-presentationstylefullscreen--pagesheet--formsheet--overfullscreen)
3838
- [[iOS only] transitionStyle:'coverVertical' | 'flipHorizontal' | 'crossDissolve' | 'partialCurl'](#ios-only-transitionstylecoververtical--fliphorizontal--crossdissolve--partialcurl)
3939
- [[iOS only] mode:"import" | "open"](#ios-only-modeimport--open)
40-
- [[iOS and Android only] copyTo:"cachesDirectory" | "documentDirectory"](#ios-and-android-only-copytocachesdirectory--documentdirectory)
4140
- [[Windows only] readContent:boolean](#windows-only-readcontentboolean)
4241
- [Result](#result)
4342
- [uri](#uri)
@@ -46,10 +45,10 @@ New architecture is supported.
4645
- [name](#name)
4746
- [size](#size)
4847
- [[Windows only] content](#windows-only-content)
49-
- [DocumentPicker.types.\*](#documentpickertypes)
50-
- [DocumentPicker.isCancel(err)](#documentpickeriscancelerr)
51-
- [DocumentPicker.isInProgress(err)](#documentpickerisinprogresserr)
52-
- [[iOS only] DocumentPicker.releaseSecureAccess(uris: Array<string>)](#ios-only-documentpickerreleasesecureaccessuris-arraystring)
48+
- [types.\*](#types)
49+
- [isCancel(err)](#iscancelerr)
50+
- [isInProgress(err)](#isinprogresserr)
51+
- [[iOS only] releaseSecureAccess(uris: Array<string>)](#ios-only-releasesecureaccessuris-arraystring)
5352
- [Example](#example)
5453
- [How to upload picked files?](#how-to-upload-picked-files)
5554
- [Help wanted: Improvements](#help-wanted-improvements)
@@ -66,6 +65,10 @@ OR
6665
yarn add react-native-document-picker
6766
```
6867

68+
```ts
69+
import DocumentPicker from 'react-native-document-picker'
70+
```
71+
6972
#### Expo
7073

7174
This package is supported in Expo managed workflow through the usage of [custom development clients](https://docs.expo.dev/development/getting-started/).
@@ -83,73 +86,69 @@ expo run:android
8386
```
8487

8588

86-
#### RN >= 0.63
89+
#### RN >= 0.69
8790

88-
If you are using RN >= 0.63, only run `pod install` from the ios directory. Then rebuild your project. Older RN versions are not supported.
91+
If you are using RN >= 0.69, only run `pod install` from the ios directory. Then rebuild your project. Older RN versions are not supported.
8992

9093
## API
9194

92-
### `DocumentPicker.pickMultiple(options)` / `DocumentPicker.pickSingle(options)` / `DocumentPicker.pick(options)`
95+
#### `pickSingle(options)` / `pick(options)`
9396

9497
⚠️ Breaking in v6: `pick` returns a `Promise<Array<DocumentPickerResponse>>` instead of `Promise<DocumentPickerResponse>`. If you were using `pick`, change those usages to `pickSingle`.
9598

96-
Use `pickMultiple`, `pickSingle` or `pick` to open a document picker for the user to select file(s). All methods return a Promise.
97-
98-
### `DocumentPicker.pickDirectory()`
99+
Use `pickSingle` or `pick` to open a document picker for the user to select file(s).
99100

100-
Open a system directory picker. Returns a promise that resolves to (`{ uri: string }`) of the directory selected by user.
101+
- with `pick`, you can use `allowMultiSelection` param to control whether user can select multiple files (`false` by default). Returns a `Promise<Array<DocumentPickerResponse>>`
101102

102-
### `DocumentPicker.pick(options)` and `DocumentPicker.pickMultiple(options)`
103+
- `pickSingle` is "sugar function" on top of `pick` and only allows a single selection returns `Promise<DocumentPickerResponse>`
103104

104-
- `pick` is the most universal, you can use `allowMultiSelection` param to control whether or not user can select multiple files (`false` by default). Returns a `Promise<Array<DocumentPickerResponse>>`
105105

106-
`pickSingle` and `pickMultiple` are "sugar functions" on top of `pick`, and they _might be removed_ in a future release for increased API clarity.
106+
#### `pickDirectory()`
107107

108-
- `pickSingle` only allows a single selection and the Promise will resolve to that single result (same behavior as `pick` in v5)
109-
- `pickMultiple` allows multiple selection and the Promise will resolve to an array of results.
108+
Open a system directory picker. Returns a promise that resolves to (`{ uri: string }`) of the directory selected by user.
110109

111110
## Options
112111

113112
All of the options are optional
114113

115114
#### `allowMultiSelection`:`boolean`
116115

117-
Whether selecting multiple files is allowed. For `pick`, this is `false` by default. `allowMultiSelection` is `false` for `pickSingle` and `true` for `pickMultiple` and cannot be overridden for those calls.
116+
Whether selecting multiple files is allowed. For `pick`, this is `false` by default. `allowMultiSelection` is `false` for `pickSingle` and cannot be overridden.
118117

119118
#### `type`:`string|Array<string>`
120119

121-
The type or types of documents to allow selection of. May be an array of types as single type string.
120+
The type or types of documents to allow selection of. An array of strings or single string.
122121

123-
- On Android these are MIME types such as `text/plain` or partial MIME types such as `image/*`. See [common MIME types](https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Common_types).
124-
- On iOS these must be Apple "[Uniform Type Identifiers](https://developer.apple.com/documentation/uniformtypeidentifiers/system-declared_uniform_type_identifiers?language=objc)"
122+
- On Android, these are MIME types such as `text/plain` or partial MIME types such as `image/*`. See [common MIME types](https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Common_types).
123+
- On iOS, these must be Apple "[Uniform Type Identifiers](https://developer.apple.com/documentation/uniformtypeidentifiers/system-declared_uniform_type_identifiers?language=objc)"
125124
- If `type` is omitted it will be treated as `*/*` or `public.item`.
126125

127-
#### [iOS only] `presentationStyle`:`'fullScreen' | 'pageSheet' | 'formSheet' | 'overFullScreen'`
126+
#### [iOS and Android only] `copyTo`:`"cachesDirectory" | "documentDirectory"`
128127

129-
Controls how the picker is presented, eg. on an iPad you may want to present it fullscreen. Defaults to `pageSheet`.
128+
If specified, the picked file is copied to `NSCachesDirectory` / `NSDocumentDirectory` (iOS) or `getCacheDir` / `getFilesDir` (Android). The uri of the copy will be available in result's `fileCopyUri`. If copying the file fails (e.g. due to lack of free space), `fileCopyUri` will be `null`, and more details about the error will be available in `copyError` field in the result.
130129

131-
#### [iOS only] `transitionStyle`:`'coverVertical' | 'flipHorizontal' | 'crossDissolve' | 'partialCurl'`
130+
This should help if you need to work with the file(s) later on, because by default, [the picked documents are temporary files. They remain available only until your application terminates](https://developer.apple.com/documentation/uikit/uidocumentpickerdelegate/2902364-documentpicker). This may impact performance for large files, so keep this in mind if you expect users to pick particularly large files and your app does not need immediate read access.
132131

133-
Configure the transition style of the picker. Defaults to `coverVertical`, when the picker is presented, its view slides up from the bottom of the screen.
132+
On Android, this can be used to obtain local, on-device copy of the file (e.g. if user picks a document from Google Drive, this will download it locally to the phone).
134133

135-
#### [iOS only] `mode`:`"import" | "open"`
134+
#### [iOS only] `presentationStyle`:`'fullScreen' | 'pageSheet' | 'formSheet' | 'overFullScreen'`
136135

137-
Defaults to `import`. If `mode` is set to `import` the document picker imports the file from outside to inside the sandbox, otherwise if `mode` is set to `open` the document picker opens the file right in place.
136+
Controls how the picker is presented, e.g. on an iPad you may want to present it fullscreen. Defaults to `pageSheet`.
138137

139-
#### [iOS and Android only] `copyTo`:`"cachesDirectory" | "documentDirectory"`
138+
#### [iOS only] `transitionStyle`:`'coverVertical' | 'flipHorizontal' | 'crossDissolve' | 'partialCurl'`
140139

141-
If specified, the picked file is copied to `NSCachesDirectory` / `NSDocumentDirectory` (iOS) or `getCacheDir` / `getFilesDir` (Android). The uri of the copy will be available in result's `fileCopyUri`. If copying the file fails (eg. due to lack of free space), `fileCopyUri` will be `null`, and more details about the error will be available in `copyError` field in the result.
140+
Configure the transition style of the picker. Defaults to `coverVertical`, when the picker is presented, its view slides up from the bottom of the screen.
142141

143-
This should help if you need to work with the file(s) later on, because by default, [the picked documents are temporary files. They remain available only until your application terminates](https://developer.apple.com/documentation/uikit/uidocumentpickerdelegate/2902364-documentpicker). This may impact performance for large files, so keep this in mind if you expect users to pick particularly large files and your app does not need immediate read access.
142+
#### [iOS only] `mode`:`"import" | "open"`
144143

145-
On Android, this can be used to obtain local, on-device copy of the file (e.g. if user picks a document from google drive, this will download it locally to the phone).
144+
Defaults to `import`. If `mode` is set to `import` the document picker imports the file from outside to inside the sandbox, otherwise if `mode` is set to `open` the document picker opens the file in-place.
146145

147146
#### [Windows only] `readContent`:`boolean`
148147

149148
Defaults to `false`. If `readContent` is set to true the content of the picked file/files will be read and supplied in the result object.
150149

151150
- Be aware that this can introduce a huge performance hit in case of big files. (The files are read completely and into the memory and encoded to base64 afterwards to add them to the result object)
152-
- However reading the file directly from within the Thread which managed the picker can be necessary on Windows: Windows Apps can only read the Downloads folder and their own app folder by default and If a file is outside of these locations it cannot be acessed directly. However if the user picks the file through a file picker permissions to that file are granted implicitly.
151+
- However, reading the file directly from within the Thread which managed the picker can be necessary on Windows: Windows Apps can only read the Downloads folder and their own app folder by default and If a file is outside of these locations it cannot be acessed directly. However if the user picks the file through a file picker permissions to that file are granted implicitly.
153152

154153
```
155154
In addition to the default locations, an app can access additional files and folders by declaring capabilities in the app manifest (see App capability declarations), or by calling a file picker to let the user pick files and folders for the app to access (see Open files and folders with a picker).
@@ -191,7 +190,7 @@ The file size of the document. _On Android some DocumentProviders may not provid
191190

192191
The base64 encoded content of the picked file if the option `readContent` was set to `true`.
193192

194-
## `DocumentPicker.types.*`
193+
## `types.*`
195194

196195
`DocumentPicker.types.*` provides a few common types for use as `type` values, these types will use the correct format for each platform (MIME types on Android, UTIs on iOS).
197196

@@ -209,26 +208,25 @@ The base64 encoded content of the picked file if the option `readContent` was se
209208
- `DocumentPicker.types.xls`: xls files
210209
- `DocumentPicker.types.xlsx`: xlsx files
211210

212-
### `DocumentPicker.isCancel(err)`
211+
#### `isCancel(err)`
213212

214-
If the user cancels the document picker without choosing a file (by pressing the system back button on Android or the Cancel button on iOS) the Promise will be rejected with a cancellation error. You can check for this error using `DocumentPicker.isCancel(err)` allowing you to ignore it and cleanup any parts of your interface that may not be needed anymore.
213+
If the user cancels the document picker without choosing a file (by pressing the system back button on Android or the Cancel button on iOS), the Promise will be rejected with a cancellation error. You can check for this error using `DocumentPicker.isCancel(err)` allowing you to ignore it and cleanup any parts of your interface that may not be needed anymore.
215214

216-
### `DocumentPicker.isInProgress(err)`
215+
#### `isInProgress(err)`
217216

218-
If the user somehow manages to open multiple file pickers (eg. due the app being unresponsive), then only the picked result from the last opened picker will be considered and the promises from previous opened pickers will be rejected with an error that you can check using `DocumentPicker.isInProgress()`.
217+
If the user somehow manages to open multiple file pickers (e.g. due the app being unresponsive), then only the picked result from the last opened picker will be considered and the promises from previous opened pickers will be rejected with an error that you can check using `DocumentPicker.isInProgress()`.
219218

220219
This behavior might change in future to allow opening only a single picker at a time. The internal logic is currently implemented only on iOS.
221220

222-
### [iOS only] `DocumentPicker.releaseSecureAccess(uris: Array<string>)`
221+
#### [iOS only] `releaseSecureAccess(uris: Array<string>)`
223222

224-
If `mode` is set to `open` iOS is giving you secure access to a file located outside from your sandbox.
223+
If `mode` is set to `open`, iOS is giving you secure access to a file located outside from your sandbox.
225224
In that case Apple is asking you to release the access as soon as you finish using the resource.
226225

227226
## Example
228227

229228
See the example app in `example` folder.
230229

231-
232230
## How to upload picked files?
233231

234232
Use blob support that is built into react native - [see comment](https://github.com/rnmods/react-native-document-picker/issues/70#issuecomment-384335402).

example/App.tsx

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { StyleSheet, View, Text, Button } from 'react-native'
44
import DocumentPicker, {
55
DirectoryPickerResponse,
66
DocumentPickerResponse,
7+
isCancel,
78
isInProgress,
89
types,
910
} from 'react-native-document-picker'
@@ -19,7 +20,7 @@ export default function App() {
1920
}, [result])
2021

2122
const handleError = (err: unknown) => {
22-
if (DocumentPicker.isCancel(err)) {
23+
if (isCancel(err)) {
2324
console.warn('cancelled')
2425
// User cancelled the picker, exit any dialogs or menus and move on
2526
} else if (isInProgress(err)) {
@@ -48,7 +49,7 @@ export default function App() {
4849
<Button
4950
title="open picker for multi file selection"
5051
onPress={() => {
51-
DocumentPicker.pickMultiple().then(setResult).catch(handleError)
52+
DocumentPicker.pick({ allowMultiSelection: true }).then(setResult).catch(handleError)
5253
}}
5354
/>
5455
<Button

src/index.tsx

+1-11
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,6 @@ export async function pickDirectory<OS extends SupportedPlatforms>(
4848
}
4949
}
5050

51-
export function pickMultiple<OS extends SupportedPlatforms>(
52-
opts?: DocumentPickerOptions<OS>,
53-
): Promise<DocumentPickerResponse[]> {
54-
const options = {
55-
...opts,
56-
allowMultiSelection: true,
57-
}
58-
return pick(options)
59-
}
6051
export function pickSingle<OS extends SupportedPlatforms>(
6152
opts?: DocumentPickerOptions<OS>,
6253
): Promise<DocumentPickerResponse> {
@@ -116,7 +107,6 @@ function doPick<OS extends SupportedPlatforms>(
116107
)
117108

118109
invariant(
119-
// @ts-ignore TS2345: Argument of type 'string' is not assignable to parameter of type 'PlatformTypes[OS][keyof PlatformTypes[OS]]'.
120110
!options.type.includes('folder'),
121111
'RN document picker: "folder" option was removed, use "pickDirectory()"',
122112
)
@@ -171,10 +161,10 @@ function isErrorWithCode(err: unknown, errorCode: string): boolean {
171161

172162
export default {
173163
isCancel,
164+
isInProgress,
174165
releaseSecureAccess,
175166
pickDirectory,
176167
pick,
177-
pickMultiple,
178168
pickSingle,
179169
types,
180170
perPlatformTypes,

0 commit comments

Comments
 (0)