Dashboard sipadu mbip
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

WaitForContainer.js 2.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
  2. function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; subClass.__proto__ = superClass; }
  3. import PropTypes from 'prop-types';
  4. import componentOrElement from 'prop-types-extra/lib/componentOrElement';
  5. import canUseDom from 'dom-helpers/util/inDOM';
  6. import ownerDocument from 'dom-helpers/ownerDocument';
  7. import React from 'react';
  8. import ReactDOM from 'react-dom';
  9. import getContainer from './utils/getContainer';
  10. var propTypes = {
  11. /**
  12. * A Node, Component instance, or function that returns either. The `container` will have the Portal children
  13. * appended to it.
  14. */
  15. container: PropTypes.oneOfType([componentOrElement, PropTypes.func]),
  16. onContainerResolved: PropTypes.func
  17. };
  18. var WaitForContainer =
  19. /*#__PURE__*/
  20. function (_React$Component) {
  21. _inheritsLoose(WaitForContainer, _React$Component);
  22. function WaitForContainer() {
  23. var _this;
  24. for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
  25. args[_key] = arguments[_key];
  26. }
  27. _this = _React$Component.call.apply(_React$Component, [this].concat(args)) || this;
  28. if (!canUseDom) return _assertThisInitialized(_this);
  29. var container = _this.props.container;
  30. if (typeof container === 'function') container = container();
  31. if (container && !ReactDOM.findDOMNode(container)) {
  32. // The container is a React component that has not yet been rendered.
  33. // Don't set the container node yet.
  34. return _assertThisInitialized(_this);
  35. }
  36. _this.setContainer(container);
  37. return _this;
  38. }
  39. var _proto = WaitForContainer.prototype;
  40. _proto.UNSAFE_componentWillReceiveProps = function UNSAFE_componentWillReceiveProps(nextProps) {
  41. if (nextProps.container !== this.props.container) {
  42. this.setContainer(nextProps.container);
  43. }
  44. };
  45. _proto.componentDidMount = function componentDidMount() {
  46. if (!this._container) {
  47. this.setContainer(this.props.container);
  48. this.forceUpdate(this.props.onContainerResolved);
  49. } else if (this.props.onContainerResolved) {
  50. this.props.onContainerResolved();
  51. }
  52. };
  53. _proto.componentWillUnmount = function componentWillUnmount() {
  54. this._container = null;
  55. };
  56. _proto.setContainer = function setContainer(container) {
  57. this._container = getContainer(container, ownerDocument().body);
  58. };
  59. _proto.render = function render() {
  60. return this._container ? this.props.children(this._container) : null;
  61. };
  62. return WaitForContainer;
  63. }(React.Component);
  64. WaitForContainer.propTypes = propTypes;
  65. export default WaitForContainer;