Skip to content
This repository was archived by the owner on Aug 23, 2022. It is now read-only.

Commit ccebe42

Browse files
committed
Updating form pristine value upon dirty/change of field values. Fixes #464
1 parent 3acba22 commit ccebe42

File tree

3 files changed

+32
-3
lines changed

3 files changed

+32
-3
lines changed

src/reducers/form/change-action-reducer.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ function updateFieldValue(field, action) {
1313
const { value, removeKeys, silent, model } = action;
1414

1515
const changedFieldProps = {
16-
pristine: false,
1716
validated: false,
1817
retouched: field.submitted
1918
? true
Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,24 @@
11
import actionTypes from '../../action-types';
22
import updateField from '../../utils/update-field';
3+
import isPristine from '../../form/is-pristine';
4+
import updateParentForms from '../../utils/update-parent-forms';
35

46
export default function setValidityActionReducer(state, action, localPath) {
5-
if (action.type !== actionTypes.SET_DIRTY) {
7+
if (action.type !== actionTypes.SET_DIRTY && action.type !== actionTypes.CHANGE) {
68
return state;
79
}
810

9-
return updateField(state, localPath, {
11+
if (action.type === actionTypes.CHANGE && action.silent) {
12+
return state;
13+
}
14+
15+
const newState = updateField(state, localPath, {
16+
pristine: false,
17+
}, {
1018
pristine: false,
1119
});
20+
21+
return updateParentForms(newState, localPath, (form) => ({
22+
pristine: isPristine(form),
23+
}));
1224
}

test/form-reducer-actions-spec.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ describe('formReducer() (V1)', () => {
2525
validated: false,
2626
value: 'foo',
2727
},
28+
expectedForm: {
29+
pristine: false,
30+
},
2831
},
2932
{
3033
action: actions.change,
@@ -36,6 +39,9 @@ describe('formReducer() (V1)', () => {
3639
value: { foo: 'bar' },
3740
},
3841
},
42+
expectedForm: {
43+
pristine: false,
44+
},
3945
},
4046
{
4147
action: actions.change,
@@ -47,6 +53,9 @@ describe('formReducer() (V1)', () => {
4753
value: [1, 2, 3],
4854
},
4955
},
56+
expectedForm: {
57+
pristine: false,
58+
},
5059
},
5160
{
5261
action: actions.change,
@@ -56,6 +65,9 @@ describe('formReducer() (V1)', () => {
5665
value: 'string',
5766
initialValue: 'string',
5867
},
68+
expectedForm: {
69+
pristine: true,
70+
},
5971
},
6072
{
6173
action: actions.change,
@@ -65,6 +77,9 @@ describe('formReducer() (V1)', () => {
6577
value: 42,
6678
initialValue: 42,
6779
},
80+
expectedForm: {
81+
pristine: true,
82+
},
6883
},
6984
{
7085
action: actions.change,
@@ -74,6 +89,9 @@ describe('formReducer() (V1)', () => {
7489
value: { foo: 'bar' },
7590
initialValue: { foo: 'bar' },
7691
},
92+
expectedForm: {
93+
pristine: true,
94+
},
7795
},
7896
],
7997
[actionTypes.FOCUS]: [

0 commit comments

Comments
 (0)