feat(vue-container): 支持自定义render

This commit is contained in:
roymondchen 2025-02-27 19:31:07 +08:00
parent 2114b71d47
commit 91cde30d75
3 changed files with 75 additions and 19 deletions

View File

@ -1,5 +1,5 @@
{
"version": "0.0.8",
"version": "1.0.0",
"name": "@tmagic/vue-container",
"type": "module",
"main": "src/index.ts",

View File

@ -0,0 +1,54 @@
import { defineComponent, h, inject, type PropType } from 'vue-demi';
import type TMagicApp from '@tmagic/core';
import { Id, IS_DSL_NODE_KEY, MComponent, toLine } from '@tmagic/core';
import { useComponent, type UserRenderFunction } from '@tmagic/vue-runtime-help';
export default defineComponent({
props: {
config: {
type: Object as PropType<MComponent>,
required: true,
},
index: Number,
iteratorIndex: {
type: Array as PropType<number[]>,
default: () => [],
},
iteratorContainerId: {
type: Array as PropType<Id[]>,
default: () => [],
},
},
setup(props) {
const userRender = inject<UserRenderFunction>('userRender', ({ h, type, props, attrs, style, className }) =>
h(type, { ...props, ...attrs, style, class: className }),
);
const app = inject<TMagicApp>('app');
return () =>
userRender({
h,
config: props.config,
type: useComponent({ componentType: props.config.type, app }),
style: app?.transformStyle(props.config.style || {}),
className: props.config.className
? `${props.config.className} magic-ui-${toLine(props.config.type)}`
: `magic-ui-${toLine(props.config.type)}`,
props: {
config: { ...props.config, [IS_DSL_NODE_KEY]: true },
containerIndex: props.index,
iteratorIndex: props.iteratorIndex,
iteratorContainerId: props.iteratorContainerId,
},
attrs: {
'data-tmagic-id': props.config.id,
'data-tmagic-iterator-index': props.iteratorIndex,
'data-tmagic-iterator-container-id': props.iteratorContainerId,
'data-container-index': props.index,
},
});
},
});

View File

@ -2,21 +2,14 @@
<div v-if="display(config)" :class="className" :style="style">
<slot>
<template v-for="(item, index) in config.items">
<component
<ItemComponent
v-if="display(item)"
:key="item.id"
:is="useComponent({ componentType: item.type, app })"
:data-tmagic-id="item.id"
:data-tmagic-iterator-index="iteratorIndex"
:data-tmagic-iterator-container-id="iteratorContainerId"
:data-container-index="index"
:class="item.className ? `${item.className} magic-ui-${toLine(item.type)}` : `magic-ui-${toLine(item.type)}`"
:style="app?.transformStyle(item.style || {})"
:config="{ ...item, [IS_DSL_NODE_KEY]: true }"
:container-index="index"
:config="item"
:index="index"
:iterator-index="iteratorIndex"
:iterator-container-id="iteratorContainerId"
></component>
></ItemComponent>
</template>
</slot>
</div>
@ -26,8 +19,10 @@
import { computed, defineComponent, type PropType } from 'vue-demi';
import type { Id, MContainer } from '@tmagic/core';
import { IS_DSL_NODE_KEY, toLine } from '@tmagic/core';
import { useApp, useComponent } from '@tmagic/vue-runtime-help';
import { IS_DSL_NODE_KEY } from '@tmagic/core';
import { useApp } from '@tmagic/vue-runtime-help';
import ItemComponent from './Component';
interface ContainerSchema extends Omit<MContainer, 'id'> {
id?: Id;
@ -35,19 +30,29 @@ interface ContainerSchema extends Omit<MContainer, 'id'> {
}
export default defineComponent({
name: 'tmagic-container',
props: {
config: {
type: Object as PropType<ContainerSchema>,
required: true,
},
iteratorIndex: Array as PropType<number[]>,
iteratorContainerId: Array as PropType<Id[]>,
iteratorIndex: {
type: Array as PropType<number[]>,
default: () => [],
},
iteratorContainerId: {
type: Array as PropType<Id[]>,
default: () => [],
},
model: {
type: Object,
default: () => ({}),
},
},
components: { ItemComponent },
setup(props) {
const { display, app } = useApp({
config: props.config,
@ -78,11 +83,8 @@ export default defineComponent({
app,
className,
style,
IS_DSL_NODE_KEY,
display,
toLine,
useComponent,
};
},
});