84 lines
3.1 KiB
JavaScript
84 lines
3.1 KiB
JavaScript
"use strict";
|
|
|
|
exports.__esModule = true;
|
|
exports.default = mapContextToProps;
|
|
|
|
var _react = _interopRequireDefault(require("react"));
|
|
|
|
var _forwardRef = _interopRequireDefault(require("./forwardRef"));
|
|
|
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
|
|
function _extends() { _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; }; return _extends.apply(this, arguments); }
|
|
|
|
var getDisplayName = function getDisplayName(Component) {
|
|
var name = typeof Component === 'string' ? Component : Component.name || Component.displayName;
|
|
return name ? "ContextTransform(" + name + ")" : 'ContextTransform';
|
|
};
|
|
|
|
var ensureConsumer = function ensureConsumer(c) {
|
|
return c.Consumer || c;
|
|
};
|
|
|
|
function $mapContextToProps(_ref, Component) {
|
|
var maybeArrayOfConsumers = _ref.consumers,
|
|
mapToProps = _ref.mapToProps,
|
|
displayName = _ref.displayName,
|
|
_ref$forwardRefAs = _ref.forwardRefAs,
|
|
forwardRefAs = _ref$forwardRefAs === void 0 ? 'ref' : _ref$forwardRefAs;
|
|
var consumers = maybeArrayOfConsumers;
|
|
|
|
if (!Array.isArray(maybeArrayOfConsumers)) {
|
|
consumers = [maybeArrayOfConsumers];
|
|
}
|
|
|
|
var SingleConsumer = ensureConsumer(consumers[0]);
|
|
|
|
function singleRender(props, ref) {
|
|
var _extends2;
|
|
|
|
var propsWithRef = _extends((_extends2 = {}, _extends2[forwardRefAs] = ref, _extends2), props);
|
|
|
|
return _react.default.createElement(SingleConsumer, null, function (value) {
|
|
return _react.default.createElement(Component, _extends({}, propsWithRef, mapToProps(value, props)));
|
|
});
|
|
}
|
|
|
|
function multiRender(props, ref) {
|
|
var _extends3;
|
|
|
|
var propsWithRef = _extends((_extends3 = {}, _extends3[forwardRefAs] = ref, _extends3), props);
|
|
|
|
return consumers.reduceRight(function (inner, Context) {
|
|
return function () {
|
|
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
|
|
args[_key] = arguments[_key];
|
|
}
|
|
|
|
var Consumer = ensureConsumer(Context);
|
|
return _react.default.createElement(Consumer, null, function (value) {
|
|
return inner.apply(void 0, args.concat([value]));
|
|
});
|
|
};
|
|
}, function () {
|
|
for (var _len2 = arguments.length, contexts = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
|
|
contexts[_key2] = arguments[_key2];
|
|
}
|
|
|
|
return _react.default.createElement(Component, _extends({}, propsWithRef, mapToProps.apply(void 0, contexts.concat([props]))));
|
|
})();
|
|
}
|
|
|
|
var contextTransform = consumers.length === 1 ? singleRender : multiRender;
|
|
return (0, _forwardRef.default)(contextTransform, {
|
|
displayName: displayName || getDisplayName(Component)
|
|
});
|
|
}
|
|
|
|
function mapContextToProps(maybeOpts, mapToProps, Component) {
|
|
if (arguments.length === 2) return $mapContextToProps(maybeOpts, mapToProps);
|
|
return $mapContextToProps({
|
|
consumers: maybeOpts,
|
|
mapToProps: mapToProps
|
|
}, Component);
|
|
} |