@@ -13,7 +13,9 @@ class Dropzone extends Component {
13
13
} ;
14
14
15
15
this . validFiletypes = {
16
- image : _ ( 'jpeg/jpg, svg, png, gif, bmp, webp' ) ,
16
+ image : _ (
17
+ 'image/jpeg, image/jpg, image/svg, image/png, image/gif, image/bmp, image/webp'
18
+ ) ,
17
19
} ;
18
20
19
21
this . onDrop = this . onDrop . bind ( this ) ;
@@ -40,7 +42,7 @@ class Dropzone extends Component {
40
42
componentWillMount ( ) {
41
43
const _ = this . props . localize ;
42
44
43
- if ( this . props . fileType && this . props . value && this . props . value !== '' ) {
45
+ if ( this . props . value && this . props . value !== '' ) {
44
46
this . setState ( { content : this . renderSuccess ( this . props . value ) } ) ;
45
47
return ;
46
48
}
@@ -50,7 +52,7 @@ class Dropzone extends Component {
50
52
< div className = "dropzone-container__message" >
51
53
< p >
52
54
{ _ ( 'Drop the ' ) +
53
- ( this . props . fileType ? this . props . fileType : _ ( 'file' ) ) +
55
+ this . props . fileType +
54
56
_ (
55
57
' to upload here or click to choose a file from your computer.'
56
58
) }
@@ -59,7 +61,9 @@ class Dropzone extends Component {
59
61
{ this . props . fileType === 'image' ? (
60
62
< p >
61
63
{ _ ( 'Supported formats are: ' ) +
62
- this . validFiletypes [ this . props . fileType ] +
64
+ this . validFiletypes [ this . props . fileType ]
65
+ . split ( 'image/' )
66
+ . join ( '' ) +
63
67
'.' }
64
68
</ p >
65
69
) : null }
@@ -70,15 +74,18 @@ class Dropzone extends Component {
70
74
71
75
onLoad ( e ) {
72
76
const _ = this . props . localize ;
77
+ const supportedFileTypes =
78
+ this . props . fileType === 'image'
79
+ ? this . validFiletypes [ this . props . fileType ] . split ( 'image/' ) . join ( '' )
80
+ : this . validFiletypes [ this . props . fileType ] ;
81
+
73
82
const parsingError = (
74
83
< div className = "dropzone-container__message" >
75
84
< p > { _ ( 'Yikes! An error occurred while parsing this file.' ) } </ p >
76
85
< p >
77
- { this . props . fileType
78
- ? _ ( 'Try again with a supported file format: ' ) +
79
- this . validFiletypes [ this . props . fileType ] +
80
- '.'
81
- : _ ( 'Try again.' ) }
86
+ { _ ( 'Try again with a supported file format: ' ) +
87
+ supportedFileTypes +
88
+ '.' }
82
89
</ p >
83
90
</ div >
84
91
) ;
@@ -98,69 +105,62 @@ class Dropzone extends Component {
98
105
}
99
106
}
100
107
101
- onDrop ( file ) {
108
+ onDrop ( accepted , rejected ) {
102
109
const _ = this . props . localize ;
103
110
const reader = new FileReader ( ) ;
104
111
105
- const invalidFileTypeMessage = this . props . fileType ? (
106
- < div className = "dropzone-container__message" >
107
- < p >
108
- { _ ( "Yikes! This doesn't look like a valid " ) +
109
- this . props . fileType +
110
- _ ( 'to us. ' ) }
111
- </ p >
112
- < p >
113
- { _ ( 'Try again with a ' ) +
114
- this . validFiletypes [ this . props . fileType ] +
115
- '.' }
116
- </ p >
117
- </ div >
118
- ) : (
119
- _ ( 'Unsupported file format!' )
120
- ) ;
112
+ if ( accepted . length ) {
113
+ if ( accepted . length > 1 ) {
114
+ this . setState ( {
115
+ content : (
116
+ < div className = "dropzone-container__message" >
117
+ < p > { _ ( 'Yikes! You can only upload one file at a time.' ) } </ p >
118
+ < p >
119
+ { _ (
120
+ 'To upload multiple files, create multiple files and upload them individually.'
121
+ ) }
122
+ </ p >
123
+ </ div >
124
+ ) ,
125
+ } ) ;
126
+ return ;
127
+ }
128
+ this . setState ( { content : _ ( 'Loading...' ) } ) ;
129
+ reader . onload = e => this . onLoad ( e ) ;
130
+ if ( this . props . fileType === 'image' ) {
131
+ reader . readAsDataURL ( accepted [ 0 ] ) ;
132
+ }
133
+ }
134
+
135
+ if ( rejected . length ) {
136
+ const supportedFileTypes =
137
+ this . props . fileType === 'image'
138
+ ? this . validFiletypes [ this . props . fileType ] . split ( 'image/' ) . join ( '' )
139
+ : this . validFiletypes [ this . props . fileType ] ;
121
140
122
- if ( file . length > 1 ) {
123
141
this . setState ( {
124
142
content : (
125
143
< div className = "dropzone-container__message" >
126
- < p > { _ ( 'Yikes! You can only upload one file at a time.' ) } </ p >
127
144
< p >
128
- { _ (
129
- 'To upload multiple files, create multiple files and upload them individually.'
130
- ) }
145
+ { _ ( "Yikes! This doesn't look like a valid " ) +
146
+ this . props . fileType +
147
+ _ ( ' to us. ' ) }
131
148
</ p >
149
+ < p > { _ ( 'Try again with a ' ) + supportedFileTypes + ' file.' } </ p >
132
150
</ div >
133
151
) ,
134
152
} ) ;
135
- return ;
136
- }
137
-
138
- this . setState ( { content : _ ( 'Loading...' ) } ) ;
139
-
140
- reader . onload = e => this . onLoad ( e ) ;
141
-
142
- if ( this . props . fileType === 'image' ) {
143
- if (
144
- [ '.jpeg' , '.jpg' , '.svg' , '.png' , '.gif' , '.bmp' , '.webp' ] . some ( ext =>
145
- file [ 0 ] . name . endsWith ( ext )
146
- )
147
- ) {
148
- reader . readAsDataURL ( file [ 0 ] ) ;
149
- } else {
150
- this . setState ( {
151
- content : invalidFileTypeMessage ,
152
- } ) ;
153
- }
154
153
}
155
154
}
156
155
157
156
render ( ) {
158
157
return (
159
158
< Drop
160
- ref = "dzone"
159
+ accept = { this . validFiletypes [ this . props . fileType ] }
161
160
onDrop = { this . onDrop }
162
161
className = "dropzone-container"
163
162
activeClassName = "dropzone-container--active"
163
+ rejectClassName = "dropzone-container--rejected"
164
164
>
165
165
< div className = "dropzone-container__content" > { this . state . content } </ div >
166
166
</ Drop >
@@ -170,7 +170,7 @@ class Dropzone extends Component {
170
170
171
171
Dropzone . propTypes = {
172
172
localize : PropTypes . func ,
173
- fileType : PropTypes . string ,
173
+ fileType : PropTypes . string . isRequired ,
174
174
onUpdate : PropTypes . func ,
175
175
value : PropTypes . any ,
176
176
} ;
0 commit comments