FileList.js 2.9 KB

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