diff --git a/src/components/Form/src/hooks/useForm.ts b/src/components/Form/src/hooks/useForm.ts index ab80409acfb3a4b01a3f9f80e59980cbb56968f5..40f246da09fead4efb78988c90946c7ffe7bae5c 100644 --- a/src/components/Form/src/hooks/useForm.ts +++ b/src/components/Form/src/hooks/useForm.ts @@ -94,7 +94,7 @@ export function useForm(props?: Props): UseFormReturnType { }, appendSchemaByField: async ( - schema: FormSchema, + schema: FormSchema | FormSchema[], prefixField: string | undefined, first: boolean, ) => { diff --git a/src/components/Form/src/hooks/useFormEvents.ts b/src/components/Form/src/hooks/useFormEvents.ts index b90fa8759f410fc4ecdbfa75e2496a52c4ab69c6..768089d0c092d18c74beddf6db3e3e61379566db 100644 --- a/src/components/Form/src/hooks/useFormEvents.ts +++ b/src/components/Form/src/hooks/useFormEvents.ts @@ -152,19 +152,23 @@ export function useFormEvents({ /** * @description: Insert after a certain field, if not insert the last */ - async function appendSchemaByField(schema: FormSchema, prefixField?: string, first = false) { + async function appendSchemaByField( + schema: FormSchema | FormSchema[], + prefixField?: string, + first = false, + ) { const schemaList: FormSchema[] = cloneDeep(unref(getSchema)); const index = schemaList.findIndex((schema) => schema.field === prefixField); - + const _schemaList = isObject(schema) ? [schema as FormSchema] : (schema as FormSchema[]); if (!prefixField || index === -1 || first) { - first ? schemaList.unshift(schema) : schemaList.push(schema); + first ? schemaList.unshift(..._schemaList) : schemaList.push(..._schemaList); schemaRef.value = schemaList; _setDefaultValue(schema); return; } if (index !== -1) { - schemaList.splice(index + 1, 0, schema); + schemaList.splice(index + 1, 0, ..._schemaList); } _setDefaultValue(schema); diff --git a/src/components/Form/src/types/form.ts b/src/components/Form/src/types/form.ts index f54b18946455f849b5125ae8b88fa38c0aaeae99..7bb5ce9a98f66632543421fa337ad8bd05e81b57 100644 --- a/src/components/Form/src/types/form.ts +++ b/src/components/Form/src/types/form.ts @@ -35,7 +35,7 @@ export interface FormActionType { setProps: (formProps: Partial) => Promise; removeSchemaByField: (field: string | string[]) => Promise; appendSchemaByField: ( - schema: FormSchema, + schema: FormSchema | FormSchema[], prefixField: string | undefined, first?: boolean | undefined, ) => Promise; diff --git a/src/views/demo/form/AppendForm.vue b/src/views/demo/form/AppendForm.vue index 7078b9934d69b9ae943899181424df9739319968..f5f3a19798a132fa9b77abae311fd9ec11be6ccb 100644 --- a/src/views/demo/form/AppendForm.vue +++ b/src/views/demo/form/AppendForm.vue @@ -4,6 +4,7 @@ @@ -106,13 +107,51 @@ ); n.value++; } + /** + * @description: 批量添加 + */ + function batchAdd() { + appendSchemaByField( + [ + { + field: `field${n.value}a`, + component: 'Input', + label: '字段' + n.value, + colProps: { + span: 8, + }, + required: true, + }, + { + field: `field${n.value}b`, + component: 'Input', + label: '字段' + n.value, + colProps: { + span: 8, + }, + required: true, + }, + { + field: `${n.value}`, + component: 'Input', + label: ' ', + colProps: { + span: 8, + }, + slot: 'add', + }, + ], + '', + ); + n.value++; + } function del(field) { removeSchemaByField([`field${field}a`, `field${field}b`, `${field}`]); n.value--; } - return { register, handleSubmit, add, del }; + return { register, handleSubmit, add, del, batchAdd }; }, });