// 输出数据类型 // single:单个值 combine:组合值 multi:多路复用 type OutputType = 'single' | 'combine' | 'multi'; // 输出数据标签 type OutputTag = | 'update' | 'delete' | 'form-item' // 选择tab | 'check-tab' // 搜索项 | 'search-item' // 取消 | 'cancel' // 确定 | 'confirm'; // 输入数据类型 type InputType = 'basic' | 'array' | 'object'; // 输入数据标签 type InputTag = 'jsx'; // 内部状态类型 // type InnerType = ''; // 内部状态标签 // type InnerTag = ''; /** * 组件输入数据元信息 */ export type InputMeta = { // 输入属性值 key?: string; // 类型 默认:basic type?: InputType; // 标签 默认:null tags?: InputTag[]; // 输入值的key作用映射 keyMap?: { [key: string]: string }; // 子结点 children?: InputMeta[]; }; /** * 组件输出数据元信息 */ export type OutputMeta = { // 输出属性值, key?: | string // 对应single | string[]; // 对应combine // 类型 默认:single type?: OutputType; // 标签 默认:null tags?: OutputTag[]; // 子结点 children?: OutputMeta[]; }; /** * 组件内部数据元信息 */ export type InnerMeta = { key?: string; keyMap?: { [key: string]: string }; }; /** * 组件元信息 */ export type Meta = { inputs?: InputMeta[]; output?: OutputMeta; // fixme 这块也是数组呢? inners?: InnerMeta[]; // 命名特征值 fixme 先这么处理 nameInfo?: any; // 对应的页面组件名字 compName?: string; // 元信息合并cb __mergeCb?: Function; // 命名合并 __nameCb?: Function; }; /** * 元信息相关的属性 */ export interface MetaProps { meta?: Meta; inputsMeta?: InputMeta[]; outputMeta?: OutputMeta | string; } export interface CompDefault
{ // 属性默认值 props?: P; // 状态默认值 state?: S; // 元信息默认值 meta?: Meta; } const isObject = (item) => { return ( item && typeof item === 'object' && !Array.isArray(item) && item !== null ); }; export const mergeDeep = (target, source) => { if (isObject(target) && isObject(source)) { Object.keys(source).forEach((key) => { if (isObject(source[key])) { if (!target[key] || !isObject(target[key])) { target[key] = source[key]; } mergeDeep(target[key], source[key]); } else { Object.assign(target, { [key]: source[key] }); } }); } return target; }; /** * 合并元信息 fixme 可能存在优先级问题导致数据不刷新,这块逻辑可以再优化下 */ export const mergeMeta = (props: MetaProps, defaultMeta?: Meta) => { const meta: Meta = {}; if (defaultMeta) mergeDeep(meta, defaultMeta); if (props.meta) mergeDeep(meta, props.meta); if (meta?.__mergeCb) { meta.__mergeCb(props, meta); } else { if (props.inputsMeta) { // fixme 先这么处理 meta.inputs = props.inputsMeta; } if (props.outputMeta) { if (typeof props.outputMeta === 'string') { mergeDeep(meta.output, { key: props.outputMeta }); } else { mergeDeep(meta.output, props.outputMeta); } } } if (meta.output) { meta.output.key = meta.output.key || ''; meta.output.tags = meta.output.tags || []; meta.output.type = meta.output.type || 'single'; } return meta; }; /** * 绑定数据回调事件 */ export const bindOnChange = (dom: any, cb: Function) => { dom.props.onChange = cb; }; /** * 绑定当前组件状态,到子组件props */ export const bindStateToProp = ( element: JSX.Element, key: string, bindCb: Function, ) => { element.props[key] = bindCb(); element.props = { ...element.props }; };