-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #38 from thomasoca/add-image-upload
Add image upload
- Loading branch information
Showing
7 changed files
with
162 additions
and
6 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import React, { FC } from "react"; | ||
import { Grid, Button } from "@material-ui/core"; | ||
import CloudUploadIcon from "@material-ui/icons/CloudUpload"; | ||
import DeleteIcon from "@material-ui/icons/Delete"; | ||
|
||
interface ImageUploadProps { | ||
path: string; | ||
updateValue: (newValue: string) => void; | ||
value?: string; | ||
} | ||
|
||
const getBase64 = (file: File) => { | ||
return new Promise<string>((resolve) => { | ||
let baseURL: string = ""; | ||
// Make new FileReader | ||
let reader = new FileReader(); | ||
|
||
// Convert the file to base64 text | ||
reader.readAsDataURL(file); | ||
|
||
// on reader load somthing... | ||
reader.onload = () => { | ||
// Make a fileInfo Object | ||
baseURL = reader.result as string; | ||
resolve(baseURL); | ||
}; | ||
}); | ||
}; | ||
|
||
const ImageUpload: FC<ImageUploadProps> = ({ path, updateValue, value }) => { | ||
const handleClear = () => { | ||
updateValue(""); | ||
}; | ||
return ( | ||
<div style={{ marginBottom: "20px" }}> | ||
<Grid container spacing={2} alignItems="center"> | ||
<Grid item> | ||
<input | ||
accept="image/*" | ||
style={{ display: "none" }} | ||
id={`icon_${path}`} | ||
onChange={(e) => { | ||
let file = e?.target.files?.item(0)!; | ||
getBase64(file) | ||
.then((result) => { | ||
updateValue(result); | ||
}) | ||
.catch((err) => { | ||
console.log(err); | ||
}); | ||
}} | ||
type="file" | ||
multiple={false} // Change to single image upload | ||
/> | ||
<label htmlFor={`icon_${path}`}> | ||
<Button | ||
variant="contained" | ||
color="primary" | ||
component="span" | ||
startIcon={<CloudUploadIcon />} | ||
> | ||
Upload Photo | ||
</Button> | ||
</label> | ||
</Grid> | ||
| ||
<Grid item> | ||
<Button | ||
onClick={handleClear} | ||
className="materialBtn" | ||
startIcon={<DeleteIcon />} | ||
> | ||
Clear | ||
</Button> | ||
</Grid> | ||
| ||
</Grid> | ||
</div> | ||
); | ||
}; | ||
|
||
export default ImageUpload; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { withJsonFormsControlProps } from "@jsonforms/react"; | ||
import ImageUpload from "./ImageUpload"; // Assuming you have an ImageUpload component | ||
import { rankWith, scopeEndsWith, ControlProps } from "@jsonforms/core"; | ||
|
||
interface ImageUploadControlTesterProps { | ||
uischema: any; | ||
schema: any; | ||
} | ||
|
||
interface NewHandleChangeProps { | ||
handleChange(path: string, newValue: any): void; | ||
path: string; | ||
newValue: any; | ||
} | ||
|
||
interface ImageUploadControlProps { | ||
data: any; | ||
path: string; | ||
label: string; | ||
handleChange(path: string, value: any): void; | ||
} | ||
|
||
export const ImageUploadControlTester = rankWith( | ||
3, // increase rank as needed | ||
scopeEndsWith("picture") | ||
); | ||
|
||
const newHandleChange = ({ | ||
handleChange, | ||
path, | ||
newValue, | ||
}: NewHandleChangeProps) => { | ||
handleChange(path, newValue); | ||
}; | ||
|
||
const ImageUploadControl = ({ | ||
data, | ||
handleChange, | ||
path, | ||
label, | ||
}: ControlProps) => ( | ||
<ImageUpload | ||
updateValue={(newValue: string) => | ||
newHandleChange({ handleChange, path, newValue }) | ||
} | ||
value={data} | ||
path={path} | ||
/> | ||
); | ||
|
||
export default withJsonFormsControlProps(ImageUploadControl); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters