UNPKG

react-dnd

Version:
49 lines 2.23 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); var react_1 = require("react"); var cloneWithRef_1 = __importDefault(require("./utils/cloneWithRef")); 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 " + displayName + " into a <div>, or turn it into a ") + 'drag source or a drop target itself.'); } function wrapHookToRecognizeElement(hook) { return function (elementOrNode, options) { if (elementOrNode === void 0) { elementOrNode = null; } if (options === void 0) { options = null; } // When passed a node, call the hook straight away. if (!react_1.isValidElement(elementOrNode)) { var node = elementOrNode; hook(node, options); return undefined; } // 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_1.default(element, ref); }; } function wrapConnectorHooks(hooks) { var wrappedHooks = {}; Object.keys(hooks).forEach(function (key) { var hook = hooks[key]; var wrappedHook = wrapHookToRecognizeElement(hook); wrappedHooks[key] = function () { return wrappedHook; }; }); return wrappedHooks; } exports.default = wrapConnectorHooks; //# sourceMappingURL=wrapConnectorHooks.js.map