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

Fix reset in onSubmit #363

Merged
merged 2 commits into from Oct 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 9 additions & 3 deletions src/FinalForm.js
Expand Up @@ -930,7 +930,7 @@ function createForm<FormValues: FormValuesShape>(

reset: (initialValues = state.formState.initialValues) => {
if (state.formState.submitting) {
throw Error('Cannot reset() in onSubmit(), use setTimeout(form.reset)')
state.formState.resetWhileSubmitting = true;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that you should return after state.formState.resetWhileSubmitting = true;, no?

}
state.formState.submitFailed = false
state.formState.submitSucceeded = false
Expand Down Expand Up @@ -1087,15 +1087,21 @@ function createForm<FormValues: FormValuesShape>(
let completeCalled = false
const complete = (errors: ?Object) => {
formState.submitting = false
const { resetWhileSubmitting } = formState
if (resetWhileSubmitting) {
delete formState.resetWhileSubmitting
}
if (errors && hasAnyError(errors)) {
formState.submitFailed = true
formState.submitSucceeded = false
formState.submitErrors = errors
formState.submitError = errors[FORM_ERROR]
markAllFieldsTouched()
} else {
formState.submitFailed = false
formState.submitSucceeded = true
if (!resetWhileSubmitting) {
formState.submitFailed = false
formState.submitSucceeded = true
}
afterSubmit()
}
notifyFormListeners()
Expand Down
13 changes: 10 additions & 3 deletions src/FinalForm.submission.test.js
Expand Up @@ -1094,10 +1094,10 @@ describe('FinalForm.submission', () => {
expect(onSubmit).toHaveBeenCalledTimes(2)
})

it('should NOT allow reset in onSubmit', async () => {
// https://github.com/final-form/final-form/issues/142#issuecomment-402296920
it('should allow reset in onSubmit', async () => {
const onSubmit = (values, form) => {
form.reset()
form.change('foo', 'bar');
}

const form = createForm({ onSubmit })
Expand All @@ -1110,7 +1110,14 @@ describe('FinalForm.submission', () => {
field.mock.calls[0][0].change('bar')
expect(field).toHaveBeenCalledTimes(2)
expect(field.mock.calls[1][0].value).toBe('bar')
expect(() => form.submit()).toThrow(/Cannot reset\(\) in onSubmit\(\)/)

await form.submit()
expect(field).toHaveBeenCalledTimes(4)
expect(field.mock.calls[2][0].submitSucceeded).toBe(false)
expect(field.mock.calls[2][0].value).toBeUndefined()
expect(field.mock.calls[3][0].value).toBe('bar')
await sleep(1)
expect(field).toHaveBeenCalledTimes(4)
})

it('should allow setTimeout(reset) in onSubmit', async () => {
Expand Down