118 lines
3.1 KiB
JavaScript
Raw Normal View History

2019-10-22 15:21:01 +02:00
import React, { forwardRef, useState } from 'react';
2019-10-18 15:27:45 +02:00
import PropTypes from 'prop-types';
import Carret from './Carret';
import DraggedField from '../DraggedField';
2019-10-22 15:21:01 +02:00
import PreviewCarret from '../PreviewCarret';
2019-10-18 15:27:45 +02:00
import Wrapper from './Wrapper';
// eslint-disable-next-line react/display-name
const DraggedFieldWithPreview = forwardRef(
2019-10-22 15:21:01 +02:00
(
2019-10-22 17:24:11 +02:00
{
2019-10-23 11:36:46 +02:00
goTo,
groupUid,
2019-10-23 10:53:49 +02:00
isDragging,
2019-10-22 17:24:11 +02:00
name,
onClickEdit,
onClickRemove,
selectedItem,
showLeftCarret,
showRightCarret,
size,
2019-10-23 11:11:30 +02:00
style,
2019-10-22 17:24:11 +02:00
type,
},
2019-10-22 15:21:01 +02:00
refs
) => {
2019-10-18 15:27:45 +02:00
const isHidden = name === '_TEMP_';
2019-10-22 15:21:01 +02:00
const [dragStart, setDragStart] = useState(false);
2019-10-23 10:53:49 +02:00
const opacity = isDragging ? 0.2 : 1;
2019-10-22 15:21:01 +02:00
const isFullSize = size === 12;
const display = isFullSize && dragStart ? 'none' : '';
const width = isFullSize && dragStart ? 0 : '100%';
const withLongerHeight =
['json', 'text', 'file', 'media', 'group', 'richtext'].includes(type) &&
!dragStart;
2019-10-22 17:24:11 +02:00
const carretStyle = withLongerHeight ? { height: '102px' } : {};
2019-10-18 15:27:45 +02:00
return (
2019-10-22 15:21:01 +02:00
<div
style={{ width: `${(1 / 12) * size * 100}%` }}
onDrag={() => {
if (isFullSize && !dragStart) {
setDragStart(true);
}
}}
onDragEnd={() => {
if (isFullSize) {
setDragStart(false);
}
}}
>
2019-10-23 11:11:30 +02:00
<Wrapper
ref={refs.dropRef}
withLongerHeight={withLongerHeight}
style={style}
>
2019-10-22 15:21:01 +02:00
{dragStart && isFullSize && (
<PreviewCarret style={{ marginRight: '-10px' }} />
)}
<>
{showLeftCarret && <Carret style={carretStyle} />}
2019-10-18 15:27:45 +02:00
2019-10-23 10:53:49 +02:00
<div className="sub" style={{ width, opacity }}>
2019-10-22 15:21:01 +02:00
<DraggedField
2019-10-23 11:36:46 +02:00
goTo={goTo}
groupUid={groupUid}
2019-10-22 15:21:01 +02:00
isHidden={isHidden}
name={name}
2019-10-22 17:24:11 +02:00
onClick={onClickEdit}
2019-10-22 15:21:01 +02:00
onRemove={onClickRemove}
2019-10-23 11:36:46 +02:00
ref={refs.dragRef}
2019-10-22 17:24:11 +02:00
selectedItem={selectedItem}
2019-10-22 15:21:01 +02:00
style={{ padding: 0, margin: 0, display }}
2019-10-22 17:24:11 +02:00
type={type}
2019-10-22 15:21:01 +02:00
withLongerHeight={withLongerHeight}
2019-10-22 17:24:11 +02:00
></DraggedField>
2019-10-22 15:21:01 +02:00
</div>
{showRightCarret && <Carret right style={carretStyle} />}
</>
2019-10-18 15:27:45 +02:00
</Wrapper>
</div>
);
}
);
2019-10-22 10:18:01 +02:00
DraggedFieldWithPreview.defaultProps = {
2019-10-23 11:36:46 +02:00
goTo: () => {
console.log('l');
},
groupUid: null,
2019-10-23 10:53:49 +02:00
isDragging: false,
2019-10-22 17:24:11 +02:00
onClickEdit: () => {},
2019-10-22 15:21:01 +02:00
onClickRemove: () => {},
2019-10-22 17:24:11 +02:00
selectedItem: '',
2019-10-22 10:18:01 +02:00
showLeftCarret: false,
showRightCarret: false,
size: 1,
2019-10-23 11:11:30 +02:00
style: {},
2019-10-22 15:21:01 +02:00
type: 'string',
2019-10-22 10:18:01 +02:00
};
DraggedFieldWithPreview.propTypes = {
2019-10-23 11:36:46 +02:00
goTo: PropTypes.func,
groupUid: PropTypes.string,
2019-10-23 10:53:49 +02:00
isDragging: PropTypes.bool,
2019-10-22 10:18:01 +02:00
name: PropTypes.string.isRequired,
2019-10-22 17:24:11 +02:00
onClickEdit: PropTypes.func,
2019-10-22 15:21:01 +02:00
onClickRemove: PropTypes.func,
2019-10-22 17:24:11 +02:00
selectedItem: PropTypes.string,
2019-10-22 10:18:01 +02:00
showLeftCarret: PropTypes.bool,
showRightCarret: PropTypes.bool,
size: PropTypes.number,
2019-10-23 11:11:30 +02:00
style: PropTypes.object,
2019-10-22 15:21:01 +02:00
type: PropTypes.string,
2019-10-22 10:18:01 +02:00
};
2019-10-18 15:27:45 +02:00
export default DraggedFieldWithPreview;