UNPKG

3.06 kBJavaScriptView Raw
1"use strict";
2
3var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
4Object.defineProperty(exports, "__esModule", {
5 value: true
6});
7exports.default = getContainerRenderMixin;
8var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));
9var _reactDom = _interopRequireDefault(require("react-dom"));
10function defaultGetContainer() {
11 var container = document.createElement('div');
12 document.body.appendChild(container);
13 return container;
14}
15function getContainerRenderMixin(config) {
16 var _config$autoMount = config.autoMount,
17 autoMount = _config$autoMount === void 0 ? true : _config$autoMount,
18 _config$autoDestroy = config.autoDestroy,
19 autoDestroy = _config$autoDestroy === void 0 ? true : _config$autoDestroy,
20 isVisible = config.isVisible,
21 isForceRender = config.isForceRender,
22 getComponent = config.getComponent,
23 _config$getContainer = config.getContainer,
24 getContainer = _config$getContainer === void 0 ? defaultGetContainer : _config$getContainer;
25 var mixin;
26 function _renderComponent(instance, componentArg, ready) {
27 if (!isVisible || instance._component || isVisible(instance) || isForceRender && isForceRender(instance)) {
28 if (!instance._container) {
29 instance._container = getContainer(instance);
30 }
31 var component;
32 if (instance.getComponent) {
33 component = instance.getComponent(componentArg);
34 } else {
35 component = getComponent(instance, componentArg);
36 }
37 _reactDom.default.unstable_renderSubtreeIntoContainer(instance, component, instance._container, function callback() {
38 instance._component = this;
39 if (ready) {
40 ready.call(this);
41 }
42 });
43 }
44 }
45 if (autoMount) {
46 mixin = (0, _objectSpread2.default)((0, _objectSpread2.default)({}, mixin), {}, {
47 componentDidMount: function componentDidMount() {
48 _renderComponent(this);
49 },
50 componentDidUpdate: function componentDidUpdate() {
51 _renderComponent(this);
52 }
53 });
54 }
55 if (!autoMount || !autoDestroy) {
56 mixin = (0, _objectSpread2.default)((0, _objectSpread2.default)({}, mixin), {}, {
57 renderComponent: function renderComponent(componentArg, ready) {
58 _renderComponent(this, componentArg, ready);
59 }
60 });
61 }
62 function _removeContainer(instance) {
63 if (instance._container) {
64 var container = instance._container;
65 _reactDom.default.unmountComponentAtNode(container);
66 container.parentNode.removeChild(container);
67 instance._container = null;
68 }
69 }
70 if (autoDestroy) {
71 mixin = (0, _objectSpread2.default)((0, _objectSpread2.default)({}, mixin), {}, {
72 componentWillUnmount: function componentWillUnmount() {
73 _removeContainer(this);
74 }
75 });
76 } else {
77 mixin = (0, _objectSpread2.default)((0, _objectSpread2.default)({}, mixin), {}, {
78 removeContainer: function removeContainer() {
79 _removeContainer(this);
80 }
81 });
82 }
83 return mixin;
84}
\No newline at end of file