-
Notifications
You must be signed in to change notification settings - Fork 2k
/
Copy pathFileList.jsx
100 lines (92 loc) · 3.04 KB
/
FileList.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import classNames from 'classnames'
import { h } from 'preact'
import FileItem from './FileItem/index.jsx'
import VirtualList from './VirtualList.jsx'
function chunks (list, size) {
const chunked = []
let currentChunk = []
list.forEach((item) => {
if (currentChunk.length < size) {
currentChunk.push(item)
} else {
chunked.push(currentChunk)
currentChunk = [item]
}
})
if (currentChunk.length) chunked.push(currentChunk)
return chunked
}
export default (props) => {
const noFiles = props.totalFileCount === 0
const dashboardFilesClass = classNames(
'uppy-Dashboard-files',
{ 'uppy-Dashboard-files--noFiles': noFiles },
)
// It's not great that this is hardcoded!
// It's ESPECIALLY not great that this is checking against `itemsPerRow`!
const rowHeight = props.itemsPerRow === 1
// Mobile
? 71
// 190px height + 2 * 5px margin
: 200
const fileProps = {
// FIXME This is confusing, it's actually the Dashboard's plugin ID
id: props.id,
error: props.error,
// TODO move this to context
i18n: props.i18n,
uppy: props.uppy,
// features
acquirers: props.acquirers,
resumableUploads: props.resumableUploads,
individualCancellation: props.individualCancellation,
// visual options
hideRetryButton: props.hideRetryButton,
hidePauseResumeButton: props.hidePauseResumeButton,
hideCancelButton: props.hideCancelButton,
showLinkToFileUploadResult: props.showLinkToFileUploadResult,
showRemoveButtonAfterComplete: props.showRemoveButtonAfterComplete,
isWide: props.isWide,
metaFields: props.metaFields,
recoveredState: props.recoveredState,
// callbacks
toggleFileCard: props.toggleFileCard,
handleRequestThumbnail: props.handleRequestThumbnail,
handleCancelThumbnail: props.handleCancelThumbnail,
}
const sortByGhostComesFirst = (file1, file2) => {
return props.files[file2].isGhost - props.files[file1].isGhost
}
// Sort files by file.isGhost, ghost files first, only if recoveredState is present
const files = Object.keys(props.files)
if (props.recoveredState) files.sort(sortByGhostComesFirst)
const rows = chunks(files, props.itemsPerRow)
const renderRow = (row) => (
// The `role="presentation` attribute ensures that the list items are properly
// associated with the `VirtualList` element.
// We use the first file ID as the key—this should not change across scroll rerenders
<div role="presentation" key={row[0]}>
{row.map((fileID) => (
<FileItem
key={fileID}
uppy={props.uppy}
{...fileProps} // eslint-disable-line react/jsx-props-no-spreading
role="listitem"
openFileEditor={props.openFileEditor}
canEditFile={props.canEditFile}
toggleAddFilesPanel={props.toggleAddFilesPanel}
file={props.files[fileID]}
/>
))}
</div>
)
return (
<VirtualList
class={dashboardFilesClass}
role="list"
data={rows}
renderRow={renderRow}
rowHeight={rowHeight}
/>
)
}