-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPreview.tsx
139 lines (128 loc) · 4.77 KB
/
Preview.tsx
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import React from 'react'
import PropTypes from 'prop-types'
import { formatBytes, formatDuration } from './utils'
import { IPreviewProps } from './Dropzone'
//@ts-ignore
import cancelImg from './assets/cancel.svg'
//@ts-ignore
import removeImg from './assets/remove.svg'
//@ts-ignore
import restartImg from './assets/restart.svg'
const iconByFn = {
cancel: { backgroundImage: `url(${cancelImg})` },
remove: { backgroundImage: `url(${removeImg})` },
restart: { backgroundImage: `url(${restartImg})` },
}
class Preview extends React.PureComponent<IPreviewProps> {
render() {
const {
className,
imageClassName,
style,
imageStyle,
fileWithMeta: { cancel, remove, restart },
meta: { name = '', percent = 0, size = 0, previewUrl, status, duration, validationError },
isUpload,
canCancel,
canRemove,
canRestart,
extra: { minSizeBytes },
} = this.props
let title = `${name || '?'}, ${formatBytes(size)}`
if (duration) title = `${title}, ${formatDuration(duration)}`
if (status === 'error_file_size' || status === 'error_validation') {
return (
<div className={className} style={style}>
<span className="dzu-previewFileNameError">{title}</span>
{status === 'error_file_size' && <span>{size < minSizeBytes ? 'File too small' : 'File too big'}</span>}
{status === 'error_validation' && <span>{String(validationError)}</span>}
{canRemove && <span className="dzu-previewButton" style={iconByFn.remove} onClick={remove} />}
</div>
)
}
if (status === 'error_upload_params' || status === 'exception_upload' || status === 'error_upload') {
title = `${title} (upload failed)`
}
if (status === 'aborted') title = `${title} (cancelled)`
return (
<div className={className} style={style}>
{previewUrl && <img className={imageClassName} style={imageStyle} src={previewUrl} alt={title} title={title} />}
{!previewUrl && <span className="dzu-previewFileName">{title}</span>}
<div className="dzu-previewStatusContainer">
{isUpload && (
<progress max={100} value={status === 'done' || status === 'headers_received' ? 100 : percent} />
)}
{status === 'uploading' && canCancel && (
<span className="dzu-previewButton" style={iconByFn.cancel} onClick={cancel} />
)}
{status !== 'preparing' && status !== 'getting_upload_params' && status !== 'uploading' && canRemove && (
<span className="dzu-previewButton" style={iconByFn.remove} onClick={remove} />
)}
{['error_upload_params', 'exception_upload', 'error_upload', 'aborted', 'ready'].includes(status) &&
canRestart && <span className="dzu-previewButton" style={iconByFn.restart} onClick={restart} />}
</div>
</div>
)
}
}
// @ts-ignore
Preview.propTypes = {
className: PropTypes.string,
imageClassName: PropTypes.string,
style: PropTypes.object,
imageStyle: PropTypes.object,
fileWithMeta: PropTypes.shape({
file: PropTypes.any.isRequired,
meta: PropTypes.object.isRequired,
cancel: PropTypes.func.isRequired,
restart: PropTypes.func.isRequired,
remove: PropTypes.func.isRequired,
xhr: PropTypes.any,
}).isRequired,
// copy of fileWithMeta.meta, won't be mutated
meta: PropTypes.shape({
status: PropTypes.oneOf([
'preparing',
'error_file_size',
'error_validation',
'ready',
'getting_upload_params',
'error_upload_params',
'uploading',
'exception_upload',
'aborted',
'error_upload',
'headers_received',
'done',
]).isRequired,
type: PropTypes.string.isRequired,
name: PropTypes.string,
uploadedDate: PropTypes.string.isRequired,
percent: PropTypes.number,
size: PropTypes.number,
lastModifiedDate: PropTypes.string,
previewUrl: PropTypes.string,
duration: PropTypes.number,
width: PropTypes.number,
height: PropTypes.number,
videoWidth: PropTypes.number,
videoHeight: PropTypes.number,
validationError: PropTypes.any,
}).isRequired,
isUpload: PropTypes.bool.isRequired,
canCancel: PropTypes.bool.isRequired,
canRemove: PropTypes.bool.isRequired,
canRestart: PropTypes.bool.isRequired,
files: PropTypes.arrayOf(PropTypes.any).isRequired, // eslint-disable-line react/no-unused-prop-types
extra: PropTypes.shape({
active: PropTypes.bool.isRequired,
reject: PropTypes.bool.isRequired,
dragged: PropTypes.arrayOf(PropTypes.any).isRequired,
accept: PropTypes.string.isRequired,
multiple: PropTypes.bool.isRequired,
minSizeBytes: PropTypes.number.isRequired,
maxSizeBytes: PropTypes.number.isRequired,
maxFiles: PropTypes.number.isRequired,
}).isRequired,
}
export default Preview