Skip to content

Commit

Permalink
feat(editor,form): 新增属性配置表单error事件
Browse files Browse the repository at this point in the history
  • Loading branch information
roymondchen committed Dec 21, 2023
1 parent d981014 commit fbe1d88
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 6 deletions.
4 changes: 4 additions & 0 deletions packages/editor/src/Editor.vue
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@
<PropsPanel
:extend-state="extendFormState"
@mounted="(instance: any) => $emit('props-panel-mounted', instance)"
@form-error="(e: any) => $emit('props-form-error', e)"
@submit-error="(e: any) => $emit('props-submit-error', e)"
>
<template #props-panel-header>
<slot name="props-panel-header"></slot>
Expand Down Expand Up @@ -137,6 +139,8 @@ defineOptions({
const emit = defineEmits<{
'props-panel-mounted': [instance: InstanceType<typeof PropsPanel>];
'update:modelValue': [value: MApp | null];
'props-form-error': [e: any];
'props-submit-error': [e: any];
}>();
const props = withDefaults(defineProps<EditorProps>(), defaultEditorProps);
Expand Down
12 changes: 7 additions & 5 deletions packages/editor/src/layouts/PropsPanel.vue
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@
:config="curFormConfig"
:extend-state="extendState"
@change="submit"
@error="errorHandler"
></MForm>
</div>
</template>

<script lang="ts" setup>
import { computed, getCurrentInstance, inject, onBeforeUnmount, onMounted, ref, watchEffect } from 'vue';
import { tMagicMessage } from '@tmagic/design';
import type { FormState, FormValue } from '@tmagic/form';
import { MForm } from '@tmagic/form';
Expand All @@ -33,7 +33,7 @@ defineProps<{
extendState?: (state: FormState) => Record<string, any> | Promise<Record<string, any>>;
}>();
const emit = defineEmits(['mounted']);
const emit = defineEmits(['mounted', 'submit-error', 'form-error']);
const internalInstance = getCurrentInstance();
const values = ref<FormValue>({});
Expand Down Expand Up @@ -79,11 +79,13 @@ const submit = async () => {
const values = await configForm.value?.submitForm();
services?.editorService.update(values);
} catch (e: any) {
console.error(e);
tMagicMessage.closeAll();
tMagicMessage.error(e.message);
emit('submit-error', e);
}
};
const errorHandler = (e: any) => {
emit('form-error', e);
};
defineExpose({ configForm, submit });
</script>
5 changes: 4 additions & 1 deletion packages/form/src/Form.vue
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ const props = withDefaults(
},
);
const emit = defineEmits(['change', 'field-input', 'field-change']);
const emit = defineEmits(['change', 'error', 'field-input', 'field-change']);
const tMagicForm = ref<InstanceType<typeof TMagicForm>>();
const initialized = ref(false);
Expand Down Expand Up @@ -182,6 +182,8 @@ defineExpose({
await tMagicForm.value?.validate();
return native ? values.value : cloneDeep(toRaw(values.value));
} catch (invalidFields: any) {
emit('error', invalidFields);
const error: string[] = [];
Object.entries(invalidFields).forEach(([, ValidateError]) => {
Expand All @@ -191,6 +193,7 @@ defineExpose({
if (!field && message) error.push(`${message}`);
});
});
throw new Error(error.join('<br>'));
}
},
Expand Down
7 changes: 7 additions & 0 deletions playground/src/pages/Editor.vue
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
:moveable-options="moveableOptions"
:auto-scroll-into-view="true"
:stage-rect="stageRect"
@props-submit-error="propsSubmitErrorHandler"
>
<template #workspace-content>
<DeviceGroup ref="deviceGroup" v-model="stageRect"></DeviceGroup>
Expand Down Expand Up @@ -237,6 +238,12 @@ editorService.usePlugin({
onBeforeUnmount(() => {
editorService.removeAllPlugins();
});
const propsSubmitErrorHandler = async (e: any) => {
console.error(e);
tMagicMessage.closeAll();
tMagicMessage.error(e.message);
};
</script>

<style lang="scss">
Expand Down

0 comments on commit fbe1d88

Please sign in to comment.