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 (
{
if (isFullSize && !dragStart) {
setDragStart(true);
}
}}
onDragEnd={() => {
if (isFullSize) {
setDragStart(false);
}
}}
>
{dragStart && isFullSize && (
)}
<>
{showLeftCarret && }
{type === 'component' &&
componentLayout.map((row, i) => {
const marginBottom =
i === componentLayout.length - 1 ? '29px' : '';
const marginTop = i === 0 ? '5px' : '';
return (
{row.map(field => {
const fieldType = get(
componentData,
['schema', 'attributes', field.name, 'type'],
''
);
const label = get(
componentData,
['metadatas', field.name, 'edit', 'label'],
''
);
return (
);
})}
);
})}
{type === 'dynamiczone' && (
{dynamicZoneComponents.map(compo => {
return (
);
})}
)}
{showRightCarret && }
>
);
}
);
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;