FileList.js 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. recoveredState: props.recoveredState,
  53. // callbacks
  54. retryUpload: props.retryUpload,
  55. pauseUpload: props.pauseUpload,
  56. cancelUpload: props.cancelUpload,
  57. toggleFileCard: props.toggleFileCard,
  58. removeFile: props.removeFile,
  59. handleRequestThumbnail: props.handleRequestThumbnail,
  60. handleCancelThumbnail: props.handleCancelThumbnail,
  61. }
  62. const rows = chunks(Object.keys(props.files), props.itemsPerRow)
  63. function renderRow (row) {
  64. return (
  65. // The `role="presentation` attribute ensures that the list items are properly associated with the `VirtualList` element
  66. // We use the first file ID as the key—this should not change across scroll rerenders
  67. <div role="presentation" key={row[0]}>
  68. {row.map((fileID) => (
  69. <FileItem
  70. key={fileID}
  71. {...fileProps}
  72. role="listitem"
  73. openFileEditor={props.openFileEditor}
  74. canEditFile={props.canEditFile}
  75. file={props.files[fileID]}
  76. />
  77. ))}
  78. </div>
  79. )
  80. }
  81. return (
  82. <VirtualList
  83. class={dashboardFilesClass}
  84. role="list"
  85. data={rows}
  86. renderRow={renderRow}
  87. rowHeight={rowHeight}
  88. />
  89. )
  90. }