mirror of
https://github.com/alibaba/lowcode-engine.git
synced 2026-04-20 20:38:06 +00:00
76 lines
2.6 KiB
JavaScript
76 lines
2.6 KiB
JavaScript
import React, { PureComponent } from 'react';
|
|
import PropTypes from 'prop-types';
|
|
import CompEngine from '../engine/compEngine';
|
|
import BlockEngine from '../engine/blockEngine';
|
|
import AppContext from '../context/appContext';
|
|
import AppHelper from '../utils/appHelper';
|
|
import { forEach, isFileSchema } from '../utils';
|
|
export default function compFactory(schema, components = {}, componentsMap = {}, config = {}) {
|
|
// 自定义组件需要有自己独立的appHelper
|
|
const appHelper = new AppHelper(config);
|
|
class LNCompView extends PureComponent {
|
|
static dislayName = 'luna-comp-factory';
|
|
static version = config.version || '0.0.0';
|
|
static contextType = AppContext;
|
|
static propTypes = {
|
|
forwardedRef: PropTypes.func
|
|
};
|
|
render() {
|
|
if (!schema || schema.componentName !== 'Component' || !isFileSchema(schema)) {
|
|
console.warn('自定义组件模型结构异常!');
|
|
return null;
|
|
}
|
|
const { forwardedRef, ...otherProps } = this.props;
|
|
// 低代码组件透传应用上下文
|
|
const appCtx = ['utils', 'constants'];
|
|
appCtx.forEach(key => {
|
|
if (!appHelper[key] && this.context && this.context.appHelper && this.context.appHelper[key]) {
|
|
appHelper.set(key, this.context.appHelper[key]);
|
|
}
|
|
});
|
|
const routerCtx = ['history', 'location', 'match'];
|
|
routerCtx.forEach(key => {
|
|
if (this.context && this.context.appHelper && this.context.appHelper[key]) {
|
|
appHelper.set(key, this.context.appHelper[key]);
|
|
}
|
|
});
|
|
// 支持通过context透传国际化配置
|
|
const localeProps = {};
|
|
const { locale, messages } = this.context;
|
|
if (locale && messages && messages[schema.fileName]) {
|
|
localeProps.locale = locale;
|
|
localeProps.messages = messages[schema.fileName];
|
|
}
|
|
const props = {
|
|
...schema.defaultProps,
|
|
...localeProps,
|
|
...otherProps,
|
|
__schema: schema,
|
|
ref: forwardedRef
|
|
};
|
|
|
|
return (
|
|
<AppContext.Provider
|
|
value={{
|
|
...this.context
|
|
}}
|
|
>
|
|
<CompEngine
|
|
{...props}
|
|
__appHelper={appHelper}
|
|
__components={{ ...components, Component: CompEngine, Block: BlockEngine }}
|
|
__componentsMap={componentsMap}
|
|
/>
|
|
</AppContext.Provider>
|
|
);
|
|
}
|
|
}
|
|
|
|
const ResComp = React.forwardRef((props, ref) => <LNCompView {...props} forwardedRef={ref} />);
|
|
forEach(schema.static, (val, key) => {
|
|
ResComp[key] = val;
|
|
});
|
|
ResComp.version = config.version || '0.0.0';
|
|
return ResComp;
|
|
}
|