-
-
Notifications
You must be signed in to change notification settings - Fork 441
/
App.tsx
110 lines (103 loc) · 2.79 KB
/
App.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import * as React from 'react'
import { StyleSheet, View, Text, Button } from 'react-native'
import DocumentPicker, {
DirectoryPickerResponse,
DocumentPickerResponse,
isCancel,
isInProgress,
types,
} from 'react-native-document-picker'
import { useEffect } from 'react'
export default function App() {
const [result, setResult] = React.useState<
Array<DocumentPickerResponse> | DirectoryPickerResponse | undefined | null
>()
useEffect(() => {
console.log(JSON.stringify(result, null, 2))
}, [result])
const handleError = (err: unknown) => {
if (isCancel(err)) {
console.warn('cancelled')
// User cancelled the picker, exit any dialogs or menus and move on
} else if (isInProgress(err)) {
console.warn('multiple pickers were opened, only the last will be considered')
} else {
throw err
}
}
return (
<View style={styles.container}>
<Button
title="open picker for single file selection"
onPress={async () => {
try {
const pickerResult = await DocumentPicker.pickSingle({
presentationStyle: 'fullScreen',
copyTo: 'cachesDirectory',
})
setResult([pickerResult])
} catch (e) {
handleError(e)
}
}}
/>
<Button
title="open picker for multi file selection"
onPress={() => {
DocumentPicker.pick({ allowMultiSelection: true }).then(setResult).catch(handleError)
}}
/>
<Button
title="open picker for multi selection of word files"
onPress={() => {
DocumentPicker.pick({
allowMultiSelection: true,
type: [types.doc, types.docx],
})
.then(setResult)
.catch(handleError)
}}
/>
<Button
title="open picker for single selection of pdf file"
onPress={() => {
DocumentPicker.pick({
type: types.pdf,
})
.then(setResult)
.catch(handleError)
}}
/>
<Button
title="releaseSecureAccess"
onPress={() => {
DocumentPicker.releaseSecureAccess([])
.then(() => {
console.warn('releaseSecureAccess: success')
})
.catch(handleError)
}}
/>
<Button
title="open directory picker"
onPress={() => {
DocumentPicker.pickDirectory().then(setResult).catch(handleError)
}}
/>
<Text selectable>Result: {JSON.stringify(result, null, 2)}</Text>
</View>
)
}
const styles = StyleSheet.create({
container: {
flex: 1,
alignItems: 'center',
justifyContent: 'center',
backgroundColor: 'white',
},
box: {
width: 60,
height: 60,
marginVertical: 20,
},
})