Erez Rokah 02ef2010e7
Fix: show specific field media files in library, cascade folder templates (#3252)
* feat: cascade & compose media folders - initial commit

* refactor: code cleanup

* fix: pass field instead of folder to getAsset

* fix: only show field media files in library

* test: fix medial library selector test

* fix: fallback to original path when asset not found

* fix: only show field media files in media library

* fix: properly handle empty strings in field folders
2020-02-14 15:31:33 -05:00

41 lines
1.0 KiB
JavaScript

import React from 'react';
import PropTypes from 'prop-types';
import styled from '@emotion/styled';
import { List } from 'immutable';
import { WidgetPreviewContainer } from 'netlify-cms-ui-default';
const StyledImage = styled(({ src }) => <img src={src || ''} role="presentation" />)`
display: block;
max-width: 100%;
height: auto;
`;
const StyledImageAsset = ({ getAsset, value, field }) => {
return <StyledImage src={getAsset(value, field)} />;
};
const ImagePreviewContent = props => {
const { value, getAsset, field } = props;
if (Array.isArray(value) || List.isList(value)) {
return value.map(val => (
<StyledImageAsset key={val} value={val} getAsset={getAsset} field={field} />
));
}
return <StyledImageAsset {...props} />;
};
const ImagePreview = props => {
return (
<WidgetPreviewContainer>
{props.value ? <ImagePreviewContent {...props} /> : null}
</WidgetPreviewContainer>
);
};
ImagePreview.propTypes = {
getAsset: PropTypes.func.isRequired,
value: PropTypes.node,
};
export default ImagePreview;