feat(editor,form): 添加属性配置表单formState扩展函数prop

This commit is contained in:
roymondchen 2023-07-06 14:25:59 +08:00
parent 24bb45e2b9
commit fd53170e59
4 changed files with 26 additions and 6 deletions

View File

@ -61,7 +61,10 @@
<template #props-panel> <template #props-panel>
<slot name="props-panel"> <slot name="props-panel">
<PropsPanel @mounted="(instance: any) => $emit('props-panel-mounted', instance)"> <PropsPanel
:extend-state="extendFormState"
@mounted="(instance: any) => $emit('props-panel-mounted', instance)"
>
<template #props-panel-header> <template #props-panel-header>
<slot name="props-panel-header"></slot> <slot name="props-panel-header"></slot>
</template> </template>

View File

@ -1,7 +1,7 @@
import type { PropType } from 'vue'; import type { PropType } from 'vue';
import type { EventOption } from '@tmagic/core'; import type { EventOption } from '@tmagic/core';
import type { FormConfig } from '@tmagic/form'; import type { FormConfig, FormState } from '@tmagic/form';
import type { MApp, MNode } from '@tmagic/schema'; import type { MApp, MNode } from '@tmagic/schema';
import StageCore, { import StageCore, {
CONTAINER_HIGHLIGHT_CLASS_NAME, CONTAINER_HIGHLIGHT_CLASS_NAME,
@ -137,4 +137,8 @@ export default {
disabledDragStart: { disabledDragStart: {
type: Boolean, type: Boolean,
}, },
extendFormState: {
type: Function as PropType<(state: FormState) => Record<string, any> | Promise<Record<string, any>>>,
},
}; };

View File

@ -1,5 +1,5 @@
<template> <template>
<div class="m-editor-props-panel" v-if="nodes.length === 1"> <div class="m-editor-props-panel" v-show="nodes.length === 1">
<slot name="props-panel-header"></slot> <slot name="props-panel-header"></slot>
<MForm <MForm
ref="configForm" ref="configForm"
@ -8,6 +8,7 @@
:size="propsPanelSize" :size="propsPanelSize"
:init-values="values" :init-values="values"
:config="curFormConfig" :config="curFormConfig"
:extend-state="extendState"
@change="submit" @change="submit"
></MForm> ></MForm>
</div> </div>
@ -17,7 +18,7 @@
import { computed, getCurrentInstance, inject, onMounted, onUnmounted, ref, watchEffect } from 'vue'; import { computed, getCurrentInstance, inject, onMounted, onUnmounted, ref, watchEffect } from 'vue';
import { tMagicMessage } from '@tmagic/design'; import { tMagicMessage } from '@tmagic/design';
import type { FormValue } from '@tmagic/form'; import type { FormState, FormValue } from '@tmagic/form';
import { MForm } from '@tmagic/form'; import { MForm } from '@tmagic/form';
import type { Services } from '@editor/type'; import type { Services } from '@editor/type';
@ -26,6 +27,10 @@ defineOptions({
name: 'MEditorPropsPanel', name: 'MEditorPropsPanel',
}); });
defineProps<{
extendState?: (state: FormState) => Record<string, any> | Promise<Record<string, any>>;
}>();
const emit = defineEmits(['mounted']); const emit = defineEmits(['mounted']);
const internalInstance = getCurrentInstance(); const internalInstance = getCurrentInstance();
@ -78,5 +83,5 @@ const submit = async () => {
} }
}; };
defineExpose({ submit }); defineExpose({ configForm, submit });
</script> </script>

View File

@ -62,6 +62,7 @@ const props = withDefaults(
labelPosition?: string; labelPosition?: string;
keyProp?: string; keyProp?: string;
popperClass?: string; popperClass?: string;
extendState?: (state: FormState) => Record<string, any> | Promise<Record<string, any>>;
}>(), }>(),
{ {
config: () => [], config: () => [],
@ -114,7 +115,7 @@ const formState: FormState = reactive<FormState>({
}, },
}); });
watchEffect(() => { watchEffect(async () => {
formState.initValues = props.initValues; formState.initValues = props.initValues;
formState.lastValues = props.lastValues; formState.lastValues = props.lastValues;
formState.isCompare = props.isCompare; formState.isCompare = props.isCompare;
@ -122,6 +123,13 @@ watchEffect(() => {
formState.keyProp = props.keyProp; formState.keyProp = props.keyProp;
formState.popperClass = props.popperClass; formState.popperClass = props.popperClass;
formState.parentValues = props.parentValues; formState.parentValues = props.parentValues;
if (typeof props.extendState === 'function') {
const state = (await props.extendState(formState)) || {};
Object.entries(state).forEach(([key, value]) => {
formState[key] = value;
});
}
}); });
provide('mForm', formState); provide('mForm', formState);