@goldenpine/react-form-builder2 0.18.4-patch.1 → 0.20.3-build.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +154 -121
- package/dist/1.app.js +1 -1
- package/dist/app.css +1 -38
- package/dist/app.css.map +1 -1
- package/dist/app.js +8 -8
- package/lib/dynamic-option-list.js +2 -1
- package/lib/form-elements/component-drag-handle.js +48 -72
- package/lib/form-elements/component-drag-layer.js +18 -17
- package/lib/form-elements/component-drag-preview.js +1 -9
- package/lib/form-elements/header-bar.js +2 -2
- package/lib/form-elements/index.js +2 -2
- package/lib/form-elements/star-rating.js +90 -78
- package/lib/form-elements-edit.js +2 -3
- package/lib/form.js +9 -10
- package/lib/multi-column/dustbin.js +58 -71
- package/lib/sortable-element.js +166 -139
- package/lib/toolbar-draggable-item.js +41 -45
- package/lib/toolbar.js +1 -1
- package/package.json +26 -34
package/lib/form.js
CHANGED
|
@@ -14,7 +14,6 @@ var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime
|
|
|
14
14
|
var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));
|
|
15
15
|
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
|
|
16
16
|
var _react = _interopRequireDefault(require("react"));
|
|
17
|
-
var _reactDom = _interopRequireDefault(require("react-dom"));
|
|
18
17
|
var _fbemitter = require("fbemitter");
|
|
19
18
|
var _reactIntl = require("react-intl");
|
|
20
19
|
var _formValidator = _interopRequireDefault(require("./form-validator"));
|
|
@@ -24,7 +23,7 @@ var _fieldset = require("./fieldset");
|
|
|
24
23
|
var _customElement = _interopRequireDefault(require("./form-elements/custom-element"));
|
|
25
24
|
var _registry = _interopRequireDefault(require("./stores/registry"));
|
|
26
25
|
function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = (0, _getPrototypeOf2["default"])(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = (0, _getPrototypeOf2["default"])(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return (0, _possibleConstructorReturn2["default"])(this, result); }; }
|
|
27
|
-
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } } /**
|
|
26
|
+
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } } /* eslint-disable arrow-body-style */ /* eslint-disable camelcase */ /**
|
|
28
27
|
* <Form />
|
|
29
28
|
*/
|
|
30
29
|
var Image = _formElements["default"].Image,
|
|
@@ -134,7 +133,7 @@ var ReactForm = /*#__PURE__*/function (_React$Component) {
|
|
|
134
133
|
} else if (item.element === 'FileUpload') {
|
|
135
134
|
$item.value = ref.state.fileUpload;
|
|
136
135
|
} else if (ref && ref.inputField && ref.inputField.current) {
|
|
137
|
-
$item =
|
|
136
|
+
$item = ref.inputField.current;
|
|
138
137
|
if (trimValue && $item && typeof $item.value === 'string') {
|
|
139
138
|
$item.value = $item.value.trim();
|
|
140
139
|
}
|
|
@@ -149,7 +148,7 @@ var ReactForm = /*#__PURE__*/function (_React$Component) {
|
|
|
149
148
|
var ref = this.inputs[item.field_name];
|
|
150
149
|
if (item.element === 'Checkboxes' || item.element === 'RadioButtons') {
|
|
151
150
|
item.options.forEach(function (option) {
|
|
152
|
-
var $option =
|
|
151
|
+
var $option = ref.options["child_ref_".concat(option.key)];
|
|
153
152
|
if (option.hasOwnProperty('correct') && !$option.checked || !option.hasOwnProperty('correct') && $option.checked) {
|
|
154
153
|
incorrect = true;
|
|
155
154
|
}
|
|
@@ -176,7 +175,7 @@ var ReactForm = /*#__PURE__*/function (_React$Component) {
|
|
|
176
175
|
if (item.element === 'Checkboxes' || item.element === 'RadioButtons') {
|
|
177
176
|
var checked_options = 0;
|
|
178
177
|
item.options.forEach(function (option) {
|
|
179
|
-
var $option =
|
|
178
|
+
var $option = ref.options["child_ref_".concat(option.key)];
|
|
180
179
|
if ($option.checked) {
|
|
181
180
|
checked_options += 1;
|
|
182
181
|
}
|
|
@@ -212,7 +211,7 @@ var ReactForm = /*#__PURE__*/function (_React$Component) {
|
|
|
212
211
|
if (item.element === 'Checkboxes' || item.element === 'RadioButtons') {
|
|
213
212
|
var checked_options = [];
|
|
214
213
|
item.options.forEach(function (option) {
|
|
215
|
-
var $option =
|
|
214
|
+
var $option = ref.options["child_ref_".concat(option.key)];
|
|
216
215
|
if ($option.checked) {
|
|
217
216
|
checked_options.push(_this3._getOptionKeyValue(option));
|
|
218
217
|
}
|
|
@@ -245,7 +244,7 @@ var ReactForm = /*#__PURE__*/function (_React$Component) {
|
|
|
245
244
|
if ($canvas_sig) {
|
|
246
245
|
var base64 = $canvas_sig.toDataURL().replace('data:image/png;base64,', '');
|
|
247
246
|
var isEmpty = $canvas_sig.isEmpty();
|
|
248
|
-
var $input_sig =
|
|
247
|
+
var $input_sig = ref.inputField.current;
|
|
249
248
|
if (isEmpty) {
|
|
250
249
|
$input_sig.value = '';
|
|
251
250
|
} else {
|
|
@@ -271,14 +270,14 @@ var ReactForm = /*#__PURE__*/function (_React$Component) {
|
|
|
271
270
|
var data = this._collectFormData(this.props.data, true);
|
|
272
271
|
onSubmit(data);
|
|
273
272
|
} else {
|
|
274
|
-
var $form =
|
|
273
|
+
var $form = this.form;
|
|
275
274
|
$form.submit();
|
|
276
275
|
}
|
|
277
276
|
}
|
|
278
277
|
}
|
|
279
278
|
}, {
|
|
280
279
|
key: "handleBlur",
|
|
281
|
-
value: function handleBlur(
|
|
280
|
+
value: function handleBlur() {
|
|
282
281
|
// Call submit function on blur
|
|
283
282
|
if (this.props.onBlur) {
|
|
284
283
|
var onBlur = this.props.onBlur;
|
|
@@ -288,7 +287,7 @@ var ReactForm = /*#__PURE__*/function (_React$Component) {
|
|
|
288
287
|
}
|
|
289
288
|
}, {
|
|
290
289
|
key: "handleChange",
|
|
291
|
-
value: function handleChange(
|
|
290
|
+
value: function handleChange() {
|
|
292
291
|
// Call submit function on change
|
|
293
292
|
if (this.props.onChange) {
|
|
294
293
|
var onChange = this.props.onChange;
|
|
@@ -6,6 +6,7 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
6
6
|
value: true
|
|
7
7
|
});
|
|
8
8
|
exports["default"] = void 0;
|
|
9
|
+
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
|
|
9
10
|
var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));
|
|
10
11
|
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
|
|
11
12
|
var _react = _interopRequireWildcard(require("react"));
|
|
@@ -15,7 +16,7 @@ var _ItemTypes = _interopRequireDefault(require("../ItemTypes"));
|
|
|
15
16
|
var _customElement = _interopRequireDefault(require("../form-elements/custom-element"));
|
|
16
17
|
var _registry = _interopRequireDefault(require("../stores/registry"));
|
|
17
18
|
var _store = _interopRequireDefault(require("../stores/store"));
|
|
18
|
-
var _excluded = ["onDropSuccess", "seq", "
|
|
19
|
+
var _excluded = ["onDropSuccess", "seq", "parentIndex", "items", "col", "getDataById", "accepts", "data", "setAsChild"];
|
|
19
20
|
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
|
20
21
|
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
|
21
22
|
function getCustomElement(item, props) {
|
|
@@ -67,90 +68,76 @@ function isContainer(item) {
|
|
|
67
68
|
}
|
|
68
69
|
return false;
|
|
69
70
|
}
|
|
70
|
-
var Dustbin =
|
|
71
|
+
var Dustbin = function Dustbin(_ref) {
|
|
72
|
+
var _draggedItem$data;
|
|
71
73
|
var onDropSuccess = _ref.onDropSuccess,
|
|
72
74
|
seq = _ref.seq,
|
|
73
|
-
draggedItem = _ref.draggedItem,
|
|
74
75
|
parentIndex = _ref.parentIndex,
|
|
75
|
-
canDrop = _ref.canDrop,
|
|
76
|
-
isOver = _ref.isOver,
|
|
77
|
-
isOverCurrent = _ref.isOverCurrent,
|
|
78
|
-
connectDropTarget = _ref.connectDropTarget,
|
|
79
76
|
items = _ref.items,
|
|
80
77
|
col = _ref.col,
|
|
81
78
|
getDataById = _ref.getDataById,
|
|
79
|
+
accepts = _ref.accepts,
|
|
80
|
+
data = _ref.data,
|
|
81
|
+
setAsChild = _ref.setAsChild,
|
|
82
82
|
rest = (0, _objectWithoutProperties2["default"])(_ref, _excluded);
|
|
83
|
+
var dropRef = (0, _react.useRef)(null);
|
|
83
84
|
var item = getDataById(items[col]);
|
|
84
|
-
(0,
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
85
|
+
var _useDrop = (0, _reactDnd.useDrop)({
|
|
86
|
+
accept: accepts,
|
|
87
|
+
collect: function collect(monitor) {
|
|
88
|
+
return {
|
|
89
|
+
isOver: monitor.isOver(),
|
|
90
|
+
canDrop: monitor.canDrop(),
|
|
91
|
+
draggedItem: monitor.getItem()
|
|
92
|
+
};
|
|
93
|
+
},
|
|
94
|
+
drop: function drop(droppedItem) {
|
|
95
|
+
// Do nothing when moving the box inside the same column
|
|
96
|
+
if (col === droppedItem.col && items[col] === droppedItem.id) return;
|
|
97
|
+
|
|
98
|
+
// Do not allow replace component other than both items in same multi column row
|
|
99
|
+
if (droppedItem.col === undefined && items[col]) {
|
|
100
|
+
_store["default"].dispatch('resetLastItem');
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
if (!isContainer(droppedItem)) {
|
|
104
|
+
console.log("Item dropped", droppedItem);
|
|
105
|
+
var isBusy = !!items[col];
|
|
106
|
+
if (droppedItem.data) {
|
|
107
|
+
var isNew = !droppedItem.data.id;
|
|
108
|
+
var itemData = isNew ? droppedItem.onCreate(droppedItem.data) : droppedItem.data;
|
|
109
|
+
if (typeof setAsChild === 'function') {
|
|
110
|
+
setAsChild(data, itemData, col, isBusy);
|
|
111
|
+
}
|
|
112
|
+
onDropSuccess && onDropSuccess();
|
|
113
|
+
_store["default"].dispatch('deleteLastItem');
|
|
114
|
+
}
|
|
92
115
|
}
|
|
116
|
+
},
|
|
117
|
+
canDrop: function canDrop(item) {
|
|
118
|
+
// Add any custom logic for when an item can be dropped
|
|
119
|
+
return true;
|
|
93
120
|
}
|
|
94
|
-
}
|
|
95
|
-
|
|
121
|
+
}),
|
|
122
|
+
_useDrop2 = (0, _slicedToArray2["default"])(_useDrop, 2),
|
|
123
|
+
_useDrop2$ = _useDrop2[0],
|
|
124
|
+
isOver = _useDrop2$.isOver,
|
|
125
|
+
canDrop = _useDrop2$.canDrop,
|
|
126
|
+
draggedItem = _useDrop2$.draggedItem,
|
|
127
|
+
drop = _useDrop2[1];
|
|
96
128
|
var element = getElement(item, rest);
|
|
97
129
|
var sameCard = draggedItem ? draggedItem.index === parentIndex : false;
|
|
98
|
-
|
|
99
|
-
// console.log('dragIndex:',draggedItem?.index)
|
|
100
|
-
// console.log('HoverIndex:',parentIndex)
|
|
101
|
-
// console.log('SameCard:',sameCard)
|
|
102
|
-
|
|
103
130
|
var backgroundColor = 'rgba(0, 0, 0, .03)';
|
|
104
|
-
if (!sameCard && isOver && canDrop && !draggedItem.data.isContainer) {
|
|
131
|
+
if (!sameCard && isOver && canDrop && draggedItem && !((_draggedItem$data = draggedItem.data) !== null && _draggedItem$data !== void 0 && _draggedItem$data.isContainer)) {
|
|
105
132
|
backgroundColor = '#F7F589';
|
|
106
133
|
}
|
|
107
134
|
|
|
108
|
-
//
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
drop: function drop(props, monitor, component) {
|
|
117
|
-
if (!component) {
|
|
118
|
-
return;
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
// //Do nothing whith busy dustbin
|
|
122
|
-
// if(props.items[props.col]) return;
|
|
123
|
-
// Allow swap column if target and source are in same multi column row
|
|
124
|
-
var isBusy = !!props.items[props.col];
|
|
125
|
-
var item = monitor.getItem();
|
|
126
|
-
|
|
127
|
-
// Do nothing when moving the box inside the same column
|
|
128
|
-
if (props.col === item.col && props.items[props.col] === item.id) return;
|
|
129
|
-
|
|
130
|
-
// Do not allow replace component other than both items in same multi column row
|
|
131
|
-
if (item.col === undefined && props.items[props.col]) {
|
|
132
|
-
_store["default"].dispatch('resetLastItem');
|
|
133
|
-
return;
|
|
134
|
-
}
|
|
135
|
-
if (!isContainer(item)) {
|
|
136
|
-
component.onDrop(item);
|
|
137
|
-
console.log("calling on Drop from 137", item);
|
|
138
|
-
if (item.data && typeof props.setAsChild === 'function') {
|
|
139
|
-
var isNew = !item.data.id;
|
|
140
|
-
var data = isNew ? item.onCreate(item.data) : item.data;
|
|
141
|
-
props.setAsChild(props.data, data, props.col, isBusy);
|
|
142
|
-
}
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
}, function (connect, monitor) {
|
|
146
|
-
return {
|
|
147
|
-
connectDropTarget: connect.dropTarget(),
|
|
148
|
-
draggedItem: monitor.getItem() ? monitor.getItem() : null,
|
|
149
|
-
isOver: monitor.isOver(),
|
|
150
|
-
isOverCurrent: monitor.isOver({
|
|
151
|
-
shallow: true
|
|
152
|
-
}),
|
|
153
|
-
canDrop: monitor.canDrop()
|
|
154
|
-
};
|
|
155
|
-
})(Dustbin);
|
|
135
|
+
// Connect the drop ref to the DOM element
|
|
136
|
+
drop(dropRef);
|
|
137
|
+
return /*#__PURE__*/_react["default"].createElement("div", {
|
|
138
|
+
ref: dropRef,
|
|
139
|
+
style: !sameCard ? getStyle(backgroundColor) : getStyle('rgba(0, 0, 0, .03)')
|
|
140
|
+
}, !element && /*#__PURE__*/_react["default"].createElement("span", null, "Drop your element here"), element);
|
|
141
|
+
};
|
|
142
|
+
var _default = Dustbin;
|
|
156
143
|
exports["default"] = _default;
|
package/lib/sortable-element.js
CHANGED
|
@@ -5,25 +5,20 @@ var _typeof = require("@babel/runtime/helpers/typeof");
|
|
|
5
5
|
Object.defineProperty(exports, "__esModule", {
|
|
6
6
|
value: true
|
|
7
7
|
});
|
|
8
|
-
exports["default"] =
|
|
8
|
+
exports["default"] = createDraggableCard;
|
|
9
9
|
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
|
|
10
|
-
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
|
|
11
|
-
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
|
|
12
|
-
var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));
|
|
13
|
-
var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn"));
|
|
14
|
-
var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));
|
|
15
10
|
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
|
|
11
|
+
var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));
|
|
12
|
+
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
|
|
16
13
|
var _react = _interopRequireWildcard(require("react"));
|
|
17
14
|
var _propTypes = _interopRequireDefault(require("prop-types"));
|
|
18
|
-
var _reactDom = require("react-dom");
|
|
19
15
|
var _reactDnd = require("react-dnd");
|
|
20
16
|
var _ItemTypes = _interopRequireDefault(require("./ItemTypes"));
|
|
17
|
+
var _excluded = ["index", "id", "moveCard", "seq"];
|
|
21
18
|
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
|
22
19
|
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
|
23
20
|
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
|
|
24
21
|
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { (0, _defineProperty2["default"])(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
|
|
25
|
-
function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = (0, _getPrototypeOf2["default"])(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = (0, _getPrototypeOf2["default"])(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return (0, _possibleConstructorReturn2["default"])(this, result); }; }
|
|
26
|
-
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
|
|
27
22
|
var style = {
|
|
28
23
|
border: '1px dashed gray',
|
|
29
24
|
padding: '0.5rem 1rem',
|
|
@@ -31,147 +26,179 @@ var style = {
|
|
|
31
26
|
backgroundColor: 'white',
|
|
32
27
|
cursor: 'pointer'
|
|
33
28
|
};
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
return;
|
|
54
|
-
}
|
|
55
|
-
if (item.data && typeof item.setAsChild === 'function') {
|
|
56
|
-
// console.log('BOX', item);
|
|
57
|
-
if (dragIndex === -1) {
|
|
58
|
-
props.insertCard(item, hoverIndex, item.id);
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
},
|
|
62
|
-
hover: function hover(props, monitor, component) {
|
|
63
|
-
var _props$data, _item$data;
|
|
64
|
-
var item = monitor.getItem();
|
|
65
|
-
if (item.itemType === _ItemTypes["default"].BOX && item.index === -1) return;
|
|
66
|
-
|
|
67
|
-
// Don't replace multi-column component unless both drag & hover are multi-column
|
|
68
|
-
if ((_props$data = props.data) !== null && _props$data !== void 0 && _props$data.isContainer && !((_item$data = item.data) !== null && _item$data !== void 0 && _item$data.isContainer)) return;
|
|
69
|
-
var dragIndex = item.index;
|
|
70
|
-
var hoverIndex = props.index;
|
|
71
|
-
|
|
72
|
-
// Don't replace items with themselves
|
|
73
|
-
if (dragIndex === hoverIndex) {
|
|
74
|
-
return;
|
|
75
|
-
}
|
|
76
|
-
if (dragIndex === -1) {
|
|
77
|
-
if (props.data && props.data.isContainer) {
|
|
78
|
-
return;
|
|
29
|
+
|
|
30
|
+
// Modern approach using a custom hook for DnD logic
|
|
31
|
+
var useDragAndDrop = function useDragAndDrop(props) {
|
|
32
|
+
var _ref = (0, _react.useRef)(null);
|
|
33
|
+
|
|
34
|
+
// Setup drag
|
|
35
|
+
var _useDrag = (0, _reactDnd.useDrag)({
|
|
36
|
+
type: _ItemTypes["default"].CARD,
|
|
37
|
+
item: function item() {
|
|
38
|
+
return {
|
|
39
|
+
itemType: _ItemTypes["default"].CARD,
|
|
40
|
+
id: props.id,
|
|
41
|
+
index: props.index
|
|
42
|
+
};
|
|
43
|
+
},
|
|
44
|
+
collect: function collect(monitor) {
|
|
45
|
+
return {
|
|
46
|
+
isDragging: monitor.isDragging()
|
|
47
|
+
};
|
|
79
48
|
}
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
49
|
+
}),
|
|
50
|
+
_useDrag2 = (0, _slicedToArray2["default"])(_useDrag, 3),
|
|
51
|
+
isDragging = _useDrag2[0].isDragging,
|
|
52
|
+
drag = _useDrag2[1],
|
|
53
|
+
preview = _useDrag2[2];
|
|
84
54
|
|
|
85
|
-
|
|
86
|
-
|
|
55
|
+
// Setup drop
|
|
56
|
+
var _useDrop = (0, _reactDnd.useDrop)({
|
|
57
|
+
accept: [_ItemTypes["default"].CARD, _ItemTypes["default"].BOX],
|
|
58
|
+
drop: function drop(item) {
|
|
59
|
+
// Don't handle drops if we're a container and this is a card drop
|
|
60
|
+
if (props.data && props.data.isContainer || item.itemType === _ItemTypes["default"].CARD) {
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
var hoverIndex = props.index;
|
|
64
|
+
var dragIndex = item.index;
|
|
87
65
|
|
|
88
|
-
|
|
89
|
-
|
|
66
|
+
// Handle box drops
|
|
67
|
+
if (item.data && typeof item.setAsChild === 'function') {
|
|
68
|
+
if (dragIndex === -1) {
|
|
69
|
+
props.insertCard(item, hoverIndex, item.id);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
},
|
|
73
|
+
hover: function hover(item, monitor) {
|
|
74
|
+
var _props$data, _item$data;
|
|
75
|
+
// Don't replace items being dragged from box with index -1
|
|
76
|
+
if (item.itemType === _ItemTypes["default"].BOX && item.index === -1) return;
|
|
90
77
|
|
|
91
|
-
|
|
92
|
-
|
|
78
|
+
// Don't replace multi-column component unless both drag & hover are multi-column
|
|
79
|
+
if ((_props$data = props.data) !== null && _props$data !== void 0 && _props$data.isContainer && !((_item$data = item.data) !== null && _item$data !== void 0 && _item$data.isContainer)) return;
|
|
80
|
+
var dragIndex = item.index;
|
|
81
|
+
var hoverIndex = props.index;
|
|
93
82
|
|
|
94
|
-
|
|
95
|
-
|
|
83
|
+
// Don't replace items with themselves
|
|
84
|
+
if (dragIndex === hoverIndex) {
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
96
87
|
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
88
|
+
// Handle new items being created
|
|
89
|
+
if (dragIndex === -1) {
|
|
90
|
+
if (props.data && props.data.isContainer) {
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
item.index = hoverIndex;
|
|
94
|
+
props.insertCard(item.onCreate(item.data), hoverIndex);
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
100
97
|
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
98
|
+
// Skip if no ref available
|
|
99
|
+
if (!_ref.current) {
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
105
102
|
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
return;
|
|
109
|
-
}
|
|
103
|
+
// Determine rectangle on screen
|
|
104
|
+
var hoverBoundingRect = _ref.current.getBoundingClientRect();
|
|
110
105
|
|
|
111
|
-
|
|
112
|
-
|
|
106
|
+
// Get vertical middle
|
|
107
|
+
var hoverMiddleY = (hoverBoundingRect.bottom - hoverBoundingRect.top) / 2;
|
|
113
108
|
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
// to avoid expensive index searches.
|
|
109
|
+
// Determine mouse position
|
|
110
|
+
var clientOffset = monitor.getClientOffset();
|
|
111
|
+
if (!clientOffset) return;
|
|
118
112
|
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
113
|
+
// Get pixels to the top
|
|
114
|
+
var hoverClientY = clientOffset.y - hoverBoundingRect.top;
|
|
115
|
+
|
|
116
|
+
// Only perform the move when the mouse has crossed half of the items height
|
|
117
|
+
// When dragging downwards, only move when the cursor is below 50%
|
|
118
|
+
// When dragging upwards, only move when the cursor is above 50%
|
|
119
|
+
|
|
120
|
+
// Dragging downwards
|
|
121
|
+
if (dragIndex < hoverIndex && hoverClientY < hoverMiddleY) {
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
// Dragging upwards
|
|
126
|
+
if (dragIndex > hoverIndex && hoverClientY > hoverMiddleY) {
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
// Time to actually perform the action
|
|
131
|
+
props.moveCard(dragIndex, hoverIndex);
|
|
124
132
|
|
|
125
|
-
//
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
function Card() {
|
|
131
|
-
(0, _classCallCheck2["default"])(this, Card);
|
|
132
|
-
return _super.apply(this, arguments);
|
|
133
|
-
}
|
|
134
|
-
(0, _createClass2["default"])(Card, [{
|
|
135
|
-
key: "render",
|
|
136
|
-
value: function render() {
|
|
137
|
-
var _this$props = this.props,
|
|
138
|
-
isDragging = _this$props.isDragging,
|
|
139
|
-
connectDragPreview = _this$props.connectDragPreview,
|
|
140
|
-
connectDropTarget = _this$props.connectDropTarget;
|
|
141
|
-
var opacity = isDragging ? 0 : 1;
|
|
142
|
-
return connectDragPreview(connectDropTarget(/*#__PURE__*/_react["default"].createElement("div", null, /*#__PURE__*/_react["default"].createElement(ComposedComponent, (0, _extends2["default"])({}, this.props, {
|
|
143
|
-
style: _objectSpread(_objectSpread({}, style), {}, {
|
|
144
|
-
opacity: opacity
|
|
145
|
-
})
|
|
146
|
-
})))));
|
|
133
|
+
// Note: we're mutating the monitor item here!
|
|
134
|
+
// Generally it's better to avoid mutations,
|
|
135
|
+
// but it's good here for the sake of performance
|
|
136
|
+
// to avoid expensive index searches.
|
|
137
|
+
item.index = hoverIndex;
|
|
147
138
|
}
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
139
|
+
}),
|
|
140
|
+
_useDrop2 = (0, _slicedToArray2["default"])(_useDrop, 2),
|
|
141
|
+
drop = _useDrop2[1];
|
|
142
|
+
|
|
143
|
+
// Connect the drag and drop refs to the same element
|
|
144
|
+
return {
|
|
145
|
+
ref: function ref(node) {
|
|
146
|
+
_ref.current = node;
|
|
147
|
+
drop(node);
|
|
148
|
+
drag(node);
|
|
149
|
+
},
|
|
150
|
+
previewRef: preview,
|
|
151
|
+
isDragging: isDragging
|
|
152
|
+
};
|
|
153
|
+
};
|
|
154
|
+
|
|
155
|
+
// Modern approach using a functional component wrapper instead of HOC
|
|
156
|
+
var DraggableCard = function DraggableCard(props) {
|
|
157
|
+
var index = props.index,
|
|
158
|
+
id = props.id,
|
|
159
|
+
moveCard = props.moveCard,
|
|
160
|
+
_props$seq = props.seq,
|
|
161
|
+
seq = _props$seq === void 0 ? -1 : _props$seq,
|
|
162
|
+
restProps = (0, _objectWithoutProperties2["default"])(props, _excluded);
|
|
163
|
+
var _useDragAndDrop = useDragAndDrop(props),
|
|
164
|
+
ref = _useDragAndDrop.ref,
|
|
165
|
+
previewRef = _useDragAndDrop.previewRef,
|
|
166
|
+
isDragging = _useDragAndDrop.isDragging;
|
|
167
|
+
var opacity = isDragging ? 0 : 1;
|
|
168
|
+
|
|
169
|
+
// Use the ComposedComponent passed in props
|
|
170
|
+
var ComposedComponent = props.component;
|
|
171
|
+
return /*#__PURE__*/_react["default"].createElement("div", {
|
|
172
|
+
ref: previewRef
|
|
173
|
+
}, /*#__PURE__*/_react["default"].createElement("div", {
|
|
174
|
+
ref: ref
|
|
175
|
+
}, /*#__PURE__*/_react["default"].createElement(ComposedComponent, (0, _extends2["default"])({}, restProps, {
|
|
176
|
+
index: index,
|
|
177
|
+
id: id,
|
|
178
|
+
moveCard: moveCard,
|
|
179
|
+
seq: seq,
|
|
180
|
+
style: _objectSpread(_objectSpread({}, style), {}, {
|
|
181
|
+
opacity: opacity
|
|
182
|
+
})
|
|
183
|
+
}))));
|
|
184
|
+
};
|
|
185
|
+
DraggableCard.propTypes = {
|
|
186
|
+
component: _propTypes["default"].elementType.isRequired,
|
|
187
|
+
index: _propTypes["default"].number.isRequired,
|
|
188
|
+
isDragging: _propTypes["default"].bool,
|
|
189
|
+
id: _propTypes["default"].any.isRequired,
|
|
190
|
+
moveCard: _propTypes["default"].func.isRequired,
|
|
191
|
+
seq: _propTypes["default"].number
|
|
192
|
+
};
|
|
193
|
+
DraggableCard.defaultProps = {
|
|
194
|
+
seq: -1
|
|
195
|
+
};
|
|
196
|
+
|
|
197
|
+
// This replaces the HOC pattern with a component that takes the component as a prop
|
|
198
|
+
function createDraggableCard(ComposedComponent) {
|
|
199
|
+
return function (props) {
|
|
200
|
+
return /*#__PURE__*/_react["default"].createElement(DraggableCard, (0, _extends2["default"])({}, props, {
|
|
201
|
+
component: ComposedComponent
|
|
202
|
+
}));
|
|
203
|
+
};
|
|
177
204
|
}
|