index.js 4.3 KB

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