2019-05-07 17:03:27 +02:00
|
|
|
import React, { Component } from 'react'
|
|
|
|
import PropTypes from 'prop-types'
|
|
|
|
import classnames from 'classnames'
|
|
|
|
import { DragSource, DropTarget } from 'react-dnd'
|
|
|
|
|
|
|
|
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
|
2019-11-27 17:07:41 +01:00
|
|
|
onClick: PropTypes.func,
|
2019-05-07 17:03:27 +02:00
|
|
|
setHoveringIndex: PropTypes.func.isRequired,
|
|
|
|
index: PropTypes.number,
|
|
|
|
word: PropTypes.string,
|
|
|
|
className: PropTypes.string,
|
|
|
|
selected: PropTypes.bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
static defaultProps = {
|
|
|
|
className: '',
|
2019-11-27 17:07:41 +01:00
|
|
|
onClick: undefined,
|
2019-05-07 17:03:27 +02:00
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
UNSAFE_componentWillReceiveProps(nextProps) {
|
2019-05-07 17:03:27 +02:00
|
|
|
const { isOver, setHoveringIndex } = this.props
|
|
|
|
if (isOver && !nextProps.isOver) {
|
|
|
|
setHoveringIndex(-1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
render() {
|
2019-05-07 17:03:27 +02:00
|
|
|
const {
|
|
|
|
connectDragSource,
|
|
|
|
connectDropTarget,
|
|
|
|
isDragging,
|
|
|
|
index,
|
|
|
|
word,
|
|
|
|
selected,
|
|
|
|
className,
|
|
|
|
onClick,
|
|
|
|
isOver,
|
|
|
|
canDrop,
|
|
|
|
} = this.props
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
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>,
|
|
|
|
),
|
|
|
|
)
|
2019-05-07 17:03:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const SEEDWORD = 'SEEDWORD'
|
|
|
|
|
|
|
|
const seedSource = {
|
2020-11-03 00:41:28 +01:00
|
|
|
beginDrag(props) {
|
2019-05-07 17:03:27 +02:00
|
|
|
setTimeout(() => props.setDraggingSeedIndex(props.seedIndex), 0)
|
|
|
|
return {
|
|
|
|
seedIndex: props.seedIndex,
|
|
|
|
word: props.word,
|
|
|
|
}
|
|
|
|
},
|
2020-11-03 00:41:28 +01:00
|
|
|
canDrag(props) {
|
2019-05-07 17:03:27 +02:00
|
|
|
return props.draggable
|
|
|
|
},
|
2020-11-03 00:41:28 +01:00
|
|
|
endDrag(props, monitor) {
|
2019-05-07 17:03:27 +02:00
|
|
|
const dropTarget = monitor.getDropResult()
|
|
|
|
|
|
|
|
if (!dropTarget) {
|
|
|
|
setTimeout(() => props.setDraggingSeedIndex(-1), 0)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
props.onDrop(dropTarget.targetIndex)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
const seedTarget = {
|
2020-11-03 00:41:28 +01:00
|
|
|
drop(props) {
|
2019-05-07 17:03:27 +02:00
|
|
|
return {
|
|
|
|
targetIndex: props.index,
|
|
|
|
}
|
|
|
|
},
|
2020-11-03 00:41:28 +01:00
|
|
|
canDrop(props) {
|
2019-05-07 17:03:27 +02:00
|
|
|
return props.droppable
|
|
|
|
},
|
2020-11-03 00:41:28 +01:00
|
|
|
hover(props) {
|
2019-05-07 17:03:27 +02:00
|
|
|
props.setHoveringIndex(props.index)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
const collectDrag = (connect, monitor) => {
|
|
|
|
return {
|
|
|
|
connectDragSource: connect.dragSource(),
|
|
|
|
isDragging: monitor.isDragging(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const collectDrop = (connect, monitor) => {
|
|
|
|
return {
|
|
|
|
connectDropTarget: connect.dropTarget(),
|
|
|
|
isOver: monitor.isOver(),
|
|
|
|
canDrop: monitor.canDrop(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
export default DropTarget(
|
|
|
|
SEEDWORD,
|
|
|
|
seedTarget,
|
|
|
|
collectDrop,
|
|
|
|
)(DragSource(SEEDWORD, seedSource, collectDrag)(DraggableSeed))
|