Skip to content

Commit c8b5b6f

Browse files
Rock070Mini-ghost
andauthored
feat(core): add initialValues for submit helper (#18)
* feat: Add initialValue to FormSubmitHelper and use it in submit and reset tests * docs(core): Add "initialValue" property to FormSubmitHelper * test(core): Update useForm tests to format family as an array instead of an object * docs(core): Update documentation to reflect change to initialValue * feat(core): use consistent variable names * docs: update doc * docs: fix typo * docs: fix typo --------- Co-authored-by: Alex Liu <[email protected]>
1 parent 1e489e5 commit c8b5b6f

File tree

3 files changed

+101
-3
lines changed

3 files changed

+101
-3
lines changed

docs/api/use-form.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@ This is your form submission handler, witch will pass your form's `values`. But
229229
```ts
230230
interface FormSubmitHelper {
231231
setSubmitting: (isSubmitting: boolean) => void;
232+
initialValues: Values;
232233
}
233234
```
234235

@@ -240,6 +241,7 @@ This is your form submission handler, witch will pass your form's `values`. But
240241
| Name | Description |
241242
|---------------|-------------------------------|
242243
|`setSubmitting`| Set isSubmitting imperatively.|
244+
|`initialValues`| Form initial values. |
243245

244246
::: warning Important
245247
When the `onSubmit()` function is asynchronous, the `isSubmitting` variable is automatically reset to `false` upon completion. Conversely, if `onSubmit()` is synchronous, you must manually call `setSubmitting(false)` to reset `isSubmitting`.

packages/core/src/composables/useForm.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,9 @@ interface FieldArrayRegistry {
4343
};
4444
}
4545

46-
export interface FormSubmitHelper {
46+
export interface FormSubmitHelper<Values extends FormValues> {
4747
setSubmitting: (isSubmitting: boolean) => void;
48+
readonly initialValues: Values;
4849
}
4950

5051
export type ValidateMode = 'blur' | 'input' | 'change' | 'submit';
@@ -56,7 +57,10 @@ export interface UseFormOptions<Values extends FormValues> {
5657
validateMode?: ValidateMode;
5758
reValidateMode?: ValidateMode;
5859
validateOnMounted?: boolean;
59-
onSubmit: (values: Values, helper: FormSubmitHelper) => void | Promise<any>;
60+
onSubmit: (
61+
values: Values,
62+
helper: FormSubmitHelper<Values>,
63+
) => void | Promise<any>;
6064
onInvalid?: (errors: FormErrors<Values>) => void;
6165
validate?: (values: Values) => void | object | Promise<FormErrors<Values>>;
6266
}
@@ -410,8 +414,11 @@ export function useForm<Values extends FormValues = FormValues>(
410414
return !isEqual(get(initialValues, name), get(state.values, name));
411415
};
412416

413-
const submitHelper: FormSubmitHelper = {
417+
const submitHelper: FormSubmitHelper<Values> = {
414418
setSubmitting,
419+
get initialValues() {
420+
return deepClone(initialValues);
421+
},
415422
};
416423

417424
const runSingleFieldValidateHandler = (name: string, value: unknown) => {

packages/core/tests/composables/useForm.test.ts

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1066,4 +1066,93 @@ describe('useForm', () => {
10661066
await wrapper.find('button[type="reset"]').trigger('click');
10671067
expect(wrapper.find('span#values').text()).toEqual('');
10681068
});
1069+
1070+
it('Get the initial values parameter in the callback of the onSubmit function', async () => {
1071+
const Comp = defineComponent({
1072+
setup() {
1073+
const defaultValues = {
1074+
name: 'Alex',
1075+
age: 10,
1076+
habit: ['basketball', 'football'],
1077+
family: [
1078+
{
1079+
name: 'Tom',
1080+
age: 12,
1081+
habit: ['TV', 'PhotoGraphy'],
1082+
},
1083+
],
1084+
};
1085+
1086+
const { handleSubmit } = useForm({
1087+
initialValues: defaultValues,
1088+
onSubmit(_, { initialValues }) {
1089+
expect(initialValues).toEqual(defaultValues);
1090+
},
1091+
});
1092+
1093+
return {
1094+
handleSubmit,
1095+
};
1096+
},
1097+
template: `
1098+
<form @submit="handleSubmit">
1099+
<button type="submit">Submit</button>
1100+
</form>
1101+
`,
1102+
});
1103+
document.body.innerHTML = `<div id="app"></div>`;
1104+
const wrapper = mount(Comp, {
1105+
attachTo: '#app',
1106+
});
1107+
1108+
await wrapper.find('button').trigger('click');
1109+
});
1110+
1111+
it('Get the initial values parameter in the callback of the onSubmit function after resetFrom execute', async () => {
1112+
const Comp = defineComponent({
1113+
setup() {
1114+
const defaultValues = {
1115+
name: 'Alex',
1116+
age: 10,
1117+
};
1118+
1119+
const resetValues = {
1120+
name: 'Alex',
1121+
age: 10,
1122+
habit: ['basketball', 'football'],
1123+
family: [
1124+
{
1125+
name: 'Tom',
1126+
age: 12,
1127+
habit: ['TV', 'PhotoGraphy'],
1128+
},
1129+
],
1130+
};
1131+
1132+
const { handleSubmit, resetForm } = useForm({
1133+
initialValues: defaultValues,
1134+
onSubmit(_, { initialValues }) {
1135+
expect(initialValues).toEqual(resetValues);
1136+
},
1137+
});
1138+
1139+
resetForm({ values: resetValues });
1140+
1141+
return {
1142+
handleSubmit,
1143+
};
1144+
},
1145+
template: `
1146+
<form @submit="handleSubmit">
1147+
<button type="submit">Submit</button>
1148+
</form>
1149+
`,
1150+
});
1151+
document.body.innerHTML = `<div id="app"></div>`;
1152+
const wrapper = mount(Comp, {
1153+
attachTo: '#app',
1154+
});
1155+
1156+
await wrapper.find('button').trigger('click');
1157+
});
10691158
});

0 commit comments

Comments
 (0)