import _objectWithoutProperties from 'babel-runtime/helpers/objectWithoutProperties';
import _classCallCheck from 'babel-runtime/helpers/classCallCheck';
import _createClass from 'babel-runtime/helpers/createClass';
import _possibleConstructorReturn from 'babel-runtime/helpers/possibleConstructorReturn';
import _inherits from 'babel-runtime/helpers/inherits';
var _jsxFileName = 'src/portal/Portal.jsx';
/**
 * Portal.jsx
 *
 * @Author: jruif
 * @Date: 2018/1/8 上午10:17
 */

import React from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';
import ReactDOM from 'react-dom';
import omit from '../util/omit';

var Portal = function (_React$PureComponent) {
    _inherits(Portal, _React$PureComponent);

    function Portal(props, context) {
        _classCallCheck(this, Portal);

        var _this = _possibleConstructorReturn(this, (Portal.__proto__ || Object.getPrototypeOf(Portal)).call(this, props, context));

        _this.state = {
            isMounted: false
        };
        _this.rootNode = document.createElement('div');
        _this.rootNode.className = classNames(props.prefixClx);
        return _this;
    }

    _createClass(Portal, [{
        key: 'componentDidMount',
        value: function componentDidMount() {
            var _props = this.props,
                mountNode = _props.mountNode,
                onChildrenMount = _props.onChildrenMount;

            mountNode.appendChild(this.rootNode);
            onChildrenMount && onChildrenMount();
            this.setState({ isMounted: true });
        }
    }, {
        key: 'componentWillUnmount',
        value: function componentWillUnmount() {
            var mountNode = this.props.mountNode;

            mountNode.removeChild(this.rootNode);
        }
    }, {
        key: 'render',
        value: function render() {
            var _props2 = this.props,
                containerRef = _props2.containerRef,
                children = _props2.children,
                prefixClx = _props2.prefixClx,
                className = _props2.className,
                other = _objectWithoutProperties(_props2, ['containerRef', 'children', 'prefixClx', 'className']);

            var classes = classNames(prefixClx + '-content', className);
            return ReactDOM.createPortal(React.createElement(
                'div',
                Object.assign({ ref: containerRef, className: classes }, omit(other, ['mountNode', 'onChildrenMount']), {
                    __source: {
                        fileName: _jsxFileName,
                        lineNumber: 55
                    },
                    __self: this
                }),
                this.state.isMounted ? children : null
            ), this.rootNode);
        }
    }]);

    return Portal;
}(React.PureComponent);

Portal.displayName = 'NeatUI.Portal';
Portal.propTypes = {
    mountNode: PropTypes.any,
    containerRef: PropTypes.func,
    prefixClx: PropTypes.string,
    onChildrenMount: PropTypes.func
};
Portal.defaultProps = {
    mountNode: document.body,
    containerRef: function containerRef() {},
    prefixClx: 'ne-portal'
};


export default Portal;