Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 

94 linhas
3.6 KiB

  1. function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
  2. function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
  3. function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
  4. import ReactDOM from 'react-dom';
  5. function defaultGetContainer() {
  6. var container = document.createElement('div');
  7. document.body.appendChild(container);
  8. return container;
  9. }
  10. export default function getContainerRenderMixin(config) {
  11. var _config$autoMount = config.autoMount,
  12. autoMount = _config$autoMount === void 0 ? true : _config$autoMount,
  13. _config$autoDestroy = config.autoDestroy,
  14. autoDestroy = _config$autoDestroy === void 0 ? true : _config$autoDestroy,
  15. isVisible = config.isVisible,
  16. isForceRender = config.isForceRender,
  17. getComponent = config.getComponent,
  18. _config$getContainer = config.getContainer,
  19. getContainer = _config$getContainer === void 0 ? defaultGetContainer : _config$getContainer;
  20. var mixin;
  21. function _renderComponent(instance, componentArg, ready) {
  22. if (!isVisible || instance._component || isVisible(instance) || isForceRender && isForceRender(instance)) {
  23. if (!instance._container) {
  24. instance._container = getContainer(instance);
  25. }
  26. var component;
  27. if (instance.getComponent) {
  28. component = instance.getComponent(componentArg);
  29. } else {
  30. component = getComponent(instance, componentArg);
  31. }
  32. ReactDOM.unstable_renderSubtreeIntoContainer(instance, component, instance._container, function callback() {
  33. instance._component = this;
  34. if (ready) {
  35. ready.call(this);
  36. }
  37. });
  38. }
  39. }
  40. if (autoMount) {
  41. mixin = _objectSpread({}, mixin, {
  42. componentDidMount: function componentDidMount() {
  43. _renderComponent(this);
  44. },
  45. componentDidUpdate: function componentDidUpdate() {
  46. _renderComponent(this);
  47. }
  48. });
  49. }
  50. if (!autoMount || !autoDestroy) {
  51. mixin = _objectSpread({}, mixin, {
  52. renderComponent: function renderComponent(componentArg, ready) {
  53. _renderComponent(this, componentArg, ready);
  54. }
  55. });
  56. }
  57. function _removeContainer(instance) {
  58. if (instance._container) {
  59. var container = instance._container;
  60. ReactDOM.unmountComponentAtNode(container);
  61. container.parentNode.removeChild(container);
  62. instance._container = null;
  63. }
  64. }
  65. if (autoDestroy) {
  66. mixin = _objectSpread({}, mixin, {
  67. componentWillUnmount: function componentWillUnmount() {
  68. _removeContainer(this);
  69. }
  70. });
  71. } else {
  72. mixin = _objectSpread({}, mixin, {
  73. removeContainer: function removeContainer() {
  74. _removeContainer(this);
  75. }
  76. });
  77. }
  78. return mixin;
  79. }