UNPKG

react-dnd

Version:
89 lines 4.32 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); var __assign = (this && this.__assign) || Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); var react_1 = __importDefault(require("react")); var dnd_core_1 = require("dnd-core"); var invariant_1 = __importDefault(require("invariant")); var hoist_non_react_statics_1 = __importDefault(require("hoist-non-react-statics")); var checkDecoratorArguments_1 = __importDefault(require("./utils/checkDecoratorArguments")); /** * Create the React Context */ exports.Consumer = (_a = react_1.default.createContext({ dragDropManager: undefined }), _a.Consumer), exports.Provider = _a.Provider; /** * Creates the context object we're providing * @param backend * @param context */ function createChildContext(backend, context) { return { dragDropManager: dnd_core_1.createDragDropManager(backend, context), }; } exports.createChildContext = createChildContext; /** * A React component that provides the React-DnD context */ exports.DragDropContextProvider = function (_a) { var backend = _a.backend, context = _a.context, children = _a.children; var contextValue = createChildContext(backend, context); return react_1.default.createElement(exports.Provider, { value: contextValue }, children); }; /** * Wrap the root component of your application with DragDropContext decorator to set up React DnD. * This lets you specify the backend, and sets up the shared DnD state behind the scenes. * @param backendFactory The DnD backend factory * @param backendContext The backend context */ function DragDropContext(backendFactory, backendContext) { checkDecoratorArguments_1.default('DragDropContext', 'backend', backendFactory); var childContext = createChildContext(backendFactory, backendContext); return function decorateContext(DecoratedComponent) { var displayName = DecoratedComponent.displayName || DecoratedComponent.name || 'Component'; var DragDropContextContainer = /** @class */ (function (_super) { __extends(DragDropContextContainer, _super); function DragDropContextContainer() { return _super !== null && _super.apply(this, arguments) || this; } DragDropContextContainer.prototype.getDecoratedComponentInstance = function () { invariant_1.default(this.child, 'In order to access an instance of the decorated component it can not be a stateless component.'); return this.child; }; DragDropContextContainer.prototype.getManager = function () { return childContext.dragDropManager; }; DragDropContextContainer.prototype.render = function () { var _this = this; return (react_1.default.createElement(exports.Provider, { value: childContext }, react_1.default.createElement(DecoratedComponent, __assign({}, this.props, { ref: function (child) { return (_this.child = child); } })))); }; DragDropContextContainer.DecoratedComponent = DecoratedComponent; DragDropContextContainer.displayName = "DragDropContext(" + displayName + ")"; return DragDropContextContainer; }(react_1.default.Component)); return hoist_non_react_statics_1.default(DragDropContextContainer, DecoratedComponent); }; } exports.DragDropContext = DragDropContext; var _a; //# sourceMappingURL=DragDropContext.js.map