-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Expand file tree
/
Copy pathImagePreview.js
More file actions
51 lines (43 loc) · 1.31 KB
/
ImagePreview.js
File metadata and controls
51 lines (43 loc) · 1.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import React, { useEffect, useState } from 'react';
import PropTypes from 'prop-types';
import styled from '@emotion/styled';
import { List } from 'immutable';
import { WidgetPreviewContainer } from 'decap-cms-ui-default';
const StyledImage = styled(({ src }) => <img src={src || ''} role="presentation" />)`
display: block;
max-width: 100%;
height: auto;
`;
function StyledImageAsset({ getAsset, value, field }) {
const [asset, setAsset] = useState(null);
useEffect(() => {
if (value) {
const newAsset = getAsset(value, field);
setAsset(newAsset);
} else {
setAsset(null);
}
}, [value, field, getAsset]);
return asset ? <StyledImage role="presentation" src={asset} /> : null;
}
function ImagePreviewContent(props) {
const { value, getAsset, field } = props;
if (Array.isArray(value) || List.isList(value)) {
return value.map((val, index) => (
<StyledImageAsset key={index} value={val} getAsset={getAsset} field={field} />
));
}
return <StyledImageAsset {...props} />;
}
function ImagePreview(props) {
return (
<WidgetPreviewContainer>
{props.value ? <ImagePreviewContent {...props} /> : null}
</WidgetPreviewContainer>
);
}
ImagePreview.propTypes = {
getAsset: PropTypes.func.isRequired,
value: PropTypes.node,
};
export default ImagePreview;