Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 

126 строки
5.4 KiB

  1. function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
  2. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  3. function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
  4. function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
  5. function _createSuper(Derived) { return function () { var Super = _getPrototypeOf(Derived), result; if (_isNativeReflectConstruct()) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
  6. function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
  7. function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
  8. function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Date.prototype.toString.call(Reflect.construct(Date, [], function () {})); return true; } catch (e) { return false; } }
  9. function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
  10. function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }
  11. function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
  12. import React from 'react';
  13. import ReactDOM from 'react-dom';
  14. import PropTypes from 'prop-types';
  15. var ContainerRender = /*#__PURE__*/function (_React$Component) {
  16. _inherits(ContainerRender, _React$Component);
  17. var _super = _createSuper(ContainerRender);
  18. function ContainerRender() {
  19. var _this;
  20. _classCallCheck(this, ContainerRender);
  21. for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
  22. args[_key] = arguments[_key];
  23. }
  24. _this = _super.call.apply(_super, [this].concat(args));
  25. _this.removeContainer = function () {
  26. if (_this.container) {
  27. ReactDOM.unmountComponentAtNode(_this.container);
  28. _this.container.parentNode.removeChild(_this.container);
  29. _this.container = null;
  30. }
  31. };
  32. _this.renderComponent = function (props, ready) {
  33. var _this$props = _this.props,
  34. visible = _this$props.visible,
  35. getComponent = _this$props.getComponent,
  36. forceRender = _this$props.forceRender,
  37. getContainer = _this$props.getContainer,
  38. parent = _this$props.parent;
  39. if (visible || parent._component || forceRender) {
  40. if (!_this.container) {
  41. _this.container = getContainer();
  42. }
  43. ReactDOM.unstable_renderSubtreeIntoContainer(parent, getComponent(props), _this.container, function callback() {
  44. if (ready) {
  45. ready.call(this);
  46. }
  47. });
  48. }
  49. };
  50. return _this;
  51. }
  52. _createClass(ContainerRender, [{
  53. key: "componentDidMount",
  54. value: function componentDidMount() {
  55. if (this.props.autoMount) {
  56. this.renderComponent();
  57. }
  58. }
  59. }, {
  60. key: "componentDidUpdate",
  61. value: function componentDidUpdate() {
  62. if (this.props.autoMount) {
  63. this.renderComponent();
  64. }
  65. }
  66. }, {
  67. key: "componentWillUnmount",
  68. value: function componentWillUnmount() {
  69. if (this.props.autoDestroy) {
  70. this.removeContainer();
  71. }
  72. }
  73. }, {
  74. key: "render",
  75. value: function render() {
  76. return this.props.children({
  77. renderComponent: this.renderComponent,
  78. removeContainer: this.removeContainer
  79. });
  80. }
  81. }]);
  82. return ContainerRender;
  83. }(React.Component);
  84. ContainerRender.propTypes = {
  85. autoMount: PropTypes.bool,
  86. autoDestroy: PropTypes.bool,
  87. visible: PropTypes.bool,
  88. forceRender: PropTypes.bool,
  89. parent: PropTypes.any,
  90. getComponent: PropTypes.func.isRequired,
  91. getContainer: PropTypes.func.isRequired,
  92. children: PropTypes.func.isRequired
  93. };
  94. ContainerRender.defaultProps = {
  95. autoMount: true,
  96. autoDestroy: true,
  97. forceRender: false
  98. };
  99. export { ContainerRender as default };