From 099a75b875ce9653871883548efa9ee4381e0415 Mon Sep 17 00:00:00 2001 From: Sergio Coimbra Date: Wed, 21 Aug 2024 19:02:42 +0100 Subject: [PATCH 1/2] feat: add isSubmitSuccessful prop --- packages/form-core/src/FormApi.ts | 16 +++++++++++- packages/form-core/tests/FormApi.spec.ts | 31 ++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/packages/form-core/src/FormApi.ts b/packages/form-core/src/FormApi.ts index 017735b07..9ec67c810 100644 --- a/packages/form-core/src/FormApi.ts +++ b/packages/form-core/src/FormApi.ts @@ -270,6 +270,10 @@ export type FormState = { * A counter for tracking the number of submission attempts. */ submissionAttempts: number + /** + * A boolean indicating if the last submission was successful. + */ + isSubmitSuccessful: boolean } function getDefaultFormState( @@ -293,6 +297,7 @@ function getDefaultFormState( isValid: defaultState.isValid ?? false, isValidating: defaultState.isValidating ?? false, submissionAttempts: defaultState.submissionAttempts ?? 0, + isSubmitSuccessful: defaultState.isSubmitSuccessful ?? false, validationMetaMap: defaultState.validationMetaMap ?? { onChange: undefined, onBlur: undefined, @@ -773,6 +778,7 @@ export class FormApi< isSubmitted: false, // Count submission attempts submissionAttempts: old.submissionAttempts + 1, + isSubmitSuccessful: false, // Reset isSubmitSuccessful at the start of submission })) // Don't let invalid forms submit @@ -814,10 +820,18 @@ export class FormApi< await this.options.onSubmit?.({ value: this.state.values, formApi: this }) this.store.batch(() => { - this.store.setState((prev) => ({ ...prev, isSubmitted: true })) + this.store.setState((prev) => ({ + ...prev, + isSubmitted: true, + isSubmitSuccessful: true, // Set isSubmitSuccessful to true on successful submission + })) done() }) } catch (err) { + this.store.setState((prev) => ({ + ...prev, + isSubmitSuccessful: false, // Ensure isSubmitSuccessful is false if an error occurs + })) done() throw err } diff --git a/packages/form-core/tests/FormApi.spec.ts b/packages/form-core/tests/FormApi.spec.ts index 21b85690f..091bad00e 100644 --- a/packages/form-core/tests/FormApi.spec.ts +++ b/packages/form-core/tests/FormApi.spec.ts @@ -18,6 +18,7 @@ describe('form api', () => { errors: [], errorMap: {}, isSubmitting: false, + isSubmitSuccessful: false, isTouched: false, isPristine: true, isDirty: false, @@ -54,6 +55,7 @@ describe('form api', () => { isFormValidating: false, isSubmitted: false, isSubmitting: false, + isSubmitSuccessful: false, isTouched: false, isPristine: true, isDirty: false, @@ -88,6 +90,7 @@ describe('form api', () => { isFormValidating: false, isSubmitted: false, isSubmitting: false, + isSubmitSuccessful: false, isTouched: false, isPristine: true, isDirty: false, @@ -133,6 +136,7 @@ describe('form api', () => { isFormValidating: false, isSubmitted: false, isSubmitting: false, + isSubmitSuccessful: false, isTouched: false, isPristine: true, isDirty: false, @@ -177,6 +181,7 @@ describe('form api', () => { isFormValidating: false, isSubmitted: false, isSubmitting: false, + isSubmitSuccessful: false, isTouched: false, isPristine: true, isDirty: false, @@ -239,6 +244,7 @@ describe('form api', () => { isFormValidating: false, isSubmitted: false, isSubmitting: false, + isSubmitSuccessful: false, isTouched: false, isPristine: true, isDirty: false, @@ -1566,4 +1572,29 @@ describe('form api', () => { }) expect(form.state.errorMap.onChange).toEqual('other validation error') }) + it('should update isSubmitSuccessful correctly during form submission', async () => { + const onSubmit = vi.fn().mockResolvedValue(undefined) + const form = new FormApi({ + defaultValues: { + name: 'test', + }, + onSubmit, + }) + + form.mount() + + expect(form.state.isSubmitSuccessful).toBe(false) + + await form.handleSubmit() + + expect(form.state.isSubmitSuccessful).toBe(true) + expect(onSubmit).toHaveBeenCalledTimes(1) + + // Simulate a failed submission + onSubmit.mockRejectedValueOnce(new Error('Submission failed')) + + await expect(form.handleSubmit()).rejects.toThrow('Submission failed') + + expect(form.state.isSubmitSuccessful).toBe(false) + }) }) From 5bcbe88e88efa3b93f533c64f2eefa7d4878a1c0 Mon Sep 17 00:00:00 2001 From: "autofix-ci[bot]" <114827586+autofix-ci[bot]@users.noreply.github.com> Date: Tue, 22 Oct 2024 05:44:12 +0000 Subject: [PATCH 2/2] ci: apply automated fixes and generate docs --- docs/reference/classes/formapi.md | 52 ++++++++++++------------ docs/reference/type-aliases/formstate.md | 8 ++++ 2 files changed, 34 insertions(+), 26 deletions(-) diff --git a/docs/reference/classes/formapi.md b/docs/reference/classes/formapi.md index 295200fbe..60c8c7e44 100644 --- a/docs/reference/classes/formapi.md +++ b/docs/reference/classes/formapi.md @@ -37,7 +37,7 @@ Constructs a new `FormApi` instance with the given form options. #### Defined in -[packages/form-core/src/FormApi.ts:386](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L386) +[packages/form-core/src/FormApi.ts:391](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L391) ## Properties @@ -51,7 +51,7 @@ A record of field information for each field in the form. #### Defined in -[packages/form-core/src/FormApi.ts:375](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L375) +[packages/form-core/src/FormApi.ts:380](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L380) *** @@ -65,7 +65,7 @@ The options for the form. #### Defined in -[packages/form-core/src/FormApi.ts:359](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L359) +[packages/form-core/src/FormApi.ts:364](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L364) *** @@ -83,7 +83,7 @@ Please use form.useStore() utility to subscribe to state #### Defined in -[packages/form-core/src/FormApi.ts:371](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L371) +[packages/form-core/src/FormApi.ts:376](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L376) *** @@ -97,7 +97,7 @@ A [TanStack Store instance](https://tanstack.com/store/latest/docs/reference/Sto #### Defined in -[packages/form-core/src/FormApi.ts:363](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L363) +[packages/form-core/src/FormApi.ts:368](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L368) ## Methods @@ -121,7 +121,7 @@ deleteField(field): void #### Defined in -[packages/form-core/src/FormApi.ts:1082](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L1082) +[packages/form-core/src/FormApi.ts:1096](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L1096) *** @@ -147,7 +147,7 @@ Gets the field info of the specified field. #### Defined in -[packages/form-core/src/FormApi.ts:998](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L998) +[packages/form-core/src/FormApi.ts:1012](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L1012) *** @@ -173,7 +173,7 @@ Gets the metadata of the specified field. #### Defined in -[packages/form-core/src/FormApi.ts:989](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L989) +[packages/form-core/src/FormApi.ts:1003](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L1003) *** @@ -199,7 +199,7 @@ Gets the value of the specified field. #### Defined in -[packages/form-core/src/FormApi.ts:982](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L982) +[packages/form-core/src/FormApi.ts:996](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L996) *** @@ -217,7 +217,7 @@ Handles the form submission, performs validation, and calls the appropriate onSu #### Defined in -[packages/form-core/src/FormApi.ts:922](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L922) +[packages/form-core/src/FormApi.ts:927](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L927) *** @@ -253,7 +253,7 @@ Inserts a value into an array field at the specified index, shifting the subsequ #### Defined in -[packages/form-core/src/FormApi.ts:1114](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L1114) +[packages/form-core/src/FormApi.ts:1128](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L1128) *** @@ -269,7 +269,7 @@ mount(): void #### Defined in -[packages/form-core/src/FormApi.ts:499](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L499) +[packages/form-core/src/FormApi.ts:504](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L504) *** @@ -305,7 +305,7 @@ Moves the value at the first specified index to the second specified index withi #### Defined in -[packages/form-core/src/FormApi.ts:1232](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L1232) +[packages/form-core/src/FormApi.ts:1246](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L1246) *** @@ -338,7 +338,7 @@ Pushes a value into an array field. #### Defined in -[packages/form-core/src/FormApi.ts:1096](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L1096) +[packages/form-core/src/FormApi.ts:1110](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L1110) *** @@ -371,7 +371,7 @@ Removes a value from an array field at the specified index. #### Defined in -[packages/form-core/src/FormApi.ts:1167](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L1167) +[packages/form-core/src/FormApi.ts:1181](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L1181) *** @@ -407,7 +407,7 @@ Replaces a value into an array field at the specified index. #### Defined in -[packages/form-core/src/FormApi.ts:1141](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L1141) +[packages/form-core/src/FormApi.ts:1155](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L1155) *** @@ -425,7 +425,7 @@ Resets the form state to the default values. #### Defined in -[packages/form-core/src/FormApi.ts:562](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L562) +[packages/form-core/src/FormApi.ts:567](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L567) *** @@ -449,7 +449,7 @@ resetFieldMeta(fieldMeta): Record #### Defined in -[packages/form-core/src/FormApi.ts:1032](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L1032) +[packages/form-core/src/FormApi.ts:1046](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L1046) *** @@ -471,7 +471,7 @@ Updates the form's errorMap #### Defined in -[packages/form-core/src/FormApi.ts:1256](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L1256) +[packages/form-core/src/FormApi.ts:1270](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L1270) *** @@ -499,7 +499,7 @@ Updates the metadata of the specified field. #### Defined in -[packages/form-core/src/FormApi.ts:1017](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L1017) +[packages/form-core/src/FormApi.ts:1031](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L1031) *** @@ -532,7 +532,7 @@ Sets the value of the specified field and optionally updates the touched state. #### Defined in -[packages/form-core/src/FormApi.ts:1056](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L1056) +[packages/form-core/src/FormApi.ts:1070](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L1070) *** @@ -568,7 +568,7 @@ Swaps the values at the specified indices within an array field. #### Defined in -[packages/form-core/src/FormApi.ts:1206](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L1206) +[packages/form-core/src/FormApi.ts:1220](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L1220) *** @@ -590,7 +590,7 @@ Updates the form options and form state. #### Defined in -[packages/form-core/src/FormApi.ts:522](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L522) +[packages/form-core/src/FormApi.ts:527](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L527) *** @@ -612,7 +612,7 @@ Validates all fields in the form using the correct handlers for a given validati #### Defined in -[packages/form-core/src/FormApi.ts:577](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L577) +[packages/form-core/src/FormApi.ts:582](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L582) *** @@ -645,7 +645,7 @@ Validates the children of a specified array in the form starting from a given in #### Defined in -[packages/form-core/src/FormApi.ts:610](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L610) +[packages/form-core/src/FormApi.ts:615](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L615) *** @@ -673,4 +673,4 @@ Validates a specified field in the form using the correct handlers for a given v #### Defined in -[packages/form-core/src/FormApi.ts:649](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L649) +[packages/form-core/src/FormApi.ts:654](https://github.com/TanStack/form/blob/main/packages/form-core/src/FormApi.ts#L654) diff --git a/docs/reference/type-aliases/formstate.md b/docs/reference/type-aliases/formstate.md index c5c3f1ebb..18b30f36d 100644 --- a/docs/reference/type-aliases/formstate.md +++ b/docs/reference/type-aliases/formstate.md @@ -105,6 +105,14 @@ isPristine: boolean; A boolean indicating if none of the form's fields' values have been modified by the user. `True` if the user have not modified any of the fields. Opposite of `isDirty`. +### isSubmitSuccessful + +```ts +isSubmitSuccessful: boolean; +``` + +A boolean indicating if the last submission was successful. + ### isSubmitted ```ts