@remax/framework-shared
Version:
使用真正的 React 构建跨平台小程序
87 lines (86 loc) • 3.64 kB
JavaScript
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var __read = (this && this.__read) || function (o, n) {
var m = typeof Symbol === "function" && o[Symbol.iterator];
if (!m) return o;
var i = m.call(o), r, ar = [], e;
try {
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
}
catch (error) { e = { error: error }; }
finally {
try {
if (r && !r.done && (m = i["return"])) m.call(i);
}
finally { if (e) throw e.error; }
}
return ar;
};
var __spread = (this && this.__spread) || function () {
for (var ar = [], i = 0; i < arguments.length; i++) ar = ar.concat(__read(arguments[i]));
return ar;
};
import * as React from 'react';
import { ForwardRef } from 'react-is';
import isClassComponent from './utils/isClassComponent';
import { Lifecycle, callbackName } from './lifecycle';
import PageInstanceContext from './PageInstanceContext';
import * as RuntimeOptions from './RuntimeOptions';
export default function createPageWrapper(Page, name) {
var WrappedPage = RuntimeOptions.get('pluginDriver').onPageComponent({ component: Page, page: name });
return /** @class */ (function (_super) {
__extends(PageWrapper, _super);
function PageWrapper(props) {
var _this = _super.call(this, props) || this;
// 页面组件的实例
_this.pageComponentInstance = null;
_this.callbacks = new Map();
Object.keys(Lifecycle).forEach(function (phase) {
var callback = callbackName(phase);
_this[callback] = function () {
var args = [];
for (var _i = 0; _i < arguments.length; _i++) {
args[_i] = arguments[_i];
}
return _this.callLifecycle.apply(_this, __spread([phase], args));
};
});
return _this;
}
PageWrapper.prototype.callLifecycle = function (phase) {
var _a;
var args = [];
for (var _i = 1; _i < arguments.length; _i++) {
args[_i - 1] = arguments[_i];
}
var callback = callbackName(phase);
if (this.pageComponentInstance && typeof this.pageComponentInstance[callback] === 'function') {
return (_a = this.pageComponentInstance)[callback].apply(_a, __spread(args));
}
};
PageWrapper.prototype.render = function () {
var _this = this;
var props = {
location: {
query: this.props.query || {},
},
};
if (isClassComponent(Page) || Page.$$typeof === ForwardRef) {
props.ref = function (node) { return (_this.pageComponentInstance = node); };
}
return React.createElement(PageInstanceContext.Provider, { value: this.props.page }, React.createElement(WrappedPage, props));
};
return PageWrapper;
}(React.Component));
}