@@ -17,7 +17,8 @@ class ReactImageUploadComponent extends React.Component {
1717 super ( props ) ;
1818 this . state = {
1919 pictures : [ ] ,
20- notAcceptedFileType : [ ] ,
20+ files : [ ] ,
21+ notAcceptedFileType : [ ] ,
2122 notAcceptedFileSize : [ ]
2223 } ;
2324 this . inputElement = '' ;
@@ -38,10 +39,7 @@ class ReactImageUploadComponent extends React.Component {
3839 onDropFile ( e ) {
3940 const files = e . target . files ;
4041 const _this = this ;
41- // If callback giving, fire.
42- if ( typeof this . props . onChange === "function" ) {
43- this . props . onChange ( files ) ;
44- }
42+
4543 // Iterate over all uploaded files
4644 for ( let i = 0 ; i < files . length ; i ++ ) {
4745 let f = files [ i ] ;
@@ -64,14 +62,25 @@ class ReactImageUploadComponent extends React.Component {
6462 // Read the image via FileReader API and save image result in state.
6563 reader . onload = ( function ( ) {
6664 return function ( e ) {
67- if ( _this . props . singleImage === true ) {
68- _this . setState ( { pictures : [ e . target . result ] } ) ;
69- }
70- else if ( _this . state . pictures . indexOf ( e . target . result ) === - 1 ) {
71- const newArray = _this . state . pictures . slice ( ) ;
72- newArray . push ( e . target . result ) ;
73- _this . setState ( { pictures : newArray } ) ;
74- }
65+ // Add the file name to the data URL
66+ let dataURL = e . target . result ;
67+ dataURL = dataURL . replace ( ";base64" , `;name=${ f . name } ;base64` ) ;
68+
69+ if ( _this . props . singleImage === true ) {
70+ _this . setState ( { pictures : [ dataURL ] , files : [ f ] } , ( ) => {
71+ _this . props . onChange ( _this . state . files , _this . state . pictures ) ;
72+ } ) ;
73+ } else if ( _this . state . pictures . indexOf ( dataURL ) === - 1 ) {
74+ const newArray = _this . state . pictures . slice ( ) ;
75+ newArray . push ( dataURL ) ;
76+
77+ const newFiles = _this . state . files . slice ( ) ;
78+ newFiles . push ( f ) ;
79+
80+ _this . setState ( { pictures : newArray , files : newFiles } , ( ) => {
81+ _this . props . onChange ( _this . state . files , _this . state . pictures ) ;
82+ } ) ;
83+ }
7584 } ;
7685 } ) ( f ) ;
7786 reader . readAsDataURL ( f ) ;
@@ -100,15 +109,21 @@ class ReactImageUploadComponent extends React.Component {
100109 Check file extension (onDropFile)
101110 */
102111 hasExtension ( fileName ) {
103- return ( new RegExp ( '(' + this . props . imgExtension . join ( '|' ) . replace ( / \. / g, '\\.' ) + ')$' ) ) . test ( fileName ) ;
112+ const pattern = '(' + this . props . imgExtension . join ( '|' ) . replace ( / \. / g, '\\.' ) + ')$' ;
113+ return new RegExp ( pattern , 'i' ) . test ( fileName ) ;
104114 }
105115
106116 /*
107117 Remove the image from state
108118 */
109119 removeImage ( picture ) {
110- const filteredAry = this . state . pictures . filter ( ( e ) => e !== picture ) ;
111- this . setState ( { pictures : filteredAry } )
120+ const removeIndex = this . state . pictures . findIndex ( e => e === picture ) ;
121+ const filteredPictures = this . state . pictures . filter ( ( e , index ) => index !== removeIndex ) ;
122+ const filteredFiles = this . state . files . filter ( ( e , index ) => index !== removeIndex ) ;
123+
124+ this . setState ( { pictures : filteredPictures , files : filteredFiles } , ( ) => {
125+ this . props . onChange ( this . state . files , this . state . pictures ) ;
126+ } ) ;
112127 }
113128
114129 /*
@@ -119,7 +134,7 @@ class ReactImageUploadComponent extends React.Component {
119134 if ( this . state . notAcceptedFileType . length > 0 ) {
120135 notAccepted = this . state . notAcceptedFileType . map ( ( error , index ) => {
121136 return (
122- < div className = { 'errorMessage' + this . props . errorClass } key = { index } style = { this . props . errorStyle } >
137+ < div className = { 'errorMessage ' + this . props . errorClass } key = { index } style = { this . props . errorStyle } >
123138 * { error } { this . props . fileTypeError }
124139 </ div >
125140 )
@@ -128,7 +143,7 @@ class ReactImageUploadComponent extends React.Component {
128143 if ( this . state . notAcceptedFileSize . length > 0 ) {
129144 notAccepted = this . state . notAcceptedFileSize . map ( ( error , index ) => {
130145 return (
131- < div className = { 'errorMessage' + this . props . errorClass } key = { index } style = { this . props . errorStyle } >
146+ < div className = { 'errorMessage ' + this . props . errorClass } key = { index } style = { this . props . errorStyle } >
132147 * { error } { this . props . fileSizeError }
133148 </ div >
134149 )
@@ -171,6 +186,7 @@ class ReactImageUploadComponent extends React.Component {
171186 { this . renderErrors ( ) }
172187 </ div >
173188 < button
189+ type = { this . props . buttonType }
174190 className = { "chooseFileButton " + this . props . buttonClassName }
175191 style = { this . props . buttonStyles }
176192 onClick = { this . triggerFileUpload }
@@ -194,34 +210,37 @@ class ReactImageUploadComponent extends React.Component {
194210
195211ReactImageUploadComponent . defaultProps = {
196212 className : '' ,
197- buttonClassName : { } ,
213+ buttonClassName : "" ,
198214 buttonStyles : { } ,
199215 withPreview : false ,
200- accept : "accept= image/*" ,
216+ accept : "image/*" ,
201217 name : "" ,
202218 withIcon : true ,
203219 buttonText : "Choose images" ,
220+ buttonType : "submit" ,
204221 withLabel : true ,
205222 label : "Max file size: 5mb, accepted: jpg|gif|png" ,
206223 labelStyles : { } ,
207224 labelClass : "" ,
208225 imgExtension : [ '.jpg' , '.gif' , '.png' ] ,
209226 maxFileSize : 5242880 ,
210227 fileSizeError : " file size is too big" ,
211- fileTypeError : " is not supported file extension" ,
228+ fileTypeError : " is not a supported file extension" ,
212229 errorClass : "" ,
213230 style : { } ,
214231 errorStyle : { } ,
215- singleImage : false
232+ singleImage : false ,
233+ onChange : ( ) => { }
216234} ;
217235
218236ReactImageUploadComponent . propTypes = {
219237 style : PropTypes . object ,
220238 className : PropTypes . string ,
221239 onChange : PropTypes . func ,
222240 onDelete : PropTypes . func ,
223- buttonClassName : PropTypes . object ,
241+ buttonClassName : PropTypes . string ,
224242 buttonStyles : PropTypes . object ,
243+ buttonType : PropTypes . string ,
225244 withPreview : PropTypes . bool ,
226245 accept : PropTypes . string ,
227246 name : PropTypes . string ,
0 commit comments