Commit 098621892de12b7eeee53872bf0dcddb1beedb2a

Authored by Vinton
Committed by GitHub
1 parent 582d7e73

perf(component): 1.优化appendSchemaByField只能单一添加一个表单项的问题,可以传入数组表单项,统一添加,减少重复方法调用 2…

…. 增加批量添加表单项demo (#2472)
src/components/Form/src/hooks/useForm.ts
@@ -94,7 +94,7 @@ export function useForm(props?: Props): UseFormReturnType { @@ -94,7 +94,7 @@ export function useForm(props?: Props): UseFormReturnType {
94 }, 94 },
95 95
96 appendSchemaByField: async ( 96 appendSchemaByField: async (
97 - schema: FormSchema, 97 + schema: FormSchema | FormSchema[],
98 prefixField: string | undefined, 98 prefixField: string | undefined,
99 first: boolean, 99 first: boolean,
100 ) => { 100 ) => {
src/components/Form/src/hooks/useFormEvents.ts
@@ -152,19 +152,23 @@ export function useFormEvents({ @@ -152,19 +152,23 @@ export function useFormEvents({
152 /** 152 /**
153 * @description: Insert after a certain field, if not insert the last 153 * @description: Insert after a certain field, if not insert the last
154 */ 154 */
155 - async function appendSchemaByField(schema: FormSchema, prefixField?: string, first = false) { 155 + async function appendSchemaByField(
  156 + schema: FormSchema | FormSchema[],
  157 + prefixField?: string,
  158 + first = false,
  159 + ) {
156 const schemaList: FormSchema[] = cloneDeep(unref(getSchema)); 160 const schemaList: FormSchema[] = cloneDeep(unref(getSchema));
157 161
158 const index = schemaList.findIndex((schema) => schema.field === prefixField); 162 const index = schemaList.findIndex((schema) => schema.field === prefixField);
159 - 163 + const _schemaList = isObject(schema) ? [schema as FormSchema] : (schema as FormSchema[]);
160 if (!prefixField || index === -1 || first) { 164 if (!prefixField || index === -1 || first) {
161 - first ? schemaList.unshift(schema) : schemaList.push(schema); 165 + first ? schemaList.unshift(..._schemaList) : schemaList.push(..._schemaList);
162 schemaRef.value = schemaList; 166 schemaRef.value = schemaList;
163 _setDefaultValue(schema); 167 _setDefaultValue(schema);
164 return; 168 return;
165 } 169 }
166 if (index !== -1) { 170 if (index !== -1) {
167 - schemaList.splice(index + 1, 0, schema); 171 + schemaList.splice(index + 1, 0, ..._schemaList);
168 } 172 }
169 _setDefaultValue(schema); 173 _setDefaultValue(schema);
170 174
src/components/Form/src/types/form.ts
@@ -35,7 +35,7 @@ export interface FormActionType { @@ -35,7 +35,7 @@ export interface FormActionType {
35 setProps: (formProps: Partial<FormProps>) => Promise<void>; 35 setProps: (formProps: Partial<FormProps>) => Promise<void>;
36 removeSchemaByField: (field: string | string[]) => Promise<void>; 36 removeSchemaByField: (field: string | string[]) => Promise<void>;
37 appendSchemaByField: ( 37 appendSchemaByField: (
38 - schema: FormSchema, 38 + schema: FormSchema | FormSchema[],
39 prefixField: string | undefined, 39 prefixField: string | undefined,
40 first?: boolean | undefined, 40 first?: boolean | undefined,
41 ) => Promise<void>; 41 ) => Promise<void>;
src/views/demo/form/AppendForm.vue
@@ -4,6 +4,7 @@ @@ -4,6 +4,7 @@
4 <BasicForm @register="register" @submit="handleSubmit"> 4 <BasicForm @register="register" @submit="handleSubmit">
5 <template #add="{ field }"> 5 <template #add="{ field }">
6 <Button v-if="Number(field) === 0" @click="add">+</Button> 6 <Button v-if="Number(field) === 0" @click="add">+</Button>
  7 + <Button class="ml-2" v-if="Number(field) === 0" @click="add">批量添加表单配置</Button>
7 <Button v-if="field > 0" @click="del(field)">-</Button> 8 <Button v-if="field > 0" @click="del(field)">-</Button>
8 </template> 9 </template>
9 </BasicForm> 10 </BasicForm>
@@ -106,13 +107,51 @@ @@ -106,13 +107,51 @@
106 ); 107 );
107 n.value++; 108 n.value++;
108 } 109 }
  110 + /**
  111 + * @description: 批量添加
  112 + */
  113 + function batchAdd() {
  114 + appendSchemaByField(
  115 + [
  116 + {
  117 + field: `field${n.value}a`,
  118 + component: 'Input',
  119 + label: '字段' + n.value,
  120 + colProps: {
  121 + span: 8,
  122 + },
  123 + required: true,
  124 + },
  125 + {
  126 + field: `field${n.value}b`,
  127 + component: 'Input',
  128 + label: '字段' + n.value,
  129 + colProps: {
  130 + span: 8,
  131 + },
  132 + required: true,
  133 + },
  134 + {
  135 + field: `${n.value}`,
  136 + component: 'Input',
  137 + label: ' ',
  138 + colProps: {
  139 + span: 8,
  140 + },
  141 + slot: 'add',
  142 + },
  143 + ],
  144 + '',
  145 + );
  146 + n.value++;
  147 + }
109 148
110 function del(field) { 149 function del(field) {
111 removeSchemaByField([`field${field}a`, `field${field}b`, `${field}`]); 150 removeSchemaByField([`field${field}a`, `field${field}b`, `${field}`]);
112 n.value--; 151 n.value--;
113 } 152 }
114 153
115 - return { register, handleSubmit, add, del }; 154 + return { register, handleSubmit, add, del, batchAdd };
116 }, 155 },
117 }); 156 });
118 </script> 157 </script>