Skip to content
This repository was archived by the owner on Jun 5, 2022. It is now read-only.

Commit f451946

Browse files
authored
Merge pull request #34 from donalmurtagh/master
Fixes #40 #37 #39 #36 #35 #33 #32 #31 #30 #4
2 parents 57a1f1e + efe2138 commit f451946

File tree

4 files changed

+60
-33
lines changed

4 files changed

+60
-33
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ class App extends React.Component {
2424
this.onDrop = this.onDrop.bind(this);
2525
}
2626

27-
onDrop(picture) {
27+
onDrop(pictureFiles, pictureDataURLs) {
2828
this.setState({
29-
pictures: this.state.pictures.concat(picture),
29+
pictures: this.state.pictures.concat(pictureFiles),
3030
});
3131
}
3232

@@ -57,6 +57,7 @@ class App extends React.Component {
5757
| name | String | - | Input name. |
5858
| withIcon | Boolean | true | If true, show upload icon on top |
5959
| buttonText | String | 'Choose images' | The text that display in the button. |
60+
| buttonType | String | 'submit' | The value of the button's "type" attribute. |
6061
| withLabel | Boolean | true | Show instruction label |
6162
| label | String | 'Max file size: 5mb, accepted: jpg|gif|png|gif' | Label text |
6263
| labelStyles | Object | - | Inline styles for the label. |

src/App.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ class App extends React.Component {
1616
this.onDrop = this.onDrop.bind(this);
1717
}
1818
19-
onDrop(picture) {
19+
onDrop(pictureFiles, pictureDataURLs) {
2020
this.setState({
21-
pictures: this.state.pictures.concat(picture),
21+
pictures: this.state.pictures.concat(pictureFiles),
2222
});
2323
}
2424
@@ -131,6 +131,12 @@ export default class App extends React.PureComponent {
131131
<td className="text-left">'Choose images' </td>
132132
<td className="text-left">The text that display in the button.</td>
133133
</tr>
134+
<tr>
135+
<td className="text-left">buttonType</td>
136+
<td className="text-left">String</td>
137+
<td className="text-left">'submit' </td>
138+
<td className="text-left">The value of the button's type attribute</td>
139+
</tr>
134140
<tr>
135141
<td className="text-left">withLabel</td>
136142
<td className="text-left">Boolean</td>

src/component/index.css

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -91,29 +91,30 @@
9191
display: flex;
9292
align-items: center;
9393
justify-content: center;
94-
height: 90px;
94+
height: inherit;
9595
box-shadow: 0 0 8px 2px rgba(0, 0, 0, 0.1);
9696
border: 1px solid #d0dbe4;
9797
position: relative;
9898
}
9999

100100
.fileContainer .uploadPictureContainer img.uploadPicture {
101-
width: 40%;
101+
width: 100%;
102102
}
103103

104104
.fileContainer .deleteImage {
105105
position: absolute;
106106
top: -9px;
107107
right: -9px;
108-
font-size: 8px;
109108
color: #fff;
110109
background: #ff4081;
111110
border-radius: 50%;
112-
width: 15px;
113-
height: 15px;
114111
text-align: center;
115-
line-height: 15px;
116112
cursor: pointer;
113+
font-size: 26px;
114+
font-weight: bold;
115+
line-height: 30px;
116+
width: 30px;
117+
height: 30px;
117118
}
118119

119120
.flipMove {

src/component/index.js

Lines changed: 42 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -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

195211
ReactImageUploadComponent.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

218236
ReactImageUploadComponent.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

Comments
 (0)