/
MosaicDropTarget.tsx
59 lines (51 loc) · 1.75 KB
/
MosaicDropTarget.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import classNames from 'classnames';
import React from 'react';
import { ConnectDropTarget, DropTarget, DropTargetMonitor } from 'react-dnd';
import { MosaicContext } from './contextTypes';
import { MosaicDragItem, MosaicDropData, MosaicDropTargetPosition } from './internalTypes';
import { MosaicDragType, MosaicPath } from './types';
export interface MosaicDropTargetProps {
position: MosaicDropTargetPosition;
path: MosaicPath;
}
interface DropTargetProps {
connectDropTarget: ConnectDropTarget;
isOver: boolean;
draggedMosaicId: string | undefined;
}
type Props = MosaicDropTargetProps & DropTargetProps;
const dropTarget = {
drop: (props: Props, monitor: DropTargetMonitor, component: MosaicDropTargetClass): MosaicDropData => {
if (component.context.mosaicId === ((monitor.getItem() || {}) as MosaicDragItem).mosaicId) {
return {
path: props.path,
position: props.position,
};
} else {
return {};
}
},
};
class MosaicDropTargetClass extends React.PureComponent<Props> {
static contextTypes = MosaicContext;
context!: MosaicContext<any>;
render() {
const { position, isOver, connectDropTarget, draggedMosaicId } = this.props;
return connectDropTarget(
<div
className={classNames('drop-target', position, {
'drop-target-hover': isOver && draggedMosaicId === this.context.mosaicId,
})}
/>,
);
}
}
export const MosaicDropTarget = (DropTarget(
MosaicDragType.WINDOW,
dropTarget,
(connect, monitor): DropTargetProps => ({
connectDropTarget: connect.dropTarget(),
isOver: monitor.isOver(),
draggedMosaicId: ((monitor.getItem() || {}) as MosaicDragItem).mosaicId,
}),
)(MosaicDropTargetClass) as any) as React.ComponentType<MosaicDropTargetProps>;