redux-contexts
Version:
[](https://www.npmjs.com/package/multireducer) [](https://travis-ci.org/Vi
93 lines (64 loc) • 4.28 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
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 _react2 = _interopRequireDefault(_react);
var _propTypes = require('prop-types');
var _propTypes2 = _interopRequireDefault(_propTypes);
var _reactRedux = require('react-redux');
var _wrapDispatch = require('./wrapDispatch');
var _wrapDispatch2 = _interopRequireDefault(_wrapDispatch);
var _virtualizedState = require('./virtualizedState');
var _virtualizedState2 = _interopRequireDefault(_virtualizedState);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
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; }
exports.default = function (mapStateToProps, mapDispatchToProps, mergeProps, options) {
var finalMapStateToProps = function finalMapStateToProps(state, ownProps) {
checkContextName(ownProps);
var contextName = ownProps.contextName;
var contextState = (0, _virtualizedState2.default)(state, contextName);
if (mapStateToProps) {
var mapped = mapStateToProps(contextState, ownProps, state);
if (typeof mapped === 'function') {
return mapped(contextState, ownProps, state);
}
return mapped;
}
return {};
};
var finalMapDispatchToProps = function finalMapDispatchToProps(dispatch, ownProps) {
checkContextName(ownProps);
var contextName = ownProps.contextName;
return mapDispatchToProps ? mapDispatchToProps((0, _wrapDispatch2.default)(dispatch, contextName), ownProps) : {};
};
var checkContextName = function checkContextName(ownProps) {
if (!ownProps.contextName) {
throw new Error('No context store provided. ', ownProps);
}
};
return function (ComposedComponent) {
var _class, _temp;
var ConnectedComponent = (0, _reactRedux.connect)(finalMapStateToProps, finalMapDispatchToProps, mergeProps, options)(ComposedComponent);
return _temp = _class = function (_Component) {
_inherits(ContextConnect, _Component);
function ContextConnect() {
_classCallCheck(this, ContextConnect);
return _possibleConstructorReturn(this, (ContextConnect.__proto__ || Object.getPrototypeOf(ContextConnect)).apply(this, arguments));
}
_createClass(ContextConnect, [{
key: 'render',
value: function render() {
return _react2.default.createElement(ConnectedComponent, _extends({}, this.props, this.context));
}
}]);
return ContextConnect;
}(_react.Component), _class.contextTypes = {
contextName: _propTypes2.default.string
}, _temp;
};
};