@cerebral/preact
Advanced tools
Comparing version
@@ -8,5 +8,6 @@ "use strict"; | ||
exports["default"] = void 0; | ||
var _preact = require("preact"); | ||
var _cerebral = require("cerebral"); | ||
var _internal = require("cerebral/internal"); | ||
var _preact = require("preact"); | ||
var _context = require("./context"); | ||
function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); } | ||
@@ -32,3 +33,5 @@ function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } } | ||
key: "getChildContext", | ||
value: function getChildContext() { | ||
value: | ||
// Support for Preact 8 | ||
function getChildContext() { | ||
var _this$props = this.props, | ||
@@ -50,3 +53,20 @@ app = _this$props.app, | ||
value: function render(props) { | ||
return props.children[0] || props.children; | ||
var app = props.app, | ||
controller = props.controller, | ||
children = props.children; | ||
if (controller) { | ||
(0, _internal.DEPRECATE)('Container', 'please change from "controller" to "app" property'); | ||
} | ||
if (!app && !controller) { | ||
(0, _cerebral.throwError)('You are not passing a Cerebral app to Container'); | ||
} | ||
var controllerInstance = app || controller; | ||
// Support for Preact 10 | ||
if (_context.ControllerContext && _context.ControllerContext.Provider) { | ||
return (0, _preact.h)(_context.ControllerContext.Provider, { | ||
value: controllerInstance | ||
}, children[0] || children); | ||
} | ||
return children[0] || children; | ||
} | ||
@@ -53,0 +73,0 @@ }]); |
@@ -11,3 +11,7 @@ "use strict"; | ||
var _propTypes = _interopRequireDefault(require("prop-types")); | ||
var _context = require("./context"); | ||
function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; } | ||
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; } | ||
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; } | ||
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; } | ||
function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); } | ||
@@ -98,4 +102,11 @@ function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } } | ||
function CerebralComponent(props, context) { | ||
var _this2; | ||
_classCallCheck(this, CerebralComponent); | ||
return _callSuper(this, CerebralComponent, [dependencies, mergeProps, props, context.controller, Component.displayName || Component.name]); | ||
// Support both Preact 8 and 10 context | ||
_this2 = _callSuper(this, CerebralComponent, [dependencies, mergeProps, props, props.controller || context && context.controller, Component.displayName || Component.name]); | ||
_this2.wrappedComponent = null; | ||
_this2.setWrappedComponent = function (instance) { | ||
_this2.wrappedComponent = instance; | ||
}; | ||
return _this2; | ||
} | ||
@@ -110,4 +121,11 @@ _inherits(CerebralComponent, _BaseComponent); | ||
key: "render", | ||
value: function render(props) { | ||
value: function render() { | ||
var _this3 = this; | ||
return this.view.render(this.props, function (componentProps) { | ||
// Capture reference for class components | ||
if (Component.prototype && Component.prototype.render) { | ||
return (0, _preact.h)(Component, _objectSpread(_objectSpread({}, componentProps), {}, { | ||
ref: _this3.setWrappedComponent | ||
})); | ||
} | ||
return (0, _preact.h)(Component, componentProps); | ||
@@ -122,4 +140,53 @@ }); | ||
}; | ||
return CerebralComponent; | ||
var WrappedComponent = typeof _preact.forwardRef === 'function' ? (0, _preact.forwardRef)(function (props, ref) { | ||
var setCerebralComponentRef = function setCerebralComponentRef(instance) { | ||
if (ref) { | ||
if (typeof ref === 'function') { | ||
ref(instance); | ||
} else { | ||
ref.current = instance; | ||
} | ||
} | ||
}; | ||
if (_context.ControllerContext && _context.ControllerContext.Consumer) { | ||
return (0, _preact.h)(_context.ControllerContext.Consumer, null, function (controller) { | ||
if (!controller) { | ||
(0, _cerebral.throwError)('Can not find controller, did you remember to use the Container component? Read more at: http://cerebraljs.com/docs/api/components.html#preact-(beta)'); | ||
} | ||
return (0, _preact.h)(CerebralComponent, _objectSpread(_objectSpread({}, props), {}, { | ||
controller: controller, | ||
ref: setCerebralComponentRef | ||
})); | ||
}); | ||
} | ||
return (0, _preact.h)(CerebralComponent, _objectSpread(_objectSpread({}, props), {}, { | ||
ref: setCerebralComponentRef | ||
})); | ||
}) : function (props) { | ||
if (_context.ControllerContext && _context.ControllerContext.Consumer) { | ||
return (0, _preact.h)(_context.ControllerContext.Consumer, null, function (controller) { | ||
if (!controller) { | ||
(0, _cerebral.throwError)('Can not find controller, did you remember to use the Container component? Read more at: http://cerebraljs.com/docs/api/components.html#preact-(beta)'); | ||
} | ||
return (0, _preact.h)(CerebralComponent, _objectSpread(_objectSpread({}, props), {}, { | ||
controller: controller | ||
})); | ||
}); | ||
} | ||
return (0, _preact.h)(CerebralComponent, props); | ||
}; | ||
WrappedComponent.toJSON = function () { | ||
return Component.displayName || Component.name; | ||
}; | ||
WrappedComponent.getWrappedInstance = function (ref) { | ||
if (ref && ref.wrappedComponent) { | ||
return ref.wrappedComponent; | ||
} | ||
return null; | ||
}; | ||
if (Component.displayName) { | ||
WrappedComponent.displayName = "ConnectWrapping_".concat(Component.displayName); | ||
} | ||
return WrappedComponent; | ||
} | ||
//# sourceMappingURL=Hoc.js.map |
{ | ||
"name": "@cerebral/preact", | ||
"version": "4.2.2", | ||
"version": "4.2.3-1740953427684", | ||
"type": "commonjs", | ||
@@ -25,9 +25,12 @@ "description": "Preact view for Cerebral", | ||
"dependencies": { | ||
"cerebral": "5.2.2", | ||
"preact": "^8.1.0", | ||
"cerebral": "5.2.2-1740953427684", | ||
"prop-types": "^15.8.1" | ||
}, | ||
"devDependencies": { | ||
"@types/prop-types": "^15.7.14" | ||
"@types/prop-types": "^15.7.14", | ||
"preact": "^10.26.4" | ||
}, | ||
"peerDependencies": { | ||
"preact": ">=8.0.0 <11.0.0" | ||
}, | ||
"nyc": { | ||
@@ -34,0 +37,0 @@ "exclude": [ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
44896
39.59%13
18.18%322
41.85%2
100%2
100%+ Added
+ Added
+ Added
- Removed
- Removed
- Removed
- Removed
Updated