storry
Version:
State Management made simple
110 lines (92 loc) • 3.33 kB
JavaScript
(function (global, factory) {
if (typeof define === "function" && define.amd) {
define(['module', 'exports', 'react'], factory);
} else if (typeof exports !== "undefined") {
factory(module, exports, require('react'));
} else {
var mod = {
exports: {}
};
factory(mod, mod.exports, global.react);
global.storryReact = mod.exports;
}
})(this, function (module, exports, _react) {
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
function _toConsumableArray(arr) {
if (Array.isArray(arr)) {
for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) {
arr2[i] = arr[i];
}
return arr2;
} else {
return Array.from(arr);
}
}
function _classCallCheck(instance, Constructor) {
if (!(instance instanceof Constructor)) {
throw new TypeError("Cannot call a class as a function");
}
}
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;
};
}();
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;
}
var Provider = function (_Component) {
_inherits(Provider, _Component);
function Provider(props) {
_classCallCheck(this, Provider);
var _this = _possibleConstructorReturn(this, (Provider.__proto__ || Object.getPrototypeOf(Provider)).call(this, props));
_this.state = props.store.state();
props.store.listen(_this.setState.bind(_this));
return _this;
}
_createClass(Provider, [{
key: 'render',
value: function render() {
var _this2 = this;
var children = _react.Children.map(this.props.children, function (child) {
return (0, _react.cloneElement)(child, _this2.state);
});
return _react.createElement.apply(undefined, ['div', null].concat(_toConsumableArray(children)));
}
}]);
return Provider;
}(_react.Component);
exports.default = Provider;
module.exports = exports['default'];
});