From 098621892de12b7eeee53872bf0dcddb1beedb2a Mon Sep 17 00:00:00 2001 From: Vinton <45648362+vintonHuang@users.noreply.github.com> Date: Thu, 22 Dec 2022 21:36:36 +0800 Subject: [PATCH] =?UTF-8?q?perf(component):=201.=E4=BC=98=E5=8C=96appendSc?= =?UTF-8?q?hemaByField=E5=8F=AA=E8=83=BD=E5=8D=95=E4=B8=80=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=E4=B8=80=E4=B8=AA=E8=A1=A8=E5=8D=95=E9=A1=B9=E7=9A=84?= =?UTF-8?q?=E9=97=AE=E9=A2=98=EF=BC=8C=E5=8F=AF=E4=BB=A5=E4=BC=A0=E5=85=A5?= =?UTF-8?q?=E6=95=B0=E7=BB=84=E8=A1=A8=E5=8D=95=E9=A1=B9=EF=BC=8C=E7=BB=9F?= =?UTF-8?q?=E4=B8=80=E6=B7=BB=E5=8A=A0=EF=BC=8C=E5=87=8F=E5=B0=91=E9=87=8D?= =?UTF-8?q?=E5=A4=8D=E6=96=B9=E6=B3=95=E8=B0=83=E7=94=A8=202.=20=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0=E6=89=B9=E9=87=8F=E6=B7=BB=E5=8A=A0=E8=A1=A8=E5=8D=95?= =?UTF-8?q?=E9=A1=B9demo=20(#2472)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/Form/src/hooks/useForm.ts | 2 +- .../Form/src/hooks/useFormEvents.ts | 12 ++++-- src/components/Form/src/types/form.ts | 2 +- src/views/demo/form/AppendForm.vue | 41 ++++++++++++++++++- 4 files changed, 50 insertions(+), 7 deletions(-) diff --git a/src/components/Form/src/hooks/useForm.ts b/src/components/Form/src/hooks/useForm.ts index ab80409a..40f246da 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 b90fa875..768089d0 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 f54b1894..7bb5ce9a 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 7078b993..f5f3a197 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 }; }, }); -- GitLab