FileList.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. import React from 'react' // eslint-disable-line no-unused-vars
  2. import { StyleSheet, View, FlatList, Text, Image } from 'react-native'
  3. import getFileTypeIcon from '@uppy/dashboard/lib/utils/getFileTypeIcon.js'
  4. import truncateString from '@uppy/dashboard/lib/utils/truncateString.js'
  5. import renderStringFromJSX from 'preact-render-to-string'
  6. import SvgUri from 'react-native-svg-uri'
  7. // function truncateString (str) {
  8. // const maxChars = 20
  9. // if (str.length > maxChars) {
  10. // return str.substring(0, 25) + '...'
  11. // }
  12. // return str
  13. // }
  14. function FileIcon () {
  15. return <View style={styles.itemIconContainer}>
  16. <Image
  17. style={styles.itemIcon}
  18. source={require('./assets/file-icon.png')}
  19. />
  20. </View>
  21. }
  22. function UppyDashboardFileIcon (props) {
  23. const icon = renderStringFromJSX(getFileTypeIcon(props.type).icon)
  24. if (!icon) {
  25. return <FileIcon />
  26. }
  27. const color = getFileTypeIcon(props.type).color
  28. return (
  29. <View style={{
  30. ...styles.itemIconContainer,
  31. backgroundColor: color
  32. }}>
  33. <SvgUri
  34. width={50}
  35. height={50}
  36. style={styles.itemIconSVG}
  37. fill="#ffffff"
  38. fillAll
  39. svgXmlData={icon}
  40. />
  41. </View>
  42. )
  43. }
  44. export default function FileList (props) {
  45. const uppyFiles = props.uppy.state.files
  46. const uppyFilesArray = Object.keys(uppyFiles).map((id) => uppyFiles[id])
  47. return (
  48. <View style={styles.container}>
  49. <FlatList
  50. data={uppyFilesArray}
  51. keyExtractor={(item, index) => item.id}
  52. numColumns={2}
  53. renderItem={({item}) => {
  54. return (
  55. <View style={styles.item}>
  56. {item.type === 'image'
  57. ? <Image
  58. style={styles.itemImage}
  59. source={{ uri: item.data.uri }} />
  60. : <UppyDashboardFileIcon type={item.type} />
  61. }
  62. <Text style={styles.itemName}>{truncateString(item.name, 20)}</Text>
  63. <Text style={styles.itemType}>{item.type}</Text>
  64. </View>
  65. )
  66. }}
  67. />
  68. </View>
  69. )
  70. }
  71. const styles = StyleSheet.create({
  72. container: {
  73. marginTop: 20,
  74. marginBottom: 20,
  75. flex: 1,
  76. justifyContent: 'center'
  77. },
  78. item: {
  79. width: 100,
  80. marginTop: 5,
  81. marginBottom: 15,
  82. marginRight: 25
  83. },
  84. itemImage: {
  85. width: 100,
  86. height: 100,
  87. borderRadius: 5,
  88. marginBottom: 5
  89. },
  90. itemIconContainer: {
  91. width: 100,
  92. height: 100,
  93. borderRadius: 5,
  94. marginBottom: 5,
  95. backgroundColor: '#cfd3d6',
  96. alignItems: 'center',
  97. justifyContent: 'center'
  98. },
  99. itemIcon: {
  100. width: 42,
  101. height: 56
  102. },
  103. itemIconSVG: {
  104. width: 50,
  105. height: 50
  106. },
  107. itemName: {
  108. fontSize: 13,
  109. color: '#2c3e50',
  110. fontWeight: '600'
  111. },
  112. itemType: {
  113. fontWeight: '600',
  114. fontSize: 12,
  115. color: '#95a5a6'
  116. }
  117. })