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

Commit 75027c6

Browse files
committed
Fix setInitial form state shape malformation bug (#1006)
1 parent 0d0f51b commit 75027c6

File tree

2 files changed

+18
-10
lines changed

2 files changed

+18
-10
lines changed

src/reducers/form-actions-reducer.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,19 +37,19 @@ const resetFieldState = (field, customInitialFieldState) => {
3737
);
3838
};
3939

40-
const setInitialFieldState = (customInitialFieldState) => (field, key) => {
40+
const setInitialFieldState = (customInitialFieldState) => field => {
4141
if (!isPlainObject(field)) return field;
4242

43-
if (key === '$form') {
44-
return updateFieldState(customInitialFieldState, {
45-
value: field.value,
46-
model: field.model,
47-
});
48-
}
49-
5043
if (field.$form) {
51-
return mapValues(field, (fieldState) =>
52-
resetFieldState(fieldState, customInitialFieldState));
44+
// eslint-disable-next-line arrow-body-style
45+
return mapValues(field, (fieldState, key) => {
46+
return key === '$form'
47+
? updateFieldState(customInitialFieldState, {
48+
value: field.value,
49+
model: field.model,
50+
})
51+
: resetFieldState(fieldState, customInitialFieldState);
52+
});
5353
}
5454

5555
return updateFieldState(customInitialFieldState, {

test/field-actions-spec.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1107,6 +1107,14 @@ Object.keys(testContexts).forEach((testKey) => {
11071107
value: 'changed',
11081108
});
11091109
});
1110+
1111+
it('should maintain the expected state shape in sub forms', () => {
1112+
const reducer = formReducer('test', { somePrimitive: 1, someArray: [] });
1113+
1114+
const changedState = reducer(undefined, actions.setInitial('test'));
1115+
1116+
assert.isUndefined(changedState.someArray.$form.$form);
1117+
});
11101118
});
11111119

11121120
describe('resetValidity() and resetErrors()', () => {

0 commit comments

Comments
 (0)