Skip to content

Commit 105d2eb

Browse files
author
Nitish Agrahari
committed
feat: image uploader added
1 parent bde877f commit 105d2eb

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

src/components/AddForm.tsx

+23-3
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ const AddForm = () => {
117117
const closeModalHandler = () => {
118118
setPreview(!preview);
119119
}
120-
120+
121121
// reset the form
122122
const handleReset = () => {
123123
setName('');
@@ -132,6 +132,23 @@ const AddForm = () => {
132132
setCheck6(false);
133133
}
134134

135+
// profile image upload handler
136+
const uploadedImage = React.useRef(null);
137+
const imageUploader = React.useRef(null);
138+
139+
const handleImageUpload = (e: ChangeEvent<HTMLInputElement>) => {
140+
const file = e.target.files[0];
141+
setProfileImage(file.name);
142+
if (file) {
143+
const reader = new FileReader();
144+
const { current } = uploadedImage;
145+
current.file = file;
146+
reader.onload = e => {
147+
current.src = e.target.result;
148+
};
149+
reader.readAsDataURL(file);
150+
}
151+
};
135152
return (
136153
<div className={classes.root}>
137154
<div className={classes.subdiv}>
@@ -263,7 +280,9 @@ const AddForm = () => {
263280
accept="image/*"
264281
className={classes.input}
265282
id="icon-button-file"
266-
type="file"
283+
type="file"
284+
ref={imageUploader}
285+
onChange={handleImageUpload}
267286
/>
268287

269288
<label htmlFor="icon-button-file">
@@ -279,7 +298,8 @@ const AddForm = () => {
279298
</Grid>
280299

281300
<Grid item xs={12} sm={7}>
282-
<img
301+
<img
302+
ref={uploadedImage}
283303
hidden
284304
style={{
285305
width: "100%",

tsconfig.json

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"esModuleInterop": true,
1212
"allowSyntheticDefaultImports": true,
1313
"strict": true,
14+
"strictNullChecks": false,
1415
"forceConsistentCasingInFileNames": true,
1516
"noFallthroughCasesInSwitch": true,
1617
"module": "esnext",

0 commit comments

Comments
 (0)