index.js 4.5 KB

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