Skip to content

Commit

Permalink
feat(reducer/clear-fields): set to initial values if present, o… (red…
Browse files Browse the repository at this point in the history
…ux-form#4498)

* fix issue redux-form#4101 where clearFields did not work as expected with initial values

* docs(actions): add missing `props.clearFields` docs, update `clearFields` behaviour

* docs(actions): fix clearFields description
  • Loading branch information
hugojanruiter authored and Andrew Luca committed Nov 28, 2019
1 parent ccebdcd commit d1e8a40
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 3 deletions.
4 changes: 2 additions & 2 deletions docs/api/ActionCreators.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,10 @@ actions such as `CHANGE` or `BLUR`, the specific field.
### `clearFields(form:String, keepTouched: boolean, persistentSubmitErrors: boolean, ...fields:String)`

> Cleans fields values for all the fields passed in.
> Cleans fields values for all the fields passed in. Will reset to initialValue for each field if has any.
> If the `keepTouched` parameter is `true`, the values of currently touched
> fields will be retained.
> fields will be retained.
> If the `persistentSubmitErrors` parameter is `true`,
> the values of currently submit errors fields will be retained
Expand Down
6 changes: 6 additions & 0 deletions docs/api/Props.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,12 @@ class SimpleForm extends React.Component {
> Clear async error of a field in the Redux store. This is a bound action
> creator, so it returns nothing.
### `clearFields(keepTouched: boolean, persistentSubmitErrors: boolean, ...fields:String)`

> Cleans fields values for all the fields passed in. Will reset to initialValue for each field if has any.
> If the keepTouched parameter is true, the values of currently touched fields will be retained.
> If the persistentSubmitErrors parameter is true, the values of currently submit errors fields will be retained
### `destroy() : Function`

> Destroys the form state in the Redux store. By default, this will be called
Expand Down
58 changes: 58 additions & 0 deletions src/__tests__/helpers/reducer.clearFields.js
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,64 @@ const describeClearFields = (reducer, expect, { fromJS }) => () => {
}
})
})

it('should clear fields value and set it to inital, clear touched, clear submitErrors', () => {
const state = reducer(
fromJS({
foo: {
registeredFields: {
myField: { type: 'Field', name: 'myField' },
myOtherField: { type: 'Field', name: 'myOtherField' }
},
values: {
myField: 'value',
myOtherField: 'otherValue'
},
initial: {
myField: 'initialValue'
},
asyncErrors: {
myField: 'async error',
myOtherField: 'async error'
},
submitErrors: {
myField: 'submit error',
myOtherField: 'submit error'
},
fields: {
myField: {
touched: true
},
myOtherField: {
touched: true
}
},
error: 'some global error',
anyTouched: true
}
}),
clearFields('foo', false, false, 'myField', 'myOtherField')
)
expect(state).toEqualMap({
foo: {
registeredFields: {
myField: { type: 'Field', name: 'myField' },
myOtherField: { type: 'Field', name: 'myOtherField' }
},
fields: {
myField: {},
myOtherField: {}
},
initial: {
myField: 'initialValue'
},
values: {
myField: 'initialValue'
},
error: 'some global error'
}
})
})
}

export default describeClearFields
5 changes: 4 additions & 1 deletion src/createReducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,6 @@ function createReducer<M, L>(structure: Structure<M, L>) {
) {
let result = state
fields.forEach(field => {
result = deleteInWithCleanUp(result, `values.${field}`)
result = deleteInWithCleanUp(result, `asyncErrors.${field}`)
if (!persistentSubmitErrors) {
result = deleteInWithCleanUp(result, `submitErrors.${field}`)
Expand All @@ -360,6 +359,10 @@ function createReducer<M, L>(structure: Structure<M, L>) {
if (!keepTouched) {
result = deleteIn(result, `fields.${field}.touched`)
}
const values = getIn(state, `initial.${field}`)
result = values
? setIn(result, `values.${field}`, values)
: deleteInWithCleanUp(result, `values.${field}`)
})
const anyTouched = some(keys(getIn(result, 'registeredFields')), key =>
getIn(result, `fields.${key}.touched`)
Expand Down

0 comments on commit d1e8a40

Please sign in to comment.