@alifd/next
Version:
A configurable component library for web built on React.
108 lines (76 loc) • 3.4 kB
JavaScript
;
exports.__esModule = true;
exports.default = undefined;
var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');
var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);
var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');
var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);
var _inherits2 = require('babel-runtime/helpers/inherits');
var _inherits3 = _interopRequireDefault(_inherits2);
var _class, _temp2;
var _react = require('react');
var _react2 = _interopRequireDefault(_react);
var _reactDom = require('react-dom');
var _propTypes = require('prop-types');
var _propTypes2 = _interopRequireDefault(_propTypes);
var _util = require('../util');
var _findNode = require('./utils/find-node');
var _findNode2 = _interopRequireDefault(_findNode);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var makeChain = _util.func.makeChain;
var Gateway = (_temp2 = _class = function (_Component) {
(0, _inherits3.default)(Gateway, _Component);
function Gateway() {
var _temp, _this, _ret;
(0, _classCallCheck3.default)(this, Gateway);
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
return _ret = (_temp = (_this = (0, _possibleConstructorReturn3.default)(this, _Component.call.apply(_Component, [this].concat(args))), _this), _this.saveChildRef = function (ref) {
_this.child = ref;
}, _temp), (0, _possibleConstructorReturn3.default)(_this, _ret);
}
Gateway.prototype.componentDidMount = function componentDidMount() {
this.containerNode = this.getContainerNode(this.props);
this.forceUpdate();
};
Gateway.prototype.componentWillReceiveProps = function componentWillReceiveProps(nextProps) {
this.containerNode = this.getContainerNode(nextProps);
};
Gateway.prototype.getContainerNode = function getContainerNode(props) {
var targetNode = (0, _findNode2.default)(props.target);
return (0, _findNode2.default)(props.container, targetNode);
};
Gateway.prototype.getChildNode = function getChildNode() {
return (0, _reactDom.findDOMNode)(this.child);
};
Gateway.prototype.render = function render() {
if (!this.containerNode) {
return null;
}
var children = this.props.children;
var child = children ? _react.Children.only(children) : null;
if (!child) {
return null;
}
if (typeof child.ref === 'string') {
throw new Error('Can not set ref by string in Gateway, use function instead.');
}
child = _react2.default.cloneElement(child, {
ref: makeChain(this.saveChildRef, child.ref)
});
return (0, _reactDom.createPortal)(child, this.containerNode);
};
return Gateway;
}(_react.Component), _class.propTypes = {
children: _propTypes2.default.node,
container: _propTypes2.default.any,
target: _propTypes2.default.any
}, _class.defaultProps = {
container: function container() {
return document.body;
}
}, _temp2);
Gateway.displayName = 'Gateway';
exports.default = Gateway;
module.exports = exports['default'];