-
Notifications
You must be signed in to change notification settings - Fork 190
/
Copy pathgetContainerRenderMixin.jsx
89 lines (80 loc) · 1.94 KB
/
getContainerRenderMixin.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import ReactDOM from 'react-dom';
function defaultGetContainer() {
const container = document.createElement('div');
document.body.appendChild(container);
return container;
}
export default function getContainerRenderMixin(config) {
const {
autoMount = true,
autoDestroy = true,
isVisible,
getComponent,
getContainer = defaultGetContainer,
} = config;
let mixin;
function renderComponent(instance, componentArg, ready) {
if (!isVisible || instance._component || isVisible(instance)) {
if (!instance._container) {
instance._container = getContainer(instance);
}
let component;
if (instance.getComponent) {
component = instance.getComponent(componentArg);
} else {
component = getComponent(instance, componentArg);
}
ReactDOM.unstable_renderSubtreeIntoContainer(instance,
component, instance._container,
function callback() {
instance._component = this;
if (ready) {
ready.call(this);
}
});
}
}
if (autoMount) {
mixin = {
...mixin,
componentDidMount() {
renderComponent(this);
},
componentDidUpdate() {
renderComponent(this);
},
};
}
if (!autoMount || !autoDestroy) {
mixin = {
...mixin,
renderComponent(componentArg, ready) {
renderComponent(this, componentArg, ready);
},
};
}
function removeContainer(instance) {
if (instance._container) {
const container = instance._container;
ReactDOM.unmountComponentAtNode(container);
container.parentNode.removeChild(container);
instance._container = null;
}
}
if (autoDestroy) {
mixin = {
...mixin,
componentWillUnmount() {
removeContainer(this);
},
};
} else {
mixin = {
...mixin,
removeContainer() {
removeContainer(this);
},
};
}
return mixin;
}