react-component-decorators
Version:
React decorators, magic.
90 lines (64 loc) • 3.25 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 defaultOptions = {
conditions: conditions,
// passes in apply to validators
thisArg: false,
// used when rendering non valid
displayLoading: null,
// will not even try to validate unless the value
// exists
exists: true,
// if this is false,
// it will validate on every render
ready: true
};
options = Object.assign(defaultOptions, options);
var displayed = options.displayLoading;
if (typeof displayed === 'function') displayed = displayed();
return function extendTarget(target) {
var RenderWhenReady = function (_target) {
_inherits(RenderWhenReady, _target);
function RenderWhenReady() {
var _temp, _this, _ret;
_classCallCheck(this, RenderWhenReady);
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
return _ret = (_temp = (_this = _possibleConstructorReturn(this, _target.call.apply(_target, [this].concat(args))), _this), _this.readyToRender = false, _temp), _possibleConstructorReturn(_this, _ret);
}
// @autobind
RenderWhenReady.prototype.render = function render() {
if (options.ready && this.readyToRender) {
return _target.prototype.render.call(this, this.props, this.state);
}
// if not valid
if ((0, _validateOn2.default)(options, this)) {
this.readyToRender = true;
return _target.prototype.render.call(this, this.props, this.state);
} else {
return displayed;
}
};
return RenderWhenReady;
}(target);
return RenderWhenReady;
};
// 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:
// - [ ] add a class builder to have those presets available
// state, props
// fn callback, or exists
//
// can extend with options or just class
module.exports = exports['default'];