Skip to content

Commit

Permalink
Reset modified state on form submit
Browse files Browse the repository at this point in the history
Submiting did not make this.state.modified resetting to false again.

Issue -> For example, when passing Prompt component (react-router-dom) like so

`<Formol extra={(this.state) => (<Prompt when={this.state.modified} />)}`

It will prompt the user a warning message to confirm the redirection after form submitting. This hangs
the redirection waiting for the user confirmation.

but the confirmation is implicit as soon as he clicks the submit button. Therefore there is no need to let the form in a modified state when
submiting was successful.
  • Loading branch information
Aro Andriamaro committed May 24, 2021
1 parent 5e91bbf commit 2ca3556
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/Formol.jsx
Expand Up @@ -266,6 +266,7 @@ export default class Formol extends React.PureComponent {

if (!Object.keys(errors).length) {
// No errors on submit
this.setState({ modified: false })
if (item === emptyItem) {
// Resetting form if no item was given
this.handleCancel()
Expand Down
25 changes: 25 additions & 0 deletions test/formol/index.test.jsx
Expand Up @@ -2,6 +2,7 @@

import { mount } from 'enzyme'
import React from 'react'
import { act } from 'react-dom/test-utils'

import Formol, { Field } from '../../src'
import InputField from '../../src/fields/InputField'
Expand Down Expand Up @@ -607,4 +608,28 @@ describe('Formol', () => {
false
)
}, 30000)

it('internal value of modified must be false on successful submit', async () => {
const extra = jest.fn()
const wrapper = mount(
<Formol
onSubmit={jest.fn()}
item={{ username: '' }}
extra={({ modified }) => extra(modified)}
>
<Field type="text" name="username">
Username
</Field>
</Formol>
)

wrapper.find('input').simulate('change', { target: { value: 'Toto' } })
expect(wrapper.getDOMNode().checkValidity()).toBeTruthy()
await act(async () => {
await wrapper.find('.Formol_Formol__submit').simulate('submit')
})
const { length } = extra.mock.calls
// last value of `modified` must be false
expect(extra.mock.calls[length - 1][0]).toBeFalsy()
}, 30000)
})

0 comments on commit 2ca3556

Please sign in to comment.