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

Commit 3dfbe15

Browse files
committed
Properly updating field state when loading complex models. Fixes #516
1 parent c7f51a3 commit 3dfbe15

File tree

3 files changed

+36
-21
lines changed

3 files changed

+36
-21
lines changed

src/actions/model-actions.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ export function createModelActions(s = defaultStrategies) {
142142

143143
const load = (model, values) => change(model, values, {
144144
silent: true,
145+
load: true,
145146
});
146147

147148
return mapValues({

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

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,28 +10,30 @@ import initialFieldState from '../../constants/initial-field-state';
1010
import updateParentForms from '../../utils/update-parent-forms';
1111

1212
function updateFieldValue(field, action) {
13-
const { value, removeKeys, silent, model } = action;
13+
const { value, removeKeys, silent, load, model } = action;
14+
15+
const fieldState = (field && field.$form)
16+
? field.$form
17+
: field;
1418

1519
const changedFieldProps = {
1620
validated: false,
17-
retouched: field.submitted
21+
retouched: fieldState.submitted
1822
? true
19-
: field.retouched,
23+
: fieldState.retouched,
2024
intents: [{ type: 'validate' }],
21-
pristine: false,
25+
pristine: silent
26+
? fieldState.pristine
27+
: false,
28+
initialValue: load
29+
? value
30+
: fieldState.initialValue,
2231
};
2332

2433
if (shallowEqual(field.value, value)) {
2534
return i.merge(field, changedFieldProps);
2635
}
2736

28-
if (silent) {
29-
return i.merge(field, {
30-
value,
31-
initialValue: value,
32-
});
33-
}
34-
3537
if (removeKeys) {
3638
const valueIsArray = Array.isArray(field.$form.value);
3739
const removeKeysArray = Array.isArray(removeKeys)
@@ -81,7 +83,12 @@ function updateFieldValue(field, action) {
8183
return subField;
8284
}
8385

84-
return i.merge(subField, i.set(changedFieldProps, 'value', subValue));
86+
return i.merge(subField, i.assign(changedFieldProps, {
87+
value: subValue,
88+
initialValue: load
89+
? subValue
90+
: subField.initialValue,
91+
}));
8592
});
8693

8794
const dirtyFormState = i.merge(field.$form || initialFieldState,

test/form-reducer-actions-spec.js

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ describe('formReducer() (V1)', () => {
5858
},
5959
},
6060
{
61-
action: actions.change,
62-
args: ['string', { silent: true }],
61+
action: actions.load,
62+
args: ['string'],
6363
expectedField: {
6464
pristine: true,
6565
value: 'string',
@@ -70,8 +70,8 @@ describe('formReducer() (V1)', () => {
7070
},
7171
},
7272
{
73-
action: actions.change,
74-
args: [42, { silent: true }],
73+
action: actions.load,
74+
args: [42],
7575
expectedField: {
7676
pristine: true,
7777
value: 42,
@@ -82,12 +82,19 @@ describe('formReducer() (V1)', () => {
8282
},
8383
},
8484
{
85-
action: actions.change,
86-
args: [{ foo: 'bar' }, { silent: true }],
85+
action: actions.load,
86+
args: [{ foo: 'bar' }],
8787
expectedField: {
88-
pristine: true,
89-
value: { foo: 'bar' },
90-
initialValue: { foo: 'bar' },
88+
$form: {
89+
pristine: true,
90+
value: { foo: 'bar' },
91+
initialValue: { foo: 'bar' },
92+
},
93+
foo: {
94+
pristine: true,
95+
value: 'bar',
96+
initialValue: 'bar',
97+
},
9198
},
9299
expectedForm: {
93100
pristine: true,

0 commit comments

Comments
 (0)