Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add isSubmitSuccessful prop #919

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 26 additions & 26 deletions docs/reference/classes/formapi.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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)

***

Expand All @@ -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)

***

Expand All @@ -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)

***

Expand All @@ -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

Expand All @@ -121,7 +121,7 @@ deleteField<TField>(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)

***

Expand All @@ -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)

***

Expand All @@ -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)

***

Expand All @@ -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)

***

Expand All @@ -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)

***

Expand Down Expand Up @@ -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)

***

Expand All @@ -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)

***

Expand Down Expand Up @@ -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)

***

Expand Down Expand Up @@ -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)

***

Expand Down Expand Up @@ -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)

***

Expand Down Expand Up @@ -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)

***

Expand All @@ -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)

***

Expand All @@ -449,7 +449,7 @@ resetFieldMeta<TField>(fieldMeta): Record<TField, FieldMeta>

#### 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)

***

Expand All @@ -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)

***

Expand Down Expand Up @@ -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)

***

Expand Down Expand Up @@ -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)

***

Expand Down Expand Up @@ -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)

***

Expand All @@ -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)

***

Expand All @@ -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)

***

Expand Down Expand Up @@ -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)

***

Expand Down Expand Up @@ -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)
8 changes: 8 additions & 0 deletions docs/reference/type-aliases/formstate.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 15 additions & 1 deletion packages/form-core/src/FormApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,10 @@ export type FormState<TFormData> = {
* A counter for tracking the number of submission attempts.
*/
submissionAttempts: number
/**
* A boolean indicating if the last submission was successful.
*/
isSubmitSuccessful: boolean
}

function getDefaultFormState<TFormData>(
Expand All @@ -326,6 +330,7 @@ function getDefaultFormState<TFormData>(
isValid: defaultState.isValid ?? false,
isValidating: defaultState.isValidating ?? false,
submissionAttempts: defaultState.submissionAttempts ?? 0,
isSubmitSuccessful: defaultState.isSubmitSuccessful ?? false,
validationMetaMap: defaultState.validationMetaMap ?? {
onChange: undefined,
onBlur: undefined,
Expand Down Expand Up @@ -926,6 +931,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
Expand Down Expand Up @@ -967,10 +973,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
}
Expand Down
32 changes: 32 additions & 0 deletions packages/form-core/tests/FormApi.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ describe('form api', () => {
errors: [],
errorMap: {},
isSubmitting: false,
isSubmitSuccessful: false,
isTouched: false,
isBlurred: false,
isPristine: true,
Expand Down Expand Up @@ -55,6 +56,7 @@ describe('form api', () => {
isFormValidating: false,
isSubmitted: false,
isSubmitting: false,
isSubmitSuccessful: false,
isTouched: false,
isBlurred: false,
isPristine: true,
Expand Down Expand Up @@ -90,6 +92,7 @@ describe('form api', () => {
isFormValidating: false,
isSubmitted: false,
isSubmitting: false,
isSubmitSuccessful: false,
isTouched: false,
isBlurred: false,
isPristine: true,
Expand Down Expand Up @@ -136,6 +139,7 @@ describe('form api', () => {
isFormValidating: false,
isSubmitted: false,
isSubmitting: false,
isSubmitSuccessful: false,
isTouched: false,
isBlurred: false,
isPristine: true,
Expand Down Expand Up @@ -181,6 +185,7 @@ describe('form api', () => {
isFormValidating: false,
isSubmitted: false,
isSubmitting: false,
isSubmitSuccessful: false,
isTouched: false,
isBlurred: false,
isPristine: true,
Expand Down Expand Up @@ -245,6 +250,7 @@ describe('form api', () => {
isFormValidating: false,
isSubmitted: false,
isSubmitting: false,
isSubmitSuccessful: false,
isTouched: false,
isBlurred: false,
isPristine: true,
Expand Down Expand Up @@ -2023,4 +2029,30 @@ describe('form api', () => {
expect(form.state.canSubmit).toBe(true)
expect(passconfirmField.state.meta.errors.length).toBe(0)
})

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)
})
})
Loading