mirror of
https://github.com/Tencent/tmagic-editor.git
synced 2026-12-04 01:45:08 +08:00
fix(editor): 数组和对象字段补上类型默认值
空默认值按类型收成 [] 或 {},切换类型时清掉自动填上的占位值;并补上 runtime 就绪后才识别自定义迭代器的测试。
This commit is contained in:
parent
ab9fb263ee
commit
cfef073072
@ -52,6 +52,7 @@
|
||||
|
||||
<script setup lang="ts">
|
||||
import { computed, type ComputedRef, inject, onMounted, provide, Ref, ref } from 'vue';
|
||||
import { isEqual } from 'lodash-es';
|
||||
|
||||
import type { DataSchema } from '@tmagic/core';
|
||||
import { TMagicButton, tMagicMessage, tMagicMessageBox } from '@tmagic/design';
|
||||
@ -97,6 +98,23 @@ const fieldTitle = ref('');
|
||||
|
||||
const width = defineModel<number>('width', { default: 670 });
|
||||
|
||||
/** 数组、对象没填默认值时用的空值。每次新建,避免多个字段共用同一个 [] / {} */
|
||||
const emptyDefaultByType = (type: string) => {
|
||||
if (type === 'array') return [];
|
||||
if (type === 'object') return {};
|
||||
return undefined;
|
||||
};
|
||||
|
||||
/** 表单没填时是空字符串;切换类型时自动填上的也是 [] / {} */
|
||||
const isPlaceholderDefaultValue = (value: unknown) =>
|
||||
value === '' || value === undefined || isEqual(value, []) || isEqual(value, {});
|
||||
|
||||
const withTypedDefaultValue = <T extends Record<string, any>>(field: T): T => {
|
||||
const next = emptyDefaultByType(field.type);
|
||||
if (next === undefined || (field.defaultValue !== '' && field.defaultValue !== undefined)) return field;
|
||||
return { ...field, defaultValue: next };
|
||||
};
|
||||
|
||||
const newHandler = () => {
|
||||
fieldValues.value = {};
|
||||
fieldTitle.value = '新增属性';
|
||||
@ -105,10 +123,10 @@ const newHandler = () => {
|
||||
};
|
||||
|
||||
const editField = (row: Record<string, any>, index: number) => {
|
||||
fieldValues.value = {
|
||||
fieldValues.value = withTypedDefaultValue({
|
||||
...row,
|
||||
index,
|
||||
};
|
||||
});
|
||||
fieldTitle.value = `编辑${row.title}`;
|
||||
calcBoxPosition();
|
||||
addDialogVisible.value = true;
|
||||
@ -116,23 +134,37 @@ const editField = (row: Record<string, any>, index: number) => {
|
||||
|
||||
const fieldChange = ({ index, ...value }: Record<string, any>, data: ContainerChangeEventData) => {
|
||||
addDialogVisible.value = false;
|
||||
const normalized = withTypedDefaultValue(value);
|
||||
|
||||
if (index > -1) {
|
||||
emit('change', value, {
|
||||
const changeRecords = (data.changeRecords || []).map((item) => ({
|
||||
propPath: `${props.prop}.${index}.${item.propPath}`,
|
||||
value: item.value,
|
||||
}));
|
||||
|
||||
// 编辑时只回写变更字段。空字符串被收成 [] / {} 后要补进记录,否则原默认值会留在字段上
|
||||
if (normalized !== value) {
|
||||
const defaultValuePath = `${props.prop}.${index}.defaultValue`;
|
||||
const record = changeRecords.find((item) => item.propPath === defaultValuePath);
|
||||
if (record) {
|
||||
record.value = normalized.defaultValue;
|
||||
} else {
|
||||
changeRecords.push({ propPath: defaultValuePath, value: normalized.defaultValue });
|
||||
}
|
||||
}
|
||||
|
||||
emit('change', normalized, {
|
||||
modifyKey: index,
|
||||
changeRecords: (data.changeRecords || []).map((item) => ({
|
||||
propPath: `${props.prop}.${index}.${item.propPath}`,
|
||||
value: item.value,
|
||||
})),
|
||||
changeRecords,
|
||||
});
|
||||
} else {
|
||||
const modifyKey = props.model[props.name].length;
|
||||
emit('change', value, {
|
||||
emit('change', normalized, {
|
||||
modifyKey,
|
||||
changeRecords: [
|
||||
{
|
||||
propPath: `${props.prop}.${modifyKey}`,
|
||||
value,
|
||||
value: normalized,
|
||||
},
|
||||
],
|
||||
});
|
||||
@ -208,10 +240,21 @@ const dataSourceFieldsConfig: FormConfig = [
|
||||
{ text: 'null', value: 'null' },
|
||||
{ text: 'any', value: 'any' },
|
||||
],
|
||||
onChange: (_formState, v: string, { setModel }) => {
|
||||
onChange: (_formState, v: string, { model, setModel }) => {
|
||||
if (!['any', 'array', 'object'].includes(v)) {
|
||||
setModel('fields', []);
|
||||
}
|
||||
|
||||
const current = model?.defaultValue;
|
||||
const next = emptyDefaultByType(v);
|
||||
if (next !== undefined) {
|
||||
if (isPlaceholderDefaultValue(current) && !isEqual(current, next)) {
|
||||
setModel('defaultValue', next);
|
||||
}
|
||||
} else if (isEqual(current, []) || isEqual(current, {})) {
|
||||
// 从数组 / 对象切走时,自动填上的 [] / {} 不能留给字符串等其它类型
|
||||
setModel('defaultValue', '');
|
||||
}
|
||||
return v;
|
||||
},
|
||||
},
|
||||
|
||||
@ -4,7 +4,7 @@
|
||||
* Copyright (C) 2025 Tencent.
|
||||
*/
|
||||
import { beforeEach, describe, expect, test, vi } from 'vitest';
|
||||
import { defineComponent, h } from 'vue';
|
||||
import { defineComponent, h, nextTick } from 'vue';
|
||||
import { mount } from '@vue/test-utils';
|
||||
|
||||
import FieldSelect from '@editor/fields/DataSourceFieldSelect/FieldSelect.vue';
|
||||
@ -236,6 +236,48 @@ describe('FieldSelect', () => {
|
||||
);
|
||||
});
|
||||
|
||||
test('runtime-ready 之后才识别自定义迭代器类型', async () => {
|
||||
const listeners = new Set<() => void>();
|
||||
const stage = {
|
||||
on: (_event: string, cb: () => void) => listeners.add(cb),
|
||||
off: (_event: string, cb: () => void) => listeners.delete(cb),
|
||||
renderer: { runtime: null as null | { getApp: () => { iteratorContainerType: Set<string> } } },
|
||||
};
|
||||
editorService.get.mockReturnValue(stage);
|
||||
editorService.getNodeInfo.mockReturnValue({
|
||||
path: [
|
||||
{ id: 'page1', type: 'page' },
|
||||
{ id: 'iter1', type: 'my-iter' },
|
||||
{ id: 'text1', type: 'text' },
|
||||
],
|
||||
});
|
||||
|
||||
mount(FieldSelect, {
|
||||
props: { modelValue: ['ds-ds1', 'array', 'xxx'] } as any,
|
||||
global: { provide: { mForm: { initValues: { id: 'text1' } } } },
|
||||
});
|
||||
expect(getCascaderOptionsFromFields).toHaveBeenCalledWith(
|
||||
expect.anything(),
|
||||
undefined,
|
||||
expect.anything(),
|
||||
expect.objectContaining({ directArrayElements: false }),
|
||||
);
|
||||
|
||||
stage.renderer.runtime = {
|
||||
getApp: () => ({ iteratorContainerType: new Set(['iterator-container', 'my-iter']) }),
|
||||
};
|
||||
vi.mocked(getCascaderOptionsFromFields).mockClear();
|
||||
listeners.forEach((cb) => cb());
|
||||
await nextTick();
|
||||
|
||||
expect(getCascaderOptionsFromFields).toHaveBeenCalledWith(
|
||||
expect.anything(),
|
||||
undefined,
|
||||
expect.anything(),
|
||||
expect.objectContaining({ directArrayElements: true }),
|
||||
);
|
||||
});
|
||||
|
||||
test('onChangeHandler emit', async () => {
|
||||
const wrapper = mount(FieldSelect, { props: {} as any });
|
||||
await wrapper.find('.fake-cascader').trigger('click');
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user