Skip to content

Commit

Permalink
fix(editor): itemsFunction中补全value
Browse files Browse the repository at this point in the history
feat(form): export createValues方法
  • Loading branch information
parisma authored and roymondchen committed Nov 24, 2022
1 parent d46d611 commit e497ab0
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
12 changes: 10 additions & 2 deletions packages/editor/src/fields/CodeSelect.vue
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@
import { computed, defineEmits, defineProps, inject, watch } from 'vue';
import { isEmpty, map } from 'lodash-es';
import { FormItem, TableConfig } from '@tmagic/form';
import { createValues, FormItem, FormState, TableConfig } from '@tmagic/form';
import { HookType, Id } from '@tmagic/schema';
import { CodeParamStatement, HookData, Services } from '../type';
const services = inject<Services>('services');
const mForm = inject<FormState>('mForm');
const emit = defineEmits(['change']);
const props = defineProps<{
Expand Down Expand Up @@ -65,7 +66,14 @@ const tableConfig = computed<FormItem>(() => {
name: 'params',
label: '参数',
defaultValue: {},
itemsFunction: (row: HookData) => getParamsConfig(row.codeId),
itemsFunction: (row: HookData) => {
const paramsConfig = getParamsConfig(row.codeId);
if (!row.params) row.params = {};
if (isEmpty(row.params)) {
createValues(mForm, paramsConfig, {}, row.params);
}
return paramsConfig;
},
},
],
};
Expand Down
10 changes: 5 additions & 5 deletions packages/form/src/utils/form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ const initItemsValue = (
{ items, name, extensible }: any,
) => {
if (Array.isArray(initValue[name])) {
value[name] = initValue[name].map((v: any, index: number) => init(mForm, items, v, value[name]?.[index]));
value[name] = initValue[name].map((v: any, index: number) => createValues(mForm, items, v, value[name]?.[index]));
} else {
value[name] = init(mForm, items, initValue[name], value[name]);
value[name] = createValues(mForm, items, initValue[name], value[name]);
if (extensible) {
value[name] = Object.assign({}, initValue[name], value[name]);
}
Expand Down Expand Up @@ -129,15 +129,15 @@ const initValueItem = function (

if (!name) {
// 没有配置name,直接跳过
return init(mForm, items, initValue, value);
return createValues(mForm, items, initValue, value);
}

setValue(mForm, value, initValue, item);

return value;
};

const init = function (
export const createValues = function (
mForm: FormState | undefined,
config: FormConfig | TabPaneConfig[] = [],
initValue: FormValue = {},
Expand Down Expand Up @@ -253,7 +253,7 @@ export const initValue = async (
) => {
if (!Array.isArray(config)) throw new Error('config应该为数组');

let valuesTmp = init(mForm, config, toRaw(initValues), {});
let valuesTmp = createValues(mForm, config, toRaw(initValues), {});

const [firstForm] = config as [ContainerCommonConfig];
if (firstForm && typeof firstForm.onInitValue === 'function') {
Expand Down

0 comments on commit e497ab0

Please sign in to comment.