All checks were successful
Publish To Prod / deploy_and_publish (push) Successful in 35s
93 lines
3.1 KiB
JavaScript
93 lines
3.1 KiB
JavaScript
"use strict";
|
|
|
|
Object.defineProperty(exports, "__esModule", {
|
|
value: true
|
|
});
|
|
exports.wrapConnectorHooks = wrapConnectorHooks;
|
|
|
|
var _invariant = require("@react-dnd/invariant");
|
|
|
|
var _react = require("react");
|
|
|
|
function throwIfCompositeComponentElement(element) {
|
|
// Custom components can no longer be wrapped directly in React DnD 2.0
|
|
// so that we don't need to depend on findDOMNode() from react-dom.
|
|
if (typeof element.type === 'string') {
|
|
return;
|
|
}
|
|
|
|
var displayName = element.type.displayName || element.type.name || 'the component';
|
|
throw new Error('Only native element nodes can now be passed to React DnD connectors.' + "You can either wrap ".concat(displayName, " into a <div>, or turn it into a ") + 'drag source or a drop target itself.');
|
|
}
|
|
|
|
function wrapHookToRecognizeElement(hook) {
|
|
return function () {
|
|
var elementOrNode = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null;
|
|
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
|
|
|
|
// When passed a node, call the hook straight away.
|
|
if (!(0, _react.isValidElement)(elementOrNode)) {
|
|
var node = elementOrNode;
|
|
hook(node, options); // return the node so it can be chained (e.g. when within callback refs
|
|
// <div ref={node => connectDragSource(connectDropTarget(node))}/>
|
|
|
|
return node;
|
|
} // If passed a ReactElement, clone it and attach this function as a ref.
|
|
// This helps us achieve a neat API where user doesn't even know that refs
|
|
// are being used under the hood.
|
|
|
|
|
|
var element = elementOrNode;
|
|
throwIfCompositeComponentElement(element); // When no options are passed, use the hook directly
|
|
|
|
var ref = options ? function (node) {
|
|
return hook(node, options);
|
|
} : hook;
|
|
return cloneWithRef(element, ref);
|
|
};
|
|
}
|
|
|
|
function wrapConnectorHooks(hooks) {
|
|
var wrappedHooks = {};
|
|
Object.keys(hooks).forEach(function (key) {
|
|
var hook = hooks[key]; // ref objects should be passed straight through without wrapping
|
|
|
|
if (key.endsWith('Ref')) {
|
|
wrappedHooks[key] = hooks[key];
|
|
} else {
|
|
var wrappedHook = wrapHookToRecognizeElement(hook);
|
|
|
|
wrappedHooks[key] = function () {
|
|
return wrappedHook;
|
|
};
|
|
}
|
|
});
|
|
return wrappedHooks;
|
|
}
|
|
|
|
function setRef(ref, node) {
|
|
if (typeof ref === 'function') {
|
|
ref(node);
|
|
} else {
|
|
ref.current = node;
|
|
}
|
|
}
|
|
|
|
function cloneWithRef(element, newRef) {
|
|
var previousRef = element.ref;
|
|
(0, _invariant.invariant)(typeof previousRef !== 'string', 'Cannot connect React DnD to an element with an existing string ref. ' + 'Please convert it to use a callback ref instead, or wrap it into a <span> or <div>. ' + 'Read more: https://reactjs.org/docs/refs-and-the-dom.html#callback-refs');
|
|
|
|
if (!previousRef) {
|
|
// When there is no ref on the element, use the new ref directly
|
|
return (0, _react.cloneElement)(element, {
|
|
ref: newRef
|
|
});
|
|
} else {
|
|
return (0, _react.cloneElement)(element, {
|
|
ref: function ref(node) {
|
|
setRef(previousRef, node);
|
|
setRef(newRef, node);
|
|
}
|
|
});
|
|
}
|
|
} |