88 lines
2.1 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-18 15:27:45 +02:00
2019-10-22 15:21:01 +02:00
import { Grab, GrabLarge, Pencil, Remove } from '@buffetjs/icons';
2019-10-17 15:18:35 +02:00
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
(
{
count,
isDragging,
isHidden,
name,
onClick,
onRemove,
selectedItem,
style,
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-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}
isSelected={isSelected}
isOverRemove={isOverRemove}
style={style}
2019-10-22 15:21:01 +02:00
withLongerHeight={withLongerHeight}
2019-10-18 15:27:45 +02:00
>
{!isHidden && (
<div className="sub_wrapper" style={{ opacity }}>
<div className="grab" ref={ref}>
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>
<div className="name" onClick={() => onClick(name)}>
{name}
</div>
<div
className="remove"
onClick={onRemove}
onMouseEnter={() => setIsOverRemove(true)}
onMouseLeave={() => setIsOverRemove(false)}
>
{isSelected ? <Pencil /> : <Remove />}
</div>
2019-10-17 15:18:35 +02:00
</div>
2019-10-18 15:27:45 +02:00
)}
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 = {
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-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,
withLongerHeight: PropTypes.bool,
2019-10-15 18:32:19 +02:00
};
export default DraggedField;