react-micro-lifecycles
Version:
Micro life-cycles form React DOM nodes
140 lines (108 loc) • 6.58 kB
JavaScript
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
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 _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; }
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; }
var noop = function noop() {};
var createHyperscriptStable = function createHyperscriptStable(h, Component) {
var Lifecycles = function (_Component) {
_inherits(Lifecycles, _Component);
function Lifecycles() {
var _ref;
var _temp, _this, _ret;
_classCallCheck(this, Lifecycles);
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
return _ret = (_temp = (_this = _possibleConstructorReturn(this, (_ref = Lifecycles.__proto__ || Object.getPrototypeOf(Lifecycles)).call.apply(_ref, [this].concat(args))), _this), _this.ref = function (el) {
_this.el = el;
_this.props.$ref(el);
}, _temp), _possibleConstructorReturn(_this, _ret);
}
_createClass(Lifecycles, [{
key: 'componentDidMount',
value: function componentDidMount() {
var _props = this.props,
$attach = _props.$attach,
$update = _props.$update,
$detach = _props.$detach,
$ref = _props.$ref,
$as = _props.$as,
rest = _objectWithoutProperties(_props, ['$attach', '$update', '$detach', '$ref', '$as']);
return $attach(this.el, rest);
}
}, {
key: 'componentDidUpdate',
value: function componentDidUpdate(prevProps) {
var _props2 = this.props,
$attach = _props2.$attach,
$update = _props2.$update,
$detach = _props2.$detach,
$ref = _props2.$ref,
$as = _props2.$as,
rest = _objectWithoutProperties(_props2, ['$attach', '$update', '$detach', '$ref', '$as']);
var a = prevProps.$attach,
b = prevProps.$update,
c = prevProps.$detach,
d = prevProps.$ref,
e = prevProps.$as,
prevRest = _objectWithoutProperties(prevProps, ['$attach', '$update', '$detach', '$ref', '$as']);
return $update(this.el, rest, prevRest);
}
}, {
key: 'componentWillUnmount',
value: function componentWillUnmount() {
var _props3 = this.props,
$attach = _props3.$attach,
$update = _props3.$update,
$detach = _props3.$detach,
$ref = _props3.$ref,
$as = _props3.$as,
rest = _objectWithoutProperties(_props3, ['$attach', '$update', '$detach', '$ref', '$as']);
return $detach(this.el, rest);
}
}, {
key: 'render',
value: function render() {
var _props4 = this.props,
$attach = _props4.$attach,
$update = _props4.$update,
$detach = _props4.$detach,
$ref = _props4.$ref,
$as = _props4.$as,
rest = _objectWithoutProperties(_props4, ['$attach', '$update', '$detach', '$ref', '$as']);
rest.ref = this.ref;
return h($as, rest);
}
}]);
return Lifecycles;
}(Component);
Lifecycles.defaultProps = {
$attach: noop,
$update: noop,
$detach: noop,
$ref: noop
};
return function (type, props) {
for (var _len2 = arguments.length, children = Array(_len2 > 2 ? _len2 - 2 : 0), _key2 = 2; _key2 < _len2; _key2++) {
children[_key2 - 2] = arguments[_key2];
}
if (!props || typeof type !== 'string') return h.apply(undefined, [type, props].concat(children));
if (!props.$attach && !props.$update && !props.$detach) return h.apply(undefined, [type, props].concat(children));
if (process.env.NODE_ENV !== 'production') {
if (props.ref && typeof props.ref !== 'function') {
console.error('react-micro-lifecycles received props with ref, expected ref to be a function, "' + _typeof(props.ref) + '" provided.');
}
}
props.$ref = props.ref;
props.$as = type;
delete props.ref;
return h.apply(undefined, [Lifecycles, props].concat(children));
};
};
exports.default = createHyperscriptStable;