UNPKG

4.63 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6
7var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
8
9var _react = require('react');
10
11var _react2 = _interopRequireDefault(_react);
12
13var _propTypes = require('prop-types');
14
15var _propTypes2 = _interopRequireDefault(_propTypes);
16
17var _reactDnd = require('react-dnd');
18
19var _reactDom = require('react-dom');
20
21function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
22
23function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; }
24
25var DragTypes = {
26 ROW: 'row'
27};
28var rowSource = {
29 canDrag: function canDrag(_ref) {
30 var rowId = _ref.rowId,
31 onCanMove = _ref.onCanMove;
32
33 return onCanMove ? onCanMove({ rowId: rowId }) : true;
34 },
35 beginDrag: function beginDrag(_ref2) {
36 var rowId = _ref2.rowId,
37 onMoveStart = _ref2.onMoveStart;
38
39 onMoveStart && onMoveStart({ rowId: rowId });
40
41 return { rowId: rowId };
42 },
43 endDrag: function endDrag(_ref3) {
44 var rowId = _ref3.rowId,
45 onMoveEnd = _ref3.onMoveEnd;
46
47 onMoveEnd && onMoveEnd({ rowId: rowId });
48 }
49};
50var rowTarget = {
51 hover: function hover(targetProps, monitor) {
52 var targetRowId = targetProps.rowId;
53 var sourceProps = monitor.getItem();
54 var sourceRowId = sourceProps.rowId;
55
56 // TODO: check if sourceRowId and targetRowId are undefined -> warning
57 if (sourceRowId !== targetRowId) {
58 targetProps.onMove({ sourceRowId: sourceRowId, targetRowId: targetRowId });
59 }
60 }
61};
62
63var dragSource = (0, _reactDnd.DragSource)( // eslint-disable-line new-cap
64DragTypes.ROW, rowSource, function (connect) {
65 return {
66 connectDragSource: connect.dragSource()
67 };
68});
69var dropTarget = (0, _reactDnd.DropTarget)( // eslint-disable-line new-cap
70DragTypes.ROW, rowTarget, function (connect) {
71 return {
72 connectDropTarget: connect.dropTarget()
73 };
74});
75var DraggableRow = function DraggableRow(_ref4) {
76 var _parent = _ref4._parent,
77 connectDragSource = _ref4.connectDragSource,
78 connectDropTarget = _ref4.connectDropTarget,
79 onCanMove = _ref4.onCanMove,
80 onMoveStart = _ref4.onMoveStart,
81 onMoveEnd = _ref4.onMoveEnd,
82 onMove = _ref4.onMove,
83 rowId = _ref4.rowId,
84 props = _objectWithoutProperties(_ref4, ['_parent', 'connectDragSource', 'connectDropTarget', 'onCanMove', 'onMoveStart', 'onMoveEnd', 'onMove', 'rowId']);
85
86 return (
87 // If you want to drag using a handle instead, then you need to pass
88 // connectDragSource to a customized cell (DndCell) through React
89 // context and wrap the handle there. You also need to annotate
90 // this function using connectDragPreview.
91 //
92 // https://github.com/gaearon/react-dnd/releases/tag/v2.0.0 - ref trick
93 _react2.default.createElement(_parent, _extends({}, props, {
94 ref: function ref(e) {
95 if (!e) {
96 return;
97 }
98
99 // XXXXX: Refactor this out
100 // eslint-disable-next-line react/no-find-dom-node
101 var node = (0, _reactDom.findDOMNode)(e);
102
103 // Chaining is not allowed
104 // https://github.com/gaearon/react-dnd/issues/305#issuecomment-164490014
105 connectDropTarget(node);
106 connectDragSource(node);
107 }
108 }))
109 );
110};
111process.env.NODE_ENV !== "production" ? DraggableRow.propTypes = {
112 _parent: _propTypes2.default.oneOfType([_propTypes2.default.func, _propTypes2.default.node]).isRequired,
113 connectDragSource: _propTypes2.default.func.isRequired,
114 connectDropTarget: _propTypes2.default.func.isRequired,
115 onMove: _propTypes2.default.func.isRequired,
116 onCanMove: _propTypes2.default.func,
117 onMoveStart: _propTypes2.default.func,
118 onMoveEnd: _propTypes2.default.func,
119 rowId: _propTypes2.default.any.isRequired
120} : void 0;
121
122var SourceTargetDraggableRow = dragSource(dropTarget(DraggableRow));
123
124var draggableRow = function draggableRow(_parent) {
125 function draggable(children) {
126 return _react2.default.createElement(SourceTargetDraggableRow, _extends({
127 _parent: _parent
128 }, children));
129 }
130
131 // Copy possible shouldComponentUpdate over or otherwise features
132 // like virtualization won't work.
133 draggable.shouldComponentUpdate = _parent.shouldComponentUpdate;
134
135 return draggable;
136};
137
138exports.default = draggableRow;
\No newline at end of file