2019-11-26 16:15:59 +01:00

205 lines
6.2 KiB
JavaScript

import React, { forwardRef, useState } from 'react';
import { get } from 'lodash';
import PropTypes from 'prop-types';
import Carret from './Carret';
import DraggedField from '../DraggedField';
import DynamicZoneWrapper from './DynamicZoneWrapper';
import PreviewCarret from '../PreviewCarret';
import Wrapper from './Wrapper';
import DynamicComponent from './DynamicComponent';
const DraggedFieldWithPreview = forwardRef(
(
{
goTo,
componentUid,
componentLayouts,
dynamicZoneComponents,
isDragging,
label,
name,
onClickEdit,
onClickRemove,
selectedItem,
showLeftCarret,
showRightCarret,
size,
style,
type,
},
refs
) => {
const isHidden = name === '_TEMP_';
const [dragStart, setDragStart] = useState(false);
const [isOverDynamicZone, setIsOverDynamicZone] = useState(false);
const opacity = isDragging ? 0.2 : 1;
const isFullSize = size === 12;
const display = isFullSize && dragStart ? 'none' : '';
const width = isFullSize && dragStart ? 0 : '100%';
const higherFields = [
'json',
'text',
'file',
'media',
'component',
'richtext',
'dynamiczone',
];
const withLongerHeight = higherFields.includes(type) && !dragStart;
const componentData = get(componentLayouts, [componentUid], {});
const componentLayout = get(componentData, ['layouts', 'edit'], []);
const getWrapperWitdh = colNum => `${(1 / 12) * colNum * 100}%`;
return (
<div
style={{ width: getWrapperWitdh(size) }}
onDrag={() => {
if (isFullSize && !dragStart) {
setDragStart(true);
}
}}
onDragEnd={() => {
if (isFullSize) {
setDragStart(false);
}
}}
>
<Wrapper
ref={refs.dropRef}
withLongerHeight={withLongerHeight}
style={style}
>
{dragStart && isFullSize && (
<PreviewCarret style={{ marginRight: '-10px' }} />
)}
<>
{showLeftCarret && <Carret />}
<div className="sub" style={{ width, opacity }}>
<DraggedField
goTo={goTo}
componentUid={componentUid}
isHidden={isHidden}
isOverDynamicZone={isOverDynamicZone}
label={label}
name={name}
onClick={onClickEdit}
onRemove={onClickRemove}
ref={refs.dragRef}
selectedItem={selectedItem}
style={{ display, marginRight: 0, paddingRight: 0 }}
type={type}
withLongerHeight={withLongerHeight}
>
{type === 'component' &&
componentLayout.map((row, i) => {
const marginBottom =
i === componentLayout.length - 1 ? '29px' : '';
const marginTop = i === 0 ? '5px' : '';
return (
<div
style={{
display: 'flex',
marginBottom,
marginTop,
}}
key={i}
>
{row.map(field => {
const fieldType = get(
componentData,
['schema', 'attributes', field.name, 'type'],
''
);
const label = get(
componentData,
['metadatas', field.name, 'edit', 'label'],
''
);
return (
<div
key={field.name}
style={{
width: getWrapperWitdh(field.size),
marginBottom: '6px',
}}
>
<DraggedField
label={label}
name={field.name}
isSub
withLongerHeight={higherFields.includes(
fieldType
)}
></DraggedField>
</div>
);
})}
</div>
);
})}
{type === 'dynamiczone' && (
<DynamicZoneWrapper>
{dynamicZoneComponents.map(compo => {
return (
<DynamicComponent
key={compo}
componentUid={compo}
setIsOverDynamicZone={setIsOverDynamicZone}
/>
);
})}
</DynamicZoneWrapper>
)}
</DraggedField>
</div>
{showRightCarret && <Carret right />}
</>
</Wrapper>
</div>
);
}
);
DraggedFieldWithPreview.defaultProps = {
goTo: () => {},
componentLayouts: {},
componentUid: null,
dynamicZoneComponents: [],
isDragging: false,
label: '',
onClickEdit: () => {},
onClickRemove: () => {},
selectedItem: '',
showLeftCarret: false,
showRightCarret: false,
size: 1,
style: {},
type: 'string',
};
DraggedFieldWithPreview.propTypes = {
goTo: PropTypes.func,
componentLayouts: PropTypes.object,
componentUid: PropTypes.string,
dynamicZoneComponents: PropTypes.array,
isDragging: PropTypes.bool,
label: PropTypes.string,
name: PropTypes.string.isRequired,
onClickEdit: PropTypes.func,
onClickRemove: PropTypes.func,
selectedItem: PropTypes.string,
showLeftCarret: PropTypes.bool,
showRightCarret: PropTypes.bool,
size: PropTypes.number,
style: PropTypes.object,
type: PropTypes.string,
};
DraggedFieldWithPreview.displayName = 'DraggedFieldWithPreview';
export default DraggedFieldWithPreview;