index.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. import React from 'react'
  2. import {
  3. StyleSheet,
  4. Modal,
  5. Text,
  6. ScrollView,
  7. TouchableOpacity } from 'react-native'
  8. import takePicture from './takePicture'
  9. import selectImage from './selectImage'
  10. import selectDocument from './selectDocument'
  11. import Provider from './provider'
  12. export default class UppyReactNativeFilePicker extends React.Component {
  13. constructor () {
  14. super()
  15. this.state = {
  16. providers: [
  17. { id: 'LocalImages', title: 'Pick Local Images/Videos' },
  18. { id: 'LocalDocuments', title: 'Pick Documents' },
  19. { id: 'LocalCamera', title: 'Take a Picture' },
  20. { id: 'Url', title: 'Url' }
  21. // { id: 'GoogleDrive', title: 'Google Drive' },
  22. // { id: 'Instagram', title: 'Instagram' }
  23. ],
  24. openProvider: null
  25. }
  26. this.takePicture = this.takePicture.bind(this)
  27. this.selectImage = this.selectImage.bind(this)
  28. this.selectDocument = this.selectDocument.bind(this)
  29. }
  30. componentDidMount () {
  31. this.uppy = this.props.uppy
  32. }
  33. takePicture () {
  34. takePicture().then((file) => {
  35. this.uppy.addFile({
  36. source: 'React Native',
  37. name: `media_${Date.now()}.jpg`,
  38. type: file.type,
  39. data: file
  40. })
  41. this.props.onRequestClose()
  42. })
  43. .catch((err) => {
  44. console.log(err)
  45. })
  46. }
  47. selectImage () {
  48. selectImage({ exif: true }).then((file) => {
  49. this.uppy.addFile({
  50. source: 'React Native',
  51. name: `media_${Date.now()}.jpg`,
  52. type: file.type,
  53. data: file
  54. })
  55. this.props.onRequestClose()
  56. })
  57. .catch((err) => {
  58. console.log(err)
  59. })
  60. }
  61. selectDocument () {
  62. selectDocument().then((file) => {
  63. this.uppy.addFile({
  64. source: 'React Native',
  65. name: file.name,
  66. data: file
  67. })
  68. this.props.onRequestClose()
  69. })
  70. .catch((err) => {
  71. console.log(err)
  72. })
  73. }
  74. openProvider (id) {
  75. console.log('Open provider:', id)
  76. this.setState({
  77. openProvider: id
  78. })
  79. }
  80. chooseProvider (id) {
  81. console.log('Provider selected:', id)
  82. switch (id) {
  83. case 'LocalImages':
  84. this.selectImage()
  85. return
  86. case 'LocalDocuments':
  87. this.selectDocument()
  88. return
  89. case 'LocalCamera':
  90. this.takePicture()
  91. return
  92. default:
  93. this.openProvider(id)
  94. }
  95. }
  96. renderSourceList () {
  97. return (
  98. <ScrollView
  99. contentContainerStyle={styles.providerList}>
  100. {this.state.providers.map((item, index) => {
  101. return (
  102. <TouchableOpacity
  103. style={styles.providerButton}
  104. key={index}
  105. onPress={ev => this.chooseProvider(item.id)}>
  106. <Text style={styles.providerButtonText}>{item.title}</Text>
  107. </TouchableOpacity>
  108. )
  109. })}
  110. <TouchableOpacity
  111. style={styles.cancelButton}
  112. onPress={ev => this.props.onRequestClose()}>
  113. <Text style={styles.cancelButtonText}>Cancel</Text>
  114. </TouchableOpacity>
  115. </ScrollView>
  116. )
  117. }
  118. render () {
  119. return (
  120. <Modal
  121. animationType="slide"
  122. transparent={false}
  123. visible={this.props.show}
  124. supportedOrientations={['portrait', 'portrait-upside-down', 'landscape', 'landscape-left', 'landscape-right']}
  125. onRequestClose={this.props.onRequestClose}>
  126. {this.state.openProvider
  127. ? <Provider
  128. providerID={this.state.openProvider}
  129. uppy={this.uppy}
  130. onDone={() => {
  131. this.setState({
  132. openProvider: null
  133. })
  134. this.props.onRequestClose()
  135. }}
  136. {...this.props} />
  137. : this.renderSourceList()
  138. }
  139. </Modal>
  140. )
  141. }
  142. }
  143. const styles = StyleSheet.create({
  144. providerList: {
  145. flex: 1,
  146. marginTop: 22,
  147. justifyContent: 'center'
  148. },
  149. providerButton: {
  150. alignItems: 'center',
  151. backgroundColor: '#0077cc',
  152. marginBottom: 15,
  153. marginLeft: 50,
  154. marginRight: 50,
  155. padding: 10,
  156. borderRadius: 5
  157. },
  158. providerButtonText: {
  159. color: '#fff'
  160. },
  161. cancelButton: {
  162. alignItems: 'center',
  163. borderWidth: 1,
  164. borderColor: '#0077cc',
  165. marginBottom: 15,
  166. marginLeft: 50,
  167. marginRight: 50,
  168. padding: 10,
  169. borderRadius: 5
  170. },
  171. cancelButtonText: {
  172. color: '#0077cc'
  173. }
  174. })