index.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. import React from 'react'
  2. import Expo from 'expo'
  3. import {
  4. // StyleSheet,
  5. Modal,
  6. Text,
  7. // View,
  8. ScrollView,
  9. // Button,
  10. // Image
  11. TouchableOpacity } from 'react-native'
  12. import takePicture from './takePicture'
  13. import selectImage from './selectImage'
  14. import selectDocument from './selectDocument'
  15. export default class UppyReactNativeFilePicker extends React.Component {
  16. constructor () {
  17. super()
  18. this.state = {
  19. providers: [
  20. { id: 'LocalImages', title: 'Pick Local Images/Videos' },
  21. { id: 'LocalDocuments', title: 'Pick Documents' },
  22. { id: 'LocalCamera', title: 'Take a Picture' },
  23. { id: 'GoogleDrive', title: 'Google Drive' },
  24. { id: 'Instagram', title: 'Instagram' }
  25. ],
  26. openProvider: null
  27. }
  28. this.takePicture = this.takePicture.bind(this)
  29. this.selectImage = this.selectImage.bind(this)
  30. this.selectDocument = this.selectDocument.bind(this)
  31. }
  32. componentDidMount () {
  33. this.uppy = this.props.uppy
  34. }
  35. takePicture () {
  36. takePicture().then((file) => {
  37. this.uppy.addFile({
  38. source: 'React Native',
  39. name: `media_${Date.now()}.jpg`,
  40. type: file.type,
  41. data: file
  42. })
  43. this.props.onRequestClose()
  44. })
  45. }
  46. selectImage () {
  47. selectImage().then((file) => {
  48. this.uppy.addFile({
  49. source: 'React Native',
  50. name: `media_${Date.now()}.jpg`,
  51. type: file.type,
  52. data: file
  53. })
  54. this.uppy.upload()
  55. this.props.onRequestClose()
  56. })
  57. }
  58. selectDocument () {
  59. selectDocument().then((file) => {
  60. this.uppy.addFile({
  61. source: 'React Native',
  62. name: `media_${Date.now()}.jpg`,
  63. type: file.type,
  64. data: file
  65. })
  66. this.props.onRequestClose()
  67. })
  68. }
  69. openProvider (id) {
  70. console.log('OPEN PROVIDER:', id)
  71. }
  72. chooseProvider (id) {
  73. console.log('PROVIDER SELECTED:', id)
  74. switch (id) {
  75. case 'LocalImages':
  76. this.selectImage()
  77. return
  78. case 'LocalDocuments':
  79. this.selectDocument()
  80. return
  81. case 'LocalCamera':
  82. this.takePicture()
  83. return
  84. default:
  85. this.openProvider(id)
  86. }
  87. }
  88. render () {
  89. if (this.state.openProvider) {
  90. return 'show provider'
  91. }
  92. return (
  93. <Modal
  94. animationType="slide"
  95. transparent={false}
  96. visible={this.props.show}
  97. onRequestClose={() => {
  98. Expo.Alert.alert('Modal has been closed.')
  99. this.props.onRequestClose()
  100. }}>
  101. <ScrollView
  102. contentContainerStyle={{
  103. flex: '1',
  104. marginTop: 22,
  105. justifyContent: 'center'
  106. }}>
  107. <TouchableOpacity
  108. style={{
  109. alignItems: 'center',
  110. backgroundColor: '#DDDDDD',
  111. marginBottom: 10,
  112. padding: 10
  113. }}
  114. onPress={ev => this.props.onRequestClose()}>
  115. <Text>Close</Text>
  116. </TouchableOpacity>
  117. {this.state.providers.map((item, index) => {
  118. return (
  119. <TouchableOpacity
  120. style={{
  121. alignItems: 'center',
  122. backgroundColor: '#DDDDDD',
  123. marginBottom: 10,
  124. padding: 10
  125. }}
  126. key={index}
  127. onPress={ev => this.chooseProvider(item.id)}>
  128. <Text>{item.title}</Text>
  129. </TouchableOpacity>
  130. )
  131. })}
  132. </ScrollView>
  133. </Modal>
  134. )
  135. }
  136. }