Merge branch 'feat/supportPropsInLowCodeComponent' into 'release/0.9.0'

低代码组件支持 props 修改设计器实时显示

暂时只支持低代码组件内部使用 this.props.xxx 的场景,不支持表达式

See merge request !874436
This commit is contained in:
康为 2020-06-30 20:25:06 +08:00
commit 642f155965
2 changed files with 26 additions and 5 deletions

View File

@ -131,7 +131,7 @@ export class DocumentModel {
* id
*/
nextId() {
return (++this.seqId).toString(36).toLocaleLowerCase();
return this.id + (++this.seqId).toString(36).toLocaleLowerCase();
}
/**

View File

@ -219,15 +219,36 @@ export class SimulatorRenderer implements BuiltinSimulatorRenderer {
_schema.methods = {};
_schema.lifeCycles = {};
const getElement = (componentsMap: any, schema: any): ReactElement => {
const processPropsSchema = (propsSchema: any, propsMap: any): any => {
if (!propsSchema) {
return {};
}
const result = {...propsSchema};
const reg = /^(?:this\.props|props)\.(\S+)$/;
Object.keys(propsSchema).map((key: string) => {
if (propsSchema[key].type === 'JSExpression') {
const { value } = propsSchema[key];
const matched = reg.exec(value);
if (matched) {
const propName = matched[1];
result[key] = propsMap[propName];
}
}
});
return result;
};
const getElement = (componentsMap: any, schema: any, propsMap: any): ReactElement => {
const Com = componentsMap[schema.componentName];
let children = null;
if (schema.children && schema.children.length > 0) {
children = schema.children.map((item: any) => getElement(componentsMap, item));
children = schema.children.map((item: any) => getElement(componentsMap, item, propsMap));
}
const _leaf = host.document.designer.currentDocument?.createNode(schema);
const node = host.document.createNode(schema);
let props = host.document.designer.transformProps(schema.props || {}, node, TransformStage.Init);
let props = processPropsSchema(schema.props, propsMap);
props = host.document.designer.transformProps(props, node, TransformStage.Init);
props = host.document.designer.transformProps(props, node, TransformStage.Render);
return createElement(Com, {...props, _leaf}, children);
}
@ -238,7 +259,7 @@ export class SimulatorRenderer implements BuiltinSimulatorRenderer {
const componentsMap = renderer.componentsMap;
let children = null;
if (_schema.children && Array.isArray(_schema.children)) {
children = _schema.children?.map((item:any) => getElement(componentsMap, item));
children = _schema.children?.map((item:any) => getElement(componentsMap, item, this.props));
}
return createElement(React.Fragment, {}, children);
}