FileList.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. const FileItem = require('./FileItem/index.js')
  2. const VirtualList = require('./VirtualList')
  3. const classNames = require('classnames')
  4. const { h } = require('preact')
  5. function chunks (list, size) {
  6. const chunked = []
  7. let currentChunk = []
  8. list.forEach((item, i) => {
  9. if (currentChunk.length < size) {
  10. currentChunk.push(item)
  11. } else {
  12. chunked.push(currentChunk)
  13. currentChunk = [item]
  14. }
  15. })
  16. if (currentChunk.length) chunked.push(currentChunk)
  17. return chunked
  18. }
  19. module.exports = (props) => {
  20. const noFiles = props.totalFileCount === 0
  21. const dashboardFilesClass = classNames(
  22. 'uppy-Dashboard-files',
  23. { 'uppy-Dashboard-files--noFiles': noFiles }
  24. )
  25. // It's not great that this is hardcoded!
  26. // It's ESPECIALLY not great that this is checking against `itemsPerRow`!
  27. const rowHeight = props.itemsPerRow === 1
  28. // Mobile
  29. ? 71
  30. // 190px height + 2 * 5px margin
  31. : 200
  32. const fileProps = {
  33. // FIXME This is confusing, it's actually the Dashboard's plugin ID
  34. id: props.id,
  35. error: props.error,
  36. // TODO move this to context
  37. i18n: props.i18n,
  38. log: props.log,
  39. info: props.info,
  40. // features
  41. acquirers: props.acquirers,
  42. resumableUploads: props.resumableUploads,
  43. individualCancellation: props.individualCancellation,
  44. // visual options
  45. hideRetryButton: props.hideRetryButton,
  46. hidePauseResumeButton: props.hidePauseResumeButton,
  47. hideCancelButton: props.hideCancelButton,
  48. showLinkToFileUploadResult: props.showLinkToFileUploadResult,
  49. showRemoveButtonAfterComplete: props.showRemoveButtonAfterComplete,
  50. isWide: props.isWide,
  51. metaFields: props.metaFields,
  52. // callbacks
  53. retryUpload: props.retryUpload,
  54. pauseUpload: props.pauseUpload,
  55. cancelUpload: props.cancelUpload,
  56. toggleFileCard: props.toggleFileCard,
  57. removeFile: props.removeFile,
  58. handleRequestThumbnail: props.handleRequestThumbnail,
  59. handleCancelThumbnail: props.handleCancelThumbnail
  60. }
  61. const rows = chunks(Object.keys(props.files), props.itemsPerRow)
  62. function renderRow (row) {
  63. return (
  64. // The `role="presentation` attribute ensures that the list items are properly associated with the `VirtualList` element
  65. // We use the first file ID as the key—this should not change across scroll rerenders
  66. <div role="presentation" key={row[0]}>
  67. {row.map((fileID) => (
  68. <FileItem
  69. key={fileID}
  70. {...fileProps}
  71. role="listitem"
  72. openFileEditor={props.openFileEditor}
  73. canEditFile={props.canEditFile}
  74. file={props.files[fileID]}
  75. />
  76. ))}
  77. </div>
  78. )
  79. }
  80. return (
  81. <VirtualList
  82. class={dashboardFilesClass}
  83. role="list"
  84. data={rows}
  85. renderRow={renderRow}
  86. rowHeight={rowHeight}
  87. />
  88. )
  89. }