UNPKG

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