index.js 4.4 KB

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