You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
133 lines
3.0 KiB
133 lines
3.0 KiB
4 years ago
|
import React, { Component } from 'react';
|
||
|
import PropTypes from 'prop-types';
|
||
|
import classnames from 'classnames';
|
||
|
import { DragSource, DropTarget } from 'react-dnd';
|
||
6 years ago
|
|
||
|
class DraggableSeed extends Component {
|
||
|
static propTypes = {
|
||
|
// React DnD Props
|
||
|
connectDragSource: PropTypes.func.isRequired,
|
||
|
connectDropTarget: PropTypes.func.isRequired,
|
||
|
isDragging: PropTypes.bool,
|
||
|
isOver: PropTypes.bool,
|
||
|
canDrop: PropTypes.bool,
|
||
|
// Own Props
|
||
5 years ago
|
onClick: PropTypes.func,
|
||
6 years ago
|
setHoveringIndex: PropTypes.func.isRequired,
|
||
|
index: PropTypes.number,
|
||
|
word: PropTypes.string,
|
||
|
className: PropTypes.string,
|
||
|
selected: PropTypes.bool,
|
||
4 years ago
|
};
|
||
6 years ago
|
|
||
|
static defaultProps = {
|
||
|
className: '',
|
||
5 years ago
|
onClick: undefined,
|
||
4 years ago
|
};
|
||
6 years ago
|
|
||
4 years ago
|
UNSAFE_componentWillReceiveProps(nextProps) {
|
||
4 years ago
|
const { isOver, setHoveringIndex } = this.props;
|
||
6 years ago
|
if (isOver && !nextProps.isOver) {
|
||
4 years ago
|
setHoveringIndex(-1);
|
||
6 years ago
|
}
|
||
|
}
|
||
|
|
||
4 years ago
|
render() {
|
||
6 years ago
|
const {
|
||
|
connectDragSource,
|
||
|
connectDropTarget,
|
||
|
isDragging,
|
||
|
index,
|
||
|
word,
|
||
|
selected,
|
||
|
className,
|
||
|
onClick,
|
||
|
isOver,
|
||
|
canDrop,
|
||
4 years ago
|
} = this.props;
|
||
6 years ago
|
|
||
4 years ago
|
return connectDropTarget(
|
||
|
connectDragSource(
|
||
|
<div
|
||
|
key={index}
|
||
|
className={classnames(
|
||
|
'btn-secondary notranslate confirm-seed-phrase__seed-word',
|
||
|
className,
|
||
|
{
|
||
|
'confirm-seed-phrase__seed-word--selected btn-primary': selected,
|
||
|
'confirm-seed-phrase__seed-word--dragging': isDragging,
|
||
|
'confirm-seed-phrase__seed-word--empty': !word,
|
||
|
'confirm-seed-phrase__seed-word--active-drop': !isOver && canDrop,
|
||
|
'confirm-seed-phrase__seed-word--drop-hover': isOver && canDrop,
|
||
|
},
|
||
|
)}
|
||
|
onClick={onClick}
|
||
|
data-testid={`draggable-seed-${selected ? 'selected-' : ''}${word}`}
|
||
|
>
|
||
|
{word}
|
||
|
</div>,
|
||
|
),
|
||
4 years ago
|
);
|
||
6 years ago
|
}
|
||
|
}
|
||
|
|
||
4 years ago
|
const SEEDWORD = 'SEEDWORD';
|
||
6 years ago
|
|
||
|
const seedSource = {
|
||
4 years ago
|
beginDrag(props) {
|
||
4 years ago
|
setTimeout(() => props.setDraggingSeedIndex(props.seedIndex), 0);
|
||
6 years ago
|
return {
|
||
|
seedIndex: props.seedIndex,
|
||
|
word: props.word,
|
||
4 years ago
|
};
|
||
6 years ago
|
},
|
||
4 years ago
|
canDrag(props) {
|
||
4 years ago
|
return props.draggable;
|
||
6 years ago
|
},
|
||
4 years ago
|
endDrag(props, monitor) {
|
||
4 years ago
|
const dropTarget = monitor.getDropResult();
|
||
6 years ago
|
|
||
|
if (!dropTarget) {
|
||
4 years ago
|
setTimeout(() => props.setDraggingSeedIndex(-1), 0);
|
||
|
return;
|
||
6 years ago
|
}
|
||
|
|
||
4 years ago
|
props.onDrop(dropTarget.targetIndex);
|
||
6 years ago
|
},
|
||
4 years ago
|
};
|
||
6 years ago
|
|
||
|
const seedTarget = {
|
||
4 years ago
|
drop(props) {
|
||
6 years ago
|
return {
|
||
|
targetIndex: props.index,
|
||
4 years ago
|
};
|
||
6 years ago
|
},
|
||
4 years ago
|
canDrop(props) {
|
||
4 years ago
|
return props.droppable;
|
||
6 years ago
|
},
|
||
4 years ago
|
hover(props) {
|
||
4 years ago
|
props.setHoveringIndex(props.index);
|
||
6 years ago
|
},
|
||
4 years ago
|
};
|
||
6 years ago
|
|
||
|
const collectDrag = (connect, monitor) => {
|
||
|
return {
|
||
|
connectDragSource: connect.dragSource(),
|
||
|
isDragging: monitor.isDragging(),
|
||
4 years ago
|
};
|
||
|
};
|
||
6 years ago
|
|
||
|
const collectDrop = (connect, monitor) => {
|
||
|
return {
|
||
|
connectDropTarget: connect.dropTarget(),
|
||
|
isOver: monitor.isOver(),
|
||
|
canDrop: monitor.canDrop(),
|
||
4 years ago
|
};
|
||
|
};
|
||
6 years ago
|
|
||
4 years ago
|
export default DropTarget(
|
||
|
SEEDWORD,
|
||
|
seedTarget,
|
||
|
collectDrop,
|
||
4 years ago
|
)(DragSource(SEEDWORD, seedSource, collectDrag)(DraggableSeed));
|