roymondchen f583c7daec feat(editor,data-source): 数据源支持内置"设置数据"方法
支持通过事件调用数据源的 setData 方法,可以选择数据源字段并根据字段类型动态设置数据;
重构 CodeParams 参数配置支持动态类型; DataSourceFieldSelect 支持指定数据源ID;
常量抽取到 utils/const.ts

Made-with: Cursor
2026-04-07 18:25:35 +08:00

87 lines
1.9 KiB
Vue

<template>
<MForm
ref="form"
:config="codeParamsConfig"
:init-values="model"
:disabled="disabled"
:size="size"
:watch-props="false"
@change="onParamsChangeHandler"
></MForm>
</template>
<script lang="ts" setup>
import { computed, useTemplateRef } from 'vue';
import { type ContainerChangeEventData, type FormItemConfig, type FormValue, MForm } from '@tmagic/form';
import type { CodeParamStatement } from '@editor/type';
import { error } from '@editor/utils';
defineOptions({
name: 'MEditorCodeParams',
});
const props = defineProps<{
model: any;
size?: 'small' | 'default' | 'large';
disabled?: boolean;
name: string;
paramsConfig: CodeParamStatement[];
}>();
const emit = defineEmits(['change']);
const formRef = useTemplateRef<InstanceType<typeof MForm>>('form');
const getFormConfig = (items: FormItemConfig[] = []) => [
{
type: 'fieldset',
items,
legend: '参数',
labelWidth: '120px',
name: props.name,
},
];
const codeParamsConfig = computed(() =>
getFormConfig(
props.paramsConfig.map(({ name, text, extra, ...config }) => {
let { type } = config;
if (typeof type === 'function') {
type = type(undefined, {
model: props.model[props.name],
});
}
if (type && ['data-source-field-select', 'vs-code'].includes(type)) {
return {
...config,
name,
text,
extra,
};
}
return {
type: 'data-source-field-select' as const,
name,
text,
extra,
fieldConfig: config,
};
}) as FormItemConfig[],
),
);
/**
* 参数值修改更新
*/
const onParamsChangeHandler = async (v: FormValue, eventData: ContainerChangeEventData) => {
try {
const value = await formRef.value?.submitForm(true);
emit('change', value, eventData);
} catch (e) {
error(e);
}
};
</script>