react-component-decorators
Version:
React decorators, magic.
62 lines (41 loc) • 2.59 kB
JavaScript
;
exports.__esModule = true;
exports.default = function () {
var conditions = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
var defaults = { debug: true };
options = Object.assign(defaults, options);
function extendTarget(target) {
return function (_target) {
_inherits(ValidateWhenUpdating, _target);
function ValidateWhenUpdating() {
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
_classCallCheck(this, ValidateWhenUpdating);
var _this = _possibleConstructorReturn(this, _target.call.apply(_target, [this].concat(args)));
var setState = _this.setState;
_this.setState = function () {
for (var _len2 = arguments.length, args = Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
args[_key2] = arguments[_key2];
}
var newState = args[0];
if ((0, _validateOn2.default)(conditions, newState, options)) return setState.apply(_this, args);else if (options.debug) console.debug('new state did not match conditions', newState);
};
return _this;
}
return ValidateWhenUpdating;
}(target);
}
return extendTarget;
};
var _validateOn = require('./validateOn');
var _validateOn2 = _interopRequireDefault(_validateOn);
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; }
// @TODO:
// - [ ] with a queue
// can extend with options or just class
module.exports = exports['default'];