-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
62 lines (50 loc) · 1.76 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
'use strict';
var React = require('react');
var PureComponent = React.PureComponent;
var h = React.createElement;
function _inherits(subClass, superClass) {
subClass.prototype = Object.create(superClass.prototype, {
constructor: {
value: subClass,
writable: true,
configurable: true
}
});
if (superClass) {
Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass;
}
}
function wrap(Context, key, WrappedComp) {
function Connected(props) {
PureComponent.call(this, props);
}
_inherits(Connected, PureComponent);
Connected.prototype.render = function () {
var props = this.props;
return h(Context.Consumer, null, function (context) {
var tmpProps = {};
tmpProps[key] = context;
return h(WrappedComp, Object.assign(tmpProps, props));
});
};
Connected.WrappedComp = WrappedComp;
var wrappedCompName = WrappedComp.displayName || WrappedComp.name || "Component";
var consumerName = Context.Consumer.displayName || Context.Consumer.name || "Context.Consumer";
Connected.displayName = wrappedCompName + "(" + consumerName + "." + key + ")";
return Connected;
};
function withContext(Context) {
var key = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : "context";
return function (WrappedComp) {
return wrap(Context, key, WrappedComp);
};
}
function withMultiContext(map) {
return function (WrappedComp) {
return Object.keys(map).reduce(function (Comp, key) {
return wrap(map[key], key, Comp);
}, WrappedComp);
};
}
exports.withContext = withContext;
exports.withMultiContext = withMultiContext;