-
Notifications
You must be signed in to change notification settings - Fork 0
/
withStateHandlers.js
78 lines (56 loc) · 2.65 KB
/
withStateHandlers.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.default = void 0;
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _inheritsLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/inheritsLoose"));
var _react = require("react");
var _createFactory = _interopRequireDefault(require("./utils/createFactory"));
var _setDisplayName = _interopRequireDefault(require("./setDisplayName"));
var _wrapDisplayName = _interopRequireDefault(require("./wrapDisplayName"));
var _mapValues = _interopRequireDefault(require("./utils/mapValues"));
var withStateHandlers = function withStateHandlers(initialState, stateUpdaters) {
return function (BaseComponent) {
var factory = (0, _createFactory.default)(BaseComponent);
var WithStateHandlers =
/*#__PURE__*/
function (_Component) {
(0, _inheritsLoose2.default)(WithStateHandlers, _Component);
function WithStateHandlers() {
var _this;
for (var _len = arguments.length, _args = new Array(_len), _key = 0; _key < _len; _key++) {
_args[_key] = arguments[_key];
}
_this = _Component.call.apply(_Component, [this].concat(_args)) || this;
_this.state = typeof initialState === 'function' ? initialState(_this.props) : initialState;
_this.stateUpdaters = (0, _mapValues.default)(stateUpdaters, function (handler) {
return function (mayBeEvent) {
for (var _len2 = arguments.length, args = new Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) {
args[_key2 - 1] = arguments[_key2];
}
// Having that functional form of setState can be called async
// we need to persist SyntheticEvent
if (mayBeEvent && typeof mayBeEvent.persist === 'function') {
mayBeEvent.persist();
}
_this.setState(function (state, props) {
return handler(state, props).apply(void 0, [mayBeEvent].concat(args));
});
};
});
return _this;
}
var _proto = WithStateHandlers.prototype;
_proto.render = function render() {
return factory((0, _extends2.default)({}, this.props, this.state, this.stateUpdaters));
};
return WithStateHandlers;
}(_react.Component);
if (process.env.NODE_ENV !== 'production') {
return (0, _setDisplayName.default)((0, _wrapDisplayName.default)(BaseComponent, 'withStateHandlers'))(WithStateHandlers);
}
return WithStateHandlers;
};
};
var _default = withStateHandlers;
exports.default = _default;