1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 |
|
7 | var _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 |
|
9 | var _react = require('react');
|
10 |
|
11 | var _react2 = _interopRequireDefault(_react);
|
12 |
|
13 | var _propTypes = require('prop-types');
|
14 |
|
15 | var _propTypes2 = _interopRequireDefault(_propTypes);
|
16 |
|
17 | var _reactDnd = require('react-dnd');
|
18 |
|
19 | var _reactDom = require('react-dom');
|
20 |
|
21 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
22 |
|
23 | function _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 |
|
25 | var DragTypes = {
|
26 | ROW: 'row'
|
27 | };
|
28 | var 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 | };
|
50 | var rowTarget = {
|
51 | hover: function hover(targetProps, monitor) {
|
52 | var targetRowId = targetProps.rowId;
|
53 | var sourceProps = monitor.getItem();
|
54 | var sourceRowId = sourceProps.rowId;
|
55 |
|
56 |
|
57 | if (sourceRowId !== targetRowId) {
|
58 | targetProps.onMove({ sourceRowId: sourceRowId, targetRowId: targetRowId });
|
59 | }
|
60 | }
|
61 | };
|
62 |
|
63 | var dragSource = (0, _reactDnd.DragSource)(
|
64 | DragTypes.ROW, rowSource, function (connect) {
|
65 | return {
|
66 | connectDragSource: connect.dragSource()
|
67 | };
|
68 | });
|
69 | var dropTarget = (0, _reactDnd.DropTarget)(
|
70 | DragTypes.ROW, rowTarget, function (connect) {
|
71 | return {
|
72 | connectDropTarget: connect.dropTarget()
|
73 | };
|
74 | });
|
75 | var 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 |
|
88 |
|
89 |
|
90 |
|
91 |
|
92 |
|
93 | _react2.default.createElement(_parent, _extends({}, props, {
|
94 | ref: function ref(e) {
|
95 | if (!e) {
|
96 | return;
|
97 | }
|
98 |
|
99 | var node = (0, _reactDom.findDOMNode)(e);
|
100 |
|
101 |
|
102 |
|
103 | connectDropTarget(node);
|
104 | connectDragSource(node);
|
105 | }
|
106 | }))
|
107 | );
|
108 | };
|
109 | process.env.NODE_ENV !== "production" ? DraggableRow.propTypes = {
|
110 | _parent: _propTypes2.default.oneOfType([_propTypes2.default.func, _propTypes2.default.node]).isRequired,
|
111 | connectDragSource: _propTypes2.default.func.isRequired,
|
112 | connectDropTarget: _propTypes2.default.func.isRequired,
|
113 | onMove: _propTypes2.default.func.isRequired,
|
114 | onCanMove: _propTypes2.default.func,
|
115 | onMoveStart: _propTypes2.default.func,
|
116 | onMoveEnd: _propTypes2.default.func,
|
117 | rowId: _propTypes2.default.any.isRequired
|
118 | } : void 0;
|
119 |
|
120 | var SourceTargetDraggableRow = dragSource(dropTarget(DraggableRow));
|
121 |
|
122 | var draggableRow = function draggableRow(_parent) {
|
123 | function draggable(children) {
|
124 | return _react2.default.createElement(SourceTargetDraggableRow, _extends({
|
125 | _parent: _parent
|
126 | }, children));
|
127 | }
|
128 |
|
129 |
|
130 |
|
131 | draggable.shouldComponentUpdate = _parent.shouldComponentUpdate;
|
132 |
|
133 | return draggable;
|
134 | };
|
135 |
|
136 | exports.default = draggableRow; |
\ | No newline at end of file |