189 lines
5.2 KiB
JavaScript
Raw Normal View History

2019-10-17 15:18:35 +02:00
/* eslint-disable react/display-name */
2019-10-18 15:27:45 +02:00
import React, { forwardRef, useState } from 'react';
2019-10-15 18:32:19 +02:00
import PropTypes from 'prop-types';
2019-10-24 15:45:50 +02:00
import { isEmpty } from 'lodash';
2019-10-22 17:24:11 +02:00
import { FormattedMessage } from 'react-intl';
2019-10-22 15:21:01 +02:00
import { Grab, GrabLarge, Pencil, Remove } from '@buffetjs/icons';
2019-10-22 17:24:11 +02:00
import pluginId from '../../pluginId';
2019-10-24 15:28:47 +02:00
import GrabWrapper from './GrabWrapper';
2019-10-22 17:24:11 +02:00
import Link from './Link';
2019-10-24 15:28:47 +02:00
import NameWrapper from './NameWrapper';
import RemoveWrapper from './RemoveWrapper';
import SubWrapper from './SubWrapper';
2019-10-15 18:32:19 +02:00
import Wrapper from './Wrapper';
2019-10-17 15:18:35 +02:00
const DraggedField = forwardRef(
2019-10-18 15:27:45 +02:00
(
{
2019-10-22 17:24:11 +02:00
children,
2019-10-18 15:27:45 +02:00
count,
2019-10-23 11:36:46 +02:00
goTo,
groupUid,
2019-10-18 15:27:45 +02:00
isDragging,
2019-10-24 15:28:47 +02:00
isDraggingSibling,
2019-10-18 15:27:45 +02:00
isHidden,
2019-10-24 15:28:47 +02:00
isSub,
2019-10-24 15:45:50 +02:00
label,
2019-10-18 15:27:45 +02:00
name,
onClick,
onRemove,
selectedItem,
style,
2019-10-22 17:24:11 +02:00
type,
2019-10-22 15:21:01 +02:00
withLongerHeight,
2019-10-18 15:27:45 +02:00
},
ref
) => {
2019-10-17 15:18:35 +02:00
const opacity = isDragging ? 0.2 : 1;
2019-10-18 15:27:45 +02:00
const [isOverRemove, setIsOverRemove] = useState(false);
2019-10-23 10:53:49 +02:00
const [isOverEditBlock, setIsOverEditBlock] = useState(false);
2019-10-17 15:52:02 +02:00
const isSelected = selectedItem === name;
2019-10-24 15:28:47 +02:00
const showEditBlockOverState = isOverEditBlock && !isDraggingSibling;
2019-10-24 15:45:50 +02:00
const displayedLabel = isEmpty(label) ? name : label;
2019-10-17 15:18:35 +02:00
return (
2019-10-18 15:27:45 +02:00
<Wrapper
count={count}
2019-10-23 10:53:49 +02:00
onDrag={() => setIsOverEditBlock(false)}
2019-10-18 15:27:45 +02:00
isSelected={isSelected}
2019-10-24 15:28:47 +02:00
isOverEditBlock={showEditBlockOverState}
2019-10-18 15:27:45 +02:00
isOverRemove={isOverRemove}
style={style}
2019-10-22 15:21:01 +02:00
withLongerHeight={withLongerHeight}
2019-10-18 15:27:45 +02:00
>
{!isHidden && (
2019-10-24 15:28:47 +02:00
<SubWrapper
2019-10-22 17:24:11 +02:00
className="sub_wrapper"
2019-10-24 15:28:47 +02:00
isSelected={isSelected}
isOverEditBlock={isOverEditBlock}
isOverRemove={isOverRemove}
onMouseEnter={() => {
if (!isSub) {
setIsOverEditBlock(true);
}
}}
2019-10-23 10:53:49 +02:00
onMouseLeave={() => setIsOverEditBlock(false)}
2019-10-22 17:24:11 +02:00
onClick={() => {
onClick(name);
}}
2019-10-24 15:28:47 +02:00
style={{ opacity }}
withLongerHeight={withLongerHeight}
2019-10-22 17:24:11 +02:00
>
2019-10-24 15:28:47 +02:00
<GrabWrapper
className="grab"
isSelected={isSelected}
isOverEditBlock={showEditBlockOverState}
isOverRemove={isOverRemove}
ref={ref}
onClick={e => {
e.stopPropagation();
e.preventDefault();
}}
>
2019-10-22 15:21:01 +02:00
{withLongerHeight ? (
<GrabLarge style={{ marginRight: 10, cursor: 'move' }} />
) : (
<Grab style={{ marginRight: 10, cursor: 'move' }} />
)}
2019-10-24 15:28:47 +02:00
</GrabWrapper>
<NameWrapper
className="name"
isSelected={isSelected}
isOverEditBlock={showEditBlockOverState}
isOverRemove={isOverRemove}
>
{children ? (
<>
2019-10-24 15:45:50 +02:00
<span>{displayedLabel}</span>
2019-10-24 15:28:47 +02:00
{children}
</>
) : (
2019-10-24 15:45:50 +02:00
<span>{displayedLabel}</span>
2019-10-24 15:28:47 +02:00
)}
</NameWrapper>
<RemoveWrapper
2019-10-18 15:27:45 +02:00
className="remove"
2019-10-24 15:28:47 +02:00
isSelected={isSelected}
// isOverEditBlock={isOverEditBlock}
isOverEditBlock={showEditBlockOverState}
isOverRemove={isOverRemove}
2019-10-18 15:27:45 +02:00
onClick={onRemove}
2019-10-24 15:28:47 +02:00
onMouseEnter={() => {
if (!isSub) {
setIsOverRemove(true);
}
}}
2019-10-18 15:27:45 +02:00
onMouseLeave={() => setIsOverRemove(false)}
>
2019-10-23 10:53:49 +02:00
{isOverRemove && !isSelected && <Remove />}
2019-10-24 15:28:47 +02:00
{((showEditBlockOverState && !isOverRemove) || isSelected) && (
<Pencil />
)}
{!showEditBlockOverState && !isOverRemove && !isSelected && (
<Remove />
)}
</RemoveWrapper>
</SubWrapper>
2019-10-18 15:27:45 +02:00
)}
2019-10-22 17:24:11 +02:00
{type === 'group' && (
<FormattedMessage
id={`${pluginId}.components.FieldItem.linkToGroupLayout`}
>
{msg => (
<Link
onClick={e => {
e.stopPropagation();
2019-10-24 17:08:52 +02:00
2019-10-23 11:36:46 +02:00
goTo(
`/plugins/${pluginId}/ctm-configurations/edit-settings/groups/${groupUid}`
);
2019-10-22 17:24:11 +02:00
}}
>
{msg}
</Link>
)}
</FormattedMessage>
)}
2019-10-17 15:18:35 +02:00
</Wrapper>
);
}
);
2019-10-15 18:32:19 +02:00
2019-10-16 19:59:00 +02:00
DraggedField.defaultProps = {
2019-10-22 17:24:11 +02:00
children: null,
2019-10-16 19:59:00 +02:00
count: 1,
2019-10-23 11:36:46 +02:00
goTo: () => {},
groupUid: null,
2019-10-17 15:18:35 +02:00
isDragging: false,
2019-10-24 15:28:47 +02:00
isDraggingSibling: false,
2019-10-18 15:27:45 +02:00
isHidden: false,
2019-10-24 15:28:47 +02:00
isSub: false,
2019-10-24 15:45:50 +02:00
label: '',
2019-10-18 15:27:45 +02:00
onClick: () => {},
onRemove: () => {},
selectedItem: '',
style: {},
2019-10-22 15:21:01 +02:00
withLongerHeight: false,
2019-10-16 19:59:00 +02:00
};
2019-10-15 18:32:19 +02:00
DraggedField.propTypes = {
2019-10-22 17:24:11 +02:00
children: PropTypes.node,
2019-10-16 19:59:00 +02:00
count: PropTypes.number,
2019-10-23 11:36:46 +02:00
goTo: PropTypes.func,
groupUid: PropTypes.string,
2019-10-17 15:18:35 +02:00
isDragging: PropTypes.bool,
2019-10-24 15:28:47 +02:00
isDraggingSibling: PropTypes.bool,
2019-10-22 15:21:01 +02:00
isHidden: PropTypes.bool,
2019-10-24 15:28:47 +02:00
isSub: PropTypes.bool,
2019-10-24 15:45:50 +02:00
label: PropTypes.string,
2019-10-15 18:32:19 +02:00
name: PropTypes.string.isRequired,
2019-10-18 15:27:45 +02:00
onClick: PropTypes.func,
onRemove: PropTypes.func,
selectedItem: PropTypes.string,
2019-10-22 15:21:01 +02:00
style: PropTypes.object,
2019-10-22 17:24:11 +02:00
type: PropTypes.string,
2019-10-22 15:21:01 +02:00
withLongerHeight: PropTypes.bool,
2019-10-15 18:32:19 +02:00
};
export default DraggedField;