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

Commit 6856b2a

Browse files
committed
Merge branch 'validation_x_load' of https://github.com/tiagoefmoraes/react-redux-form into tiagoefmoraes-validation_x_load
2 parents 9cf7f9f + 4c51e39 commit 6856b2a

File tree

2 files changed

+37
-4
lines changed

2 files changed

+37
-4
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { createInitialState } from '../form-reducer';
99
import initialFieldState from '../../constants/initial-field-state';
1010
import updateParentForms from '../../utils/update-parent-forms';
1111

12-
function updateFieldValue(field, action) {
12+
function updateFieldValue(field, action, parentModel = undefined) {
1313
const { value, removeKeys, silent, load, model } = action;
1414

1515
const fieldState = (field && field.$form)
@@ -70,13 +70,13 @@ function updateFieldValue(field, action) {
7070
}
7171

7272
const updatedField = mapValues(value, (subValue, index) => {
73-
const subField = field[index] || createInitialState(`${model}.${index}`, subValue);
73+
const subField = field[index] || createInitialState(`${(parentModel ? parentModel + '.' : '') + model}.${index}`, subValue);
7474

7575
if (Object.hasOwnProperty.call(subField, '$form')) {
7676
return updateFieldValue(subField, {
7777
model: index,
7878
value: subValue,
79-
});
79+
}, (parentModel ? parentModel + '.' : '') + model);
8080
}
8181

8282
if (shallowEqual(subValue, subField.value)) {

test/model-actions-spec.js

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { assert } from 'chai';
22
import { combineReducers } from 'redux';
33
import Immutable from 'immutable';
4-
import { actions, modelReducer, formReducer, track } from '../src';
4+
import { actions, modelReducer, formReducer, track, combineForms } from '../src';
55
import {
66
actions as immutableActions,
77
modelReducer as immutableModelReducer,
@@ -22,6 +22,39 @@ describe('model actions', () => {
2222
assert.deepEqual(actual, { bar: 'string' });
2323
});
2424

25+
it('should load array values and form values', () => {
26+
const reducer = combineForms({
27+
user: {
28+
username: '',
29+
items: [{name: 'item 1', subitems: [{name: 'subitem 1'}]}]
30+
},
31+
})
32+
const DATA = {
33+
username: 'loaded',
34+
items: [{name: 'item 1', subitems: [{name: 'subitem 1'}, {name: 'subitem 2'}]}, {name: 'item 2', subitems: [{name: 'subitem 1'}]}],
35+
};
36+
const initial = reducer(undefined, {});
37+
const actual = reducer({}, actions.load('user', DATA));
38+
39+
assert.equal(actual.forms.user.items.$form.model, "user.items");
40+
41+
assert.equal(actual.forms.user.items[0].$form.model, "user.items.0");
42+
assert.equal(actual.forms.user.items[0].name.model, "user.items.0.name");
43+
assert.equal(actual.forms.user.items[0].subitems.$form.model, "user.items.0.subitems");
44+
45+
// assert.equal(actual.forms.user.items[0].subitems, "user.items.0.subitems.0");
46+
assert.equal(actual.forms.user.items[0].subitems[0].$form.model, "user.items.0.subitems.0");
47+
assert.equal(actual.forms.user.items[0].subitems[0].name.model, "user.items.0.subitems.0.name");
48+
assert.equal(actual.forms.user.items[0].subitems[1].$form.model, "user.items.0.subitems.1");
49+
assert.equal(actual.forms.user.items[0].subitems[1].name.model, "user.items.0.subitems.1.name");
50+
51+
assert.equal(actual.forms.user.items[1].$form.model, "user.items.1");
52+
assert.equal(actual.forms.user.items[1].name.model, "user.items.1.name");
53+
assert.equal(actual.forms.user.items[1].subitems.$form.model, "user.items.1.subitems");
54+
assert.equal(actual.forms.user.items[1].subitems[0].$form.model, "user.items.1.subitems.0");
55+
assert.equal(actual.forms.user.items[1].subitems[0].name.model, "user.items.1.subitems.0.name");
56+
});
57+
2558
it('should load model and form stay untouched', () => {
2659
const reducer = combineReducers({
2760
foo: modelReducer('foo'),

0 commit comments

Comments
 (0)