Add base64 verification, retrun null if not shouldRender
This commit is contained in:
1 parent
d799fe1385
commit
3c6f3f08e1
1 file changed
+22
@@ -1,14 +1,32 @@
|
|||||||
import { Box } from "@mui/material";
|
import { Box } from "@mui/material";
|
||||||
import PropTypes from "prop-types";
|
import PropTypes from "prop-types";
|
||||||
|
|
||||||
|
const isValidBase64Image = (data) => {
|
||||||
|
return /^[A-Za-z0-9+/=]+$/.test(data);
|
||||||
|
};
|
||||||
|
|
||||||
const Image = ({
|
const Image = ({
|
||||||
|
shouldRender = true,
|
||||||
src,
|
src,
|
||||||
alt,
|
alt,
|
||||||
width = "auto",
|
width = "auto",
|
||||||
height = "auto",
|
height = "auto",
|
||||||
maxWidth = "auto",
|
maxWidth = "auto",
|
||||||
maxHeight = "auto",
|
maxHeight = "auto",
|
||||||
|
base64,
|
||||||
}) => {
|
}) => {
|
||||||
|
if (shouldRender === false) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (typeof src !== "undefined" && typeof base64 !== "undefined") {
|
||||||
|
console.warn("base64 takes precedence over src and overwrites it");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (typeof base64 !== "undefined" && isValidBase64Image(base64)) {
|
||||||
|
src = `data:image/png;base64,${base64}`;
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Box
|
<Box
|
||||||
component="img"
|
component="img"
|
||||||
@@ -23,10 +41,14 @@ const Image = ({
|
|||||||
};
|
};
|
||||||
|
|
||||||
Image.propTypes = {
|
Image.propTypes = {
|
||||||
|
shouldRender: PropTypes.bool,
|
||||||
src: PropTypes.string,
|
src: PropTypes.string,
|
||||||
alt: PropTypes.string.isRequired,
|
alt: PropTypes.string.isRequired,
|
||||||
width: PropTypes.string,
|
width: PropTypes.string,
|
||||||
height: PropTypes.string,
|
height: PropTypes.string,
|
||||||
|
maxWidth: PropTypes.string,
|
||||||
|
maxHeight: PropTypes.string,
|
||||||
|
base64: PropTypes.string,
|
||||||
};
|
};
|
||||||
|
|
||||||
export default Image;
|
export default Image;
|
||||||
Reference in new issue
Block a user