/* eslint-disable react/display-name */ import React, { forwardRef, useState } from 'react'; import PropTypes from 'prop-types'; import { isEmpty } from 'lodash'; import { FormattedMessage } from 'react-intl'; import { Grab, GrabLarge, Pencil, Remove } from '@buffetjs/icons'; import pluginId from '../../pluginId'; import { useLayoutDnd } from '../../contexts/LayoutDnd'; import GrabWrapper from './GrabWrapper'; import Link from './Link'; import NameWrapper from './NameWrapper'; import RemoveWrapper from './RemoveWrapper'; import SubWrapper from './SubWrapper'; import Wrapper from './Wrapper'; const DraggedField = forwardRef( ( { children, count, goTo, componentUid, isDragging, isHidden, isOverDynamicZone, isSub, label, name, onClick, onRemove, selectedItem, style, type, withLongerHeight, }, ref ) => { const { isDraggingSibling } = useLayoutDnd(); const [isOverRemove, setIsOverRemove] = useState(false); const [isOverEditBlock, setIsOverEditBlock] = useState(false); const opacity = isDragging ? 0.2 : 1; const isSelected = selectedItem === name; const showEditBlockOverState = isOverEditBlock && !isOverDynamicZone; const displayedLabel = isEmpty(label) ? name : label; return ( setIsOverEditBlock(false)} isSelected={isSelected} isSub={isSub} isOverEditBlock={showEditBlockOverState} isOverRemove={isOverRemove} style={style} withLongerHeight={withLongerHeight} > {!isHidden && ( { if (!isSub && !isDraggingSibling) { setIsOverEditBlock(true); } }} onMouseLeave={() => { setIsOverEditBlock(false); }} onClick={() => { onClick(name); }} style={{ opacity }} withLongerHeight={withLongerHeight} > { e.stopPropagation(); e.preventDefault(); }} > {withLongerHeight ? ( ) : ( )} {children ? ( <> {displayedLabel} {children} ) : ( {displayedLabel} )} { if (!isSub) { setIsOverRemove(true); } }} onMouseLeave={() => setIsOverRemove(false)} > {isOverRemove && !isSelected && } {((showEditBlockOverState && !isOverRemove) || isSelected) && ( )} {!showEditBlockOverState && !isOverRemove && !isSelected && ( )} )} {type === 'component' && ( {msg => ( { e.stopPropagation(); goTo( `/plugins/${pluginId}/ctm-configurations/edit-settings/components/${componentUid}` ); }} > {msg} )} )} ); } ); DraggedField.defaultProps = { children: null, count: 1, goTo: () => {}, componentUid: null, isDragging: false, isHidden: false, isOverDynamicZone: false, isSub: false, label: '', onClick: () => {}, onRemove: () => {}, selectedItem: '', shouldToggleDraggedFieldOverState: false, style: {}, withLongerHeight: false, }; DraggedField.propTypes = { children: PropTypes.node, count: PropTypes.number, goTo: PropTypes.func, componentUid: PropTypes.string, isDragging: PropTypes.bool, isHidden: PropTypes.bool, isOverDynamicZone: PropTypes.bool, isSub: PropTypes.bool, label: PropTypes.string, name: PropTypes.string.isRequired, onClick: PropTypes.func, onRemove: PropTypes.func, selectedItem: PropTypes.string, style: PropTypes.object, type: PropTypes.string, withLongerHeight: PropTypes.bool, }; export default DraggedField;