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 1 commit
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
16 changes: 15 additions & 1 deletion packages/form-core/src/FormApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,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 @@ -293,6 +297,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 @@ -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
Expand Down Expand Up @@ -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
}
Expand Down
31 changes: 31 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,
isPristine: true,
isDirty: false,
Expand Down Expand Up @@ -54,6 +55,7 @@ describe('form api', () => {
isFormValidating: false,
isSubmitted: false,
isSubmitting: false,
isSubmitSuccessful: false,
isTouched: false,
isPristine: true,
isDirty: false,
Expand Down Expand Up @@ -88,6 +90,7 @@ describe('form api', () => {
isFormValidating: false,
isSubmitted: false,
isSubmitting: false,
isSubmitSuccessful: false,
isTouched: false,
isPristine: true,
isDirty: false,
Expand Down Expand Up @@ -133,6 +136,7 @@ describe('form api', () => {
isFormValidating: false,
isSubmitted: false,
isSubmitting: false,
isSubmitSuccessful: false,
isTouched: false,
isPristine: true,
isDirty: false,
Expand Down Expand Up @@ -177,6 +181,7 @@ describe('form api', () => {
isFormValidating: false,
isSubmitted: false,
isSubmitting: false,
isSubmitSuccessful: false,
isTouched: false,
isPristine: true,
isDirty: false,
Expand Down Expand Up @@ -239,6 +244,7 @@ describe('form api', () => {
isFormValidating: false,
isSubmitted: false,
isSubmitting: false,
isSubmitSuccessful: false,
isTouched: false,
isPristine: true,
isDirty: false,
Expand Down Expand Up @@ -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)
})
})