UNPKG

rsuite-utils

Version:
106 lines (75 loc) 4.59 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _createClass = function () { 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); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); var _react = require('react'); var React = _interopRequireWildcard(_react); var _reactDom = require('react-dom'); var _reactDom2 = _interopRequireDefault(_reactDom); var _domLib = require('dom-lib'); var _reactLifecyclesCompat = require('react-lifecycles-compat'); var _shallowEqual = require('../utils/shallowEqual'); var _shallowEqual2 = _interopRequireDefault(_shallowEqual); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } /** * The `<Portal/>` component renders its children into a new "subtree" outside of * current component hierarchy. * You can think of it as a declarative `appendChild()`, or jQuery's `$.fn.appendTo()`. * The children of `<Portal/>` component will be appended to the `container` specified. */ var Portal = function (_React$Component) { _inherits(Portal, _React$Component); function Portal() { var _ref; var _temp, _this, _ret; _classCallCheck(this, Portal); for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } return _ret = (_temp = (_this = _possibleConstructorReturn(this, (_ref = Portal.__proto__ || Object.getPrototypeOf(Portal)).call.apply(_ref, [this].concat(args))), _this), _this.setContainer = function () { var props = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : _this.props; _this.portalContainerNode = (0, _domLib.getContainer)(props.container, (0, _domLib.ownerDocument)(_this).body); }, _this.getMountNode = function () { return _this.portalContainerNode; }, _this.portalContainerNode = null, _temp), _possibleConstructorReturn(_this, _ret); } _createClass(Portal, [{ key: 'componentDidMount', value: function componentDidMount() { this.setContainer(); this.forceUpdate(this.props.onRendered); } }, { key: 'shouldComponentUpdate', value: function shouldComponentUpdate(nextProps) { if (nextProps.container !== this.props.container) { this.setContainer(); } if (!(0, _shallowEqual2.default)(nextProps, this.props)) { return true; } return false; } }, { key: 'componentWillUnmount', value: function componentWillUnmount() { this.portalContainerNode = null; } }, { key: 'render', value: function render() { var children = this.props.children; return children && this.portalContainerNode ? _reactDom2.default.createPortal(children, this.portalContainerNode) : null; } }]); return Portal; }(React.Component); Portal.displayName = 'Portal'; Portal.handledProps = ['children', 'container', 'onRendered']; (0, _reactLifecyclesCompat.polyfill)(Portal); exports.default = Portal;