|
- 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; }
-
- 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; }
-
- 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; }
-
- 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); }
-
- function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a 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); } }
-
- function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
-
- 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); }; }
-
- function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
-
- function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
-
- 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; } }
-
- function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
-
- 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); }
-
- function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
-
- /* eslint-disable no-underscore-dangle,react/require-default-props */
- import React from 'react';
- import ReactDOM from 'react-dom';
- import PropTypes from 'prop-types';
- import { polyfill } from 'react-lifecycles-compat';
- import ContainerRender from './ContainerRender';
- import Portal from './Portal';
- import switchScrollingEffect from './switchScrollingEffect';
- import setStyle from './setStyle';
- var openCount = 0;
- var windowIsUndefined = !(typeof window !== 'undefined' && window.document && window.document.createElement);
- var IS_REACT_16 = ('createPortal' in ReactDOM); // https://github.com/ant-design/ant-design/issues/19340
- // https://github.com/ant-design/ant-design/issues/19332
-
- var cacheOverflow = {};
-
- var PortalWrapper = /*#__PURE__*/function (_React$Component) {
- _inherits(PortalWrapper, _React$Component);
-
- var _super = _createSuper(PortalWrapper);
-
- function PortalWrapper(props) {
- var _this;
-
- _classCallCheck(this, PortalWrapper);
-
- _this = _super.call(this, props);
-
- _this.getParent = function () {
- var getContainer = _this.props.getContainer;
-
- if (getContainer) {
- if (typeof getContainer === 'string') {
- return document.querySelectorAll(getContainer)[0];
- }
-
- if (typeof getContainer === 'function') {
- return getContainer();
- }
-
- if (_typeof(getContainer) === 'object' && getContainer instanceof window.HTMLElement) {
- return getContainer;
- }
- }
-
- return document.body;
- };
-
- _this.getContainer = function () {
- if (windowIsUndefined) {
- return null;
- }
-
- if (!_this.container) {
- _this.container = document.createElement('div');
-
- var parent = _this.getParent();
-
- if (parent) {
- parent.appendChild(_this.container);
- }
- }
-
- _this.setWrapperClassName();
-
- return _this.container;
- };
-
- _this.setWrapperClassName = function () {
- var wrapperClassName = _this.props.wrapperClassName;
-
- if (_this.container && wrapperClassName && wrapperClassName !== _this.container.className) {
- _this.container.className = wrapperClassName;
- }
- };
-
- _this.savePortal = function (c) {
- // Warning: don't rename _component
- // https://github.com/react-component/util/pull/65#discussion_r352407916
- _this._component = c;
- };
-
- _this.removeCurrentContainer = function (visible) {
- _this.container = null;
- _this._component = null;
-
- if (!IS_REACT_16) {
- if (visible) {
- _this.renderComponent({
- afterClose: _this.removeContainer,
- onClose: function onClose() {},
- visible: false
- });
- } else {
- _this.removeContainer();
- }
- }
- };
-
- _this.switchScrollingEffect = function () {
- if (openCount === 1 && !Object.keys(cacheOverflow).length) {
- switchScrollingEffect(); // Must be set after switchScrollingEffect
-
- cacheOverflow = setStyle({
- overflow: 'hidden',
- overflowX: 'hidden',
- overflowY: 'hidden'
- });
- } else if (!openCount) {
- setStyle(cacheOverflow);
- cacheOverflow = {};
- switchScrollingEffect(true);
- }
- };
-
- var _visible = props.visible;
- openCount = _visible ? openCount + 1 : openCount;
- _this.state = {
- _self: _assertThisInitialized(_this)
- };
- return _this;
- }
-
- _createClass(PortalWrapper, [{
- key: "componentDidUpdate",
- value: function componentDidUpdate() {
- this.setWrapperClassName();
- }
- }, {
- key: "componentWillUnmount",
- value: function componentWillUnmount() {
- var visible = this.props.visible; // 离开时不会 render, 导到离开时数值不变,改用 func 。。
-
- openCount = visible && openCount ? openCount - 1 : openCount;
- this.removeCurrentContainer(visible);
- }
- }, {
- key: "render",
- value: function render() {
- var _this2 = this;
-
- var _this$props = this.props,
- children = _this$props.children,
- forceRender = _this$props.forceRender,
- visible = _this$props.visible;
- var portal = null;
- var childProps = {
- getOpenCount: function getOpenCount() {
- return openCount;
- },
- getContainer: this.getContainer,
- switchScrollingEffect: this.switchScrollingEffect
- }; // suppport react15
-
- if (!IS_REACT_16) {
- return /*#__PURE__*/React.createElement(ContainerRender, {
- parent: this,
- visible: visible,
- autoDestroy: false,
- getComponent: function getComponent() {
- var extra = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
- return children(_objectSpread({}, extra, {}, childProps, {
- ref: _this2.savePortal
- }));
- },
- getContainer: this.getContainer,
- forceRender: forceRender
- }, function (_ref) {
- var renderComponent = _ref.renderComponent,
- removeContainer = _ref.removeContainer;
- _this2.renderComponent = renderComponent;
- _this2.removeContainer = removeContainer;
- return null;
- });
- }
-
- if (forceRender || visible || this._component) {
- portal = /*#__PURE__*/React.createElement(Portal, {
- getContainer: this.getContainer,
- ref: this.savePortal
- }, children(childProps));
- }
-
- return portal;
- }
- }], [{
- key: "getDerivedStateFromProps",
- value: function getDerivedStateFromProps(props, _ref2) {
- var prevProps = _ref2.prevProps,
- _self = _ref2._self;
- var visible = props.visible,
- getContainer = props.getContainer;
-
- if (prevProps) {
- var prevVisible = prevProps.visible,
- prevGetContainer = prevProps.getContainer;
-
- if (visible !== prevVisible) {
- openCount = visible && !prevVisible ? openCount + 1 : openCount - 1;
- }
-
- var getContainerIsFunc = typeof getContainer === 'function' && typeof prevGetContainer === 'function';
-
- if (getContainerIsFunc ? getContainer.toString() !== prevGetContainer.toString() : getContainer !== prevGetContainer) {
- _self.removeCurrentContainer(false);
- }
- }
-
- return {
- prevProps: props
- };
- }
- }]);
-
- return PortalWrapper;
- }(React.Component);
-
- PortalWrapper.propTypes = {
- wrapperClassName: PropTypes.string,
- forceRender: PropTypes.bool,
- getContainer: PropTypes.any,
- children: PropTypes.func,
- visible: PropTypes.bool
- };
- export default polyfill(PortalWrapper);
|