122 lines
3.3 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-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';
import Link from './Link';
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,
isDragging,
isHidden,
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-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-23 10:53:49 +02:00
isOverEditBlock={isOverEditBlock}
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-22 17:24:11 +02:00
<div
className="sub_wrapper"
style={{ opacity }}
2019-10-23 10:53:49 +02:00
onMouseEnter={() => setIsOverEditBlock(true)}
onMouseLeave={() => setIsOverEditBlock(false)}
2019-10-22 17:24:11 +02:00
onClick={() => {
onClick(name);
}}
>
<div className="grab" ref={ref} onClick={e => e.stopPropagation()}>
2019-10-22 15:21:01 +02:00
{withLongerHeight ? (
<GrabLarge style={{ marginRight: 10, cursor: 'move' }} />
) : (
<Grab style={{ marginRight: 10, cursor: 'move' }} />
)}
2019-10-18 15:27:45 +02:00
</div>
2019-10-22 17:24:11 +02:00
<div className="name">{children ? children : name}</div>
2019-10-18 15:27:45 +02:00
<div
className="remove"
onClick={onRemove}
onMouseEnter={() => setIsOverRemove(true)}
onMouseLeave={() => setIsOverRemove(false)}
>
2019-10-23 10:53:49 +02:00
{isOverRemove && !isSelected && <Remove />}
{((isOverEditBlock && !isOverRemove) || isSelected) && <Pencil />}
{!isOverEditBlock && !isOverRemove && !isSelected && <Remove />}
2019-10-18 15:27:45 +02:00
</div>
2019-10-17 15:18:35 +02:00
</div>
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();
// push(
// `/plugins/${pluginId}/ctm-configurations/groups/${groupUid}`
// )
}}
>
{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-17 15:18:35 +02:00
isDragging: false,
2019-10-18 15:27:45 +02:00
isHidden: false,
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-17 15:18:35 +02:00
isDragging: PropTypes.bool,
2019-10-22 15:21:01 +02:00
isHidden: PropTypes.bool,
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;