feat: complet dynamically render

This commit is contained in:
wuyue.xht 2020-03-15 01:03:09 +08:00
parent fcd0af8346
commit edf14c1bc6
6 changed files with 150 additions and 3 deletions

25
packages/runtime-framework/index.d.ts vendored Normal file
View File

@ -0,0 +1,25 @@
import { ReactType } from 'react';
export as namespace LowCodeEngineRuntime;
export = LowCodeEngineRuntime;
declare module LowCodeEngineRuntime {
type HistoryMode = 'browser' | 'hash';
interface ComponentsMap {
[key: string]: ReactType;
}
interface UtilsMap {
[key: string]: any;
}
interface AppConfig {
history?: HistoryMode;
globalComponents?: ComponentsMap;
globalUtils?: UtilsMap;
containerId?: string;
}
function runApp(Component: any, config?: AppConfig | (() => AppConfig), exposeModule?: boolean): any;
}

View File

@ -21,5 +21,8 @@
"ts-node/register"
]
},
"license": "MIT"
"license": "MIT",
"dependencies": {
"@ali/recore": "^1.6.9"
}
}

View File

@ -1 +1,5 @@
//
import { Router, runApp } from '@ali/recore';
import Provider from './provider';
import Trunk from './trunk';
export { runApp, Router, Trunk, Provider };

View File

@ -0,0 +1,38 @@
import { Component, createElement } from 'react';
import Trunk from './trunk';
interface IProps {
getPageData: () => any;
[key: string]: any;
}
interface IState {
schema: object | null;
}
export default class LazyComponent extends Component<IProps, IState> {
constructor(props: IProps) {
super(props);
this.state = {
schema: null,
};
}
public async componentDidMount() {
const { getPageData } = this.props;
if (getPageData) {
const schema = await getPageData();
this.setState({ schema });
}
}
public render() {
const Renderer = Trunk.getRenderer();
if (!Renderer) {
return null;
}
const { getPageData, ...restProps } = this.props;
const { schema } = this.state;
return createElement(Renderer as any, { schema, ...restProps });
}
}

View File

@ -0,0 +1,76 @@
import { createElement, ReactElement, ReactType } from 'react';
import LazyComponent from './lazyComponent';
export interface IAppData {
App: any;
config: object;
}
interface IComponentsMap {
[key: string]: ReactType;
}
interface IUtilsMap {
[key: string]: any;
}
type HistoryMode = 'browser' | 'hash';
export interface IAppConfig {
history?: HistoryMode;
globalComponents?: IComponentsMap;
globalUtils?: IUtilsMap;
containerId?: string;
}
export default abstract class Provider {
public globalComponents: any = {};
public globalUtils: any = {};
public routerConfig: { [key: string]: string } = {};
public layout: { componentName: string; props: any } | null = null;
constructor() {
this.init();
}
public create(appkey: string): Promise<IAppData> {
return new Promise(async (resolve, reject) => {
try {
const config = await this.getAppData(appkey);
const App = this.createApp();
resolve({
App,
config,
});
} catch (err) {
reject(err.message);
}
});
}
public async init() {
console.log('init');
}
public async getAppData(appkey: string, restOptions?: any): Promise<object> {
console.log('getAppData');
return {};
}
public async getPageData(pageId: string, restOptions?: any): Promise<any> {
console.log('getPageData');
return;
}
public getLazyComponent(pageId: string, props: any): ReactElement | null {
if (!pageId) {
return null;
}
return createElement(LazyComponent as any, { getPageData: async () => await this.getPageData(pageId), ...props });
}
public createApp() {
console.log('createApp');
return;
}
}

View File

@ -1,6 +1,7 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"outDir": "./lib"
"outDir": "./lib",
"jsx": "react",
}
}