Skip to content

Commit

Permalink
feat(form): adding resetSchema method
Browse files Browse the repository at this point in the history
  • Loading branch information
zuihou committed Apr 22, 2021
1 parent 7e2668f commit c639e49
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/components/Form/src/BasicForm.vue
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@
validateFields,
getFieldsValue,
updateSchema,
resetSchema,
appendSchemaByField,
removeSchemaByFiled,
resetFields,
Expand Down Expand Up @@ -230,6 +231,7 @@
setFieldsValue,
resetFields,
updateSchema,
resetSchema,
setProps,
removeSchemaByFiled,
appendSchemaByField,
Expand Down
5 changes: 5 additions & 0 deletions src/components/Form/src/hooks/useForm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ export function useForm(props?: Props): UseFormReturnType {
form.updateSchema(data);
},

resetSchema: async (data: Partial<FormSchema> | Partial<FormSchema>[]) => {
const form = await getForm();
form.resetSchema(data);
},

clearValidate: async (name?: string | string[]) => {
const form = await getForm();
form.clearValidate(name);
Expand Down
21 changes: 21 additions & 0 deletions src/components/Form/src/hooks/useFormEvents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,26 @@ export function useFormEvents({
schemaRef.value = schemaList;
}

async function resetSchema(data: Partial<FormSchema> | Partial<FormSchema>[]) {
let updateData: Partial<FormSchema>[] = [];
if (isObject(data)) {
updateData.push(data as FormSchema);
}
if (isArray(data)) {
updateData = [...data];
}

const hasField = updateData.every((item) => Reflect.has(item, 'field') && item.field);

if (!hasField) {
error(
'All children of the form Schema array that need to be updated must contain the `field` field'
);
return;
}
schemaRef.value = updateData as FormSchema[];
}

async function updateSchema(data: Partial<FormSchema> | Partial<FormSchema>[]) {
let updateData: Partial<FormSchema>[] = [];
if (isObject(data)) {
Expand Down Expand Up @@ -227,6 +247,7 @@ export function useFormEvents({
validateFields,
getFieldsValue,
updateSchema,
resetSchema,
appendSchemaByField,
removeSchemaByFiled,
resetFields,
Expand Down
1 change: 1 addition & 0 deletions src/components/Form/src/types/form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export interface FormActionType {
getFieldsValue: () => Recordable;
clearValidate: (name?: string | string[]) => Promise<void>;
updateSchema: (data: Partial<FormSchema> | Partial<FormSchema>[]) => Promise<void>;
resetSchema: (data: Partial<FormSchema> | Partial<FormSchema>[]) => Promise<void>;
setProps: (formProps: Partial<FormProps>) => Promise<void>;
removeSchemaByFiled: (field: string | string[]) => Promise<void>;
appendSchemaByField: (
Expand Down

0 comments on commit c639e49

Please sign in to comment.