getContainerRenderMixin.js 3.0 KB

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