Browse Source

优化appendSchemaByField只能单一添加一个表单项的问题

pull/768/head
cKey 3 years ago
parent
commit
91a55c9e67
  1. 2
      apps/vue/src/components/Form/src/hooks/useForm.ts
  2. 14
      apps/vue/src/components/Form/src/hooks/useFormEvents.ts
  3. 4
      apps/vue/src/components/Form/src/types/form.ts

2
apps/vue/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,
) => {

14
apps/vue/src/components/Form/src/hooks/useFormEvents.ts

@ -60,7 +60,7 @@ export function useFormEvents({
/**
* @description: Set form value
*/
async function setFieldsValue<T extends Recordable<any>>(values: T): Promise<void> {
async function setFieldsValue(values: any): Promise<void> {
const fields = unref(getSchema)
.map((item) => item.field)
.filter(Boolean);
@ -154,19 +154,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);

4
apps/vue/src/components/Form/src/types/form.ts

@ -26,7 +26,7 @@ export interface ButtonProps extends AntdButtonProps {
export interface FormActionType {
submit: () => Promise<void>;
setFieldsValue: <T extends Recordable>(values: T) => Promise<void>;
setFieldsValue: <T>(values: T) => Promise<void>;
resetFields: () => Promise<void>;
getFieldsValue: () => Recordable;
clearValidate: (name?: string | string[]) => Promise<void>;
@ -35,7 +35,7 @@ export interface FormActionType {
setProps: (formProps: Partial<FormProps>) => Promise<void>;
removeSchemaByField: (field: string | string[]) => Promise<void>;
appendSchemaByField: (
schema: FormSchema,
schema: FormSchema | FormSchema[],
prefixField: string | undefined,
first?: boolean | undefined,
) => Promise<void>;

Loading…
Cancel
Save