index.js 4.3 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'
  10. import selectImage from './selectImage'
  11. import selectDocument from './selectDocument'
  12. import Provider from './provider'
  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. 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. }).catch((err) => {
  57. console.log(err)
  58. })
  59. }
  60. selectDocument () {
  61. selectDocument().then((file) => {
  62. this.uppy.addFile({
  63. source: 'React Native',
  64. name: file.name,
  65. data: file,
  66. })
  67. this.props.onRequestClose()
  68. }).catch((err) => {
  69. console.log(err)
  70. })
  71. }
  72. openProvider (id) {
  73. console.log('Open provider:', id)
  74. this.setState({
  75. openProvider: id,
  76. })
  77. }
  78. chooseProvider (id) {
  79. console.log('Provider selected:', id)
  80. switch (id) {
  81. case 'LocalImages':
  82. this.selectImage()
  83. return
  84. case 'LocalDocuments':
  85. this.selectDocument()
  86. return
  87. case 'LocalCamera':
  88. this.takePicture()
  89. return
  90. default:
  91. this.openProvider(id)
  92. }
  93. }
  94. renderSourceList () {
  95. return (
  96. <ScrollView
  97. contentContainerStyle={styles.providerList}
  98. >
  99. {this.state.providers.map((item, index) => {
  100. return (
  101. <TouchableOpacity
  102. style={styles.providerButton}
  103. key={index}
  104. onPress={ev => this.chooseProvider(item.id)}
  105. >
  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. >
  114. <Text style={styles.cancelButtonText}>Cancel</Text>
  115. </TouchableOpacity>
  116. </ScrollView>
  117. )
  118. }
  119. render () {
  120. return (
  121. <Modal
  122. animationType="slide"
  123. transparent={false}
  124. visible={this.props.show}
  125. supportedOrientations={['portrait', 'portrait-upside-down', 'landscape', 'landscape-left', 'landscape-right']}
  126. onRequestClose={this.props.onRequestClose}
  127. >
  128. {this.state.openProvider ? (
  129. <Provider
  130. providerID={this.state.openProvider}
  131. uppy={this.uppy}
  132. onDone={() => {
  133. this.setState({
  134. openProvider: null,
  135. })
  136. this.props.onRequestClose()
  137. }}
  138. {...this.props}
  139. />
  140. ) : (
  141. this.renderSourceList()
  142. )}
  143. </Modal>
  144. )
  145. }
  146. }
  147. const styles = StyleSheet.create({
  148. providerList: {
  149. flex: 1,
  150. marginTop: 22,
  151. justifyContent: 'center',
  152. },
  153. providerButton: {
  154. alignItems: 'center',
  155. backgroundColor: '#0077cc',
  156. marginBottom: 15,
  157. marginLeft: 50,
  158. marginRight: 50,
  159. padding: 10,
  160. borderRadius: 5,
  161. },
  162. providerButtonText: {
  163. color: '#fff',
  164. },
  165. cancelButton: {
  166. alignItems: 'center',
  167. borderWidth: 1,
  168. borderColor: '#0077cc',
  169. marginBottom: 15,
  170. marginLeft: 50,
  171. marginRight: 50,
  172. padding: 10,
  173. borderRadius: 5,
  174. },
  175. cancelButtonText: {
  176. color: '#0077cc',
  177. },
  178. })