@alilc/lowcode-rax-renderer
Version:
Rax renderer for Ali lowCode engine
96 lines (95 loc) • 4.45 kB
JavaScript
var _excluded = ["forwardedRef"];
function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; _setPrototypeOf(subClass, superClass); }
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function (o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
// @ts-nocheck
import { Component, forwardRef } from 'rax';
import PropTypes from 'prop-types';
import { AppHelper } from '@alilc/lowcode-utils';
import { utils, contextFactory } from '@alilc/lowcode-renderer-core';
import componentRendererFactory from '../renderer/component';
import blockRendererFactory from '../renderer/block';
var forEach = utils.forEach,
isFileSchema = utils.isFileSchema;
export default function compFactory(schema, components, componentsMap, config) {
if (components === void 0) {
components = {};
}
if (componentsMap === void 0) {
componentsMap = {};
}
if (config === void 0) {
config = {};
}
// 自定义组件需要有自己独立的appHelper
var appHelper = new AppHelper(config);
var CompRenderer = componentRendererFactory();
var BlockRenderer = blockRendererFactory();
var AppContext = contextFactory();
var LNCompView = /*#__PURE__*/function (_Component) {
_inheritsLoose(LNCompView, _Component);
function LNCompView() {
return _Component.apply(this, arguments) || this;
}
var _proto = LNCompView.prototype;
_proto.render = function render() {
var _this = this;
if (!schema || schema.componentName !== 'Component' || !isFileSchema(schema)) {
console.warn('自定义组件模型结构异常!');
return null;
}
var _this$props = this.props,
forwardedRef = _this$props.forwardedRef,
otherProps = _objectWithoutPropertiesLoose(_this$props, _excluded);
// 低代码组件透传应用上下文
['utils', 'constants', 'history', 'location', 'match'].forEach(function (key) {
var _this$context, _this$context2;
if (!appHelper[key] && (_this$context = _this.context) != null && _this$context.appHelper && (_this$context2 = _this.context) != null && _this$context2.appHelper[key]) {
appHelper.set(key, _this.context.appHelper[key]);
}
});
// 支持通过context透传国际化配置
var localeProps = {};
var _this$context3 = this.context,
locale = _this$context3.locale,
messages = _this$context3.messages;
if (locale && messages && messages[schema.fileName]) {
localeProps.locale = locale;
localeProps.messages = messages[schema.fileName];
}
var props = _extends({}, schema.defaultProps, localeProps, otherProps, {
__schema: schema,
ref: forwardedRef
});
return createElement(AppContext.Consumer, null, function (context) {
_this.context = context;
return createElement(CompRenderer, _extends({}, props, {
__appHelper: appHelper,
__components: _extends({}, components, {
Component: CompRenderer,
Block: BlockRenderer
}),
__componentsMap: componentsMap
}));
});
};
return LNCompView;
}(Component);
LNCompView.displayName = 'LceCompFactory';
LNCompView.version = config.version || '0.0.0';
LNCompView.contextType = AppContext;
LNCompView.propTypes = {
forwardedRef: PropTypes.func
};
var ResComp = forwardRef(function (props, ref) {
return createElement(LNCompView, _extends({}, props, {
forwardedRef: ref
}));
});
forEach(schema.static, function (val, key) {
ResComp[key] = val;
});
ResComp.version = config.version || '0.0.0';
return ResComp;
}