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

Commit e7cd21c

Browse files
committed
Merge branch 'tiagoefmoraes-validation_x_load'
2 parents 9cf7f9f + de625f0 commit e7cd21c

File tree

2 files changed

+88
-4
lines changed

2 files changed

+88
-4
lines changed

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

Lines changed: 8 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,18 @@ function updateFieldValue(field, action) {
7070
}
7171

7272
const updatedField = mapValues(value, (subValue, index) => {
73-
const subField = field[index] || createInitialState(`${model}.${index}`, subValue);
73+
// TODO: refactor
74+
const subField = field[index]
75+
|| createInitialState(`${`${(parentModel
76+
? `${parentModel}.`
77+
: '')
78+
}${model}`}.${index}`, subValue);
7479

7580
if (Object.hasOwnProperty.call(subField, '$form')) {
7681
return updateFieldValue(subField, {
7782
model: index,
7883
value: subValue,
79-
});
84+
}, parentModel ? `${parentModel}.${model}` : model);
8085
}
8186

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

test/model-actions-spec.js

Lines changed: 80 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,85 @@ 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: [
35+
{ name: 'item 1', subitems: [
36+
{ name: 'subitem 1' },
37+
{ name: 'subitem 2' },
38+
] },
39+
{ name: 'item 2', subitems: [
40+
{ name: 'subitem 1' },
41+
] }],
42+
};
43+
// const initial = reducer(undefined, {});
44+
const actual = reducer({}, actions.load('user', DATA));
45+
46+
assert.equal(
47+
actual.forms.user.items.$form.model,
48+
'user.items'
49+
);
50+
51+
assert.equal(
52+
actual.forms.user.items[0].$form.model,
53+
'user.items.0'
54+
);
55+
assert.equal(
56+
actual.forms.user.items[0].name.model,
57+
'user.items.0.name'
58+
);
59+
assert.equal(
60+
actual.forms.user.items[0].subitems.$form.model,
61+
'user.items.0.subitems'
62+
);
63+
64+
// assert.equal(actual.forms.user.items[0].subitems, "user.items.0.subitems.0");
65+
assert.equal(
66+
actual.forms.user.items[0].subitems[0].$form.model,
67+
'user.items.0.subitems.0'
68+
);
69+
assert.equal(
70+
actual.forms.user.items[0].subitems[0].name.model,
71+
'user.items.0.subitems.0.name'
72+
);
73+
assert.equal(
74+
actual.forms.user.items[0].subitems[1].$form.model,
75+
'user.items.0.subitems.1'
76+
);
77+
assert.equal(
78+
actual.forms.user.items[0].subitems[1].name.model,
79+
'user.items.0.subitems.1.name'
80+
);
81+
82+
assert.equal(
83+
actual.forms.user.items[1].$form.model,
84+
'user.items.1'
85+
);
86+
assert.equal(
87+
actual.forms.user.items[1].name.model,
88+
'user.items.1.name'
89+
);
90+
assert.equal(
91+
actual.forms.user.items[1].subitems.$form.model,
92+
'user.items.1.subitems'
93+
);
94+
assert.equal(
95+
actual.forms.user.items[1].subitems[0].$form.model,
96+
'user.items.1.subitems.0'
97+
);
98+
assert.equal(
99+
actual.forms.user.items[1].subitems[0].name.model,
100+
'user.items.1.subitems.0.name'
101+
);
102+
});
103+
25104
it('should load model and form stay untouched', () => {
26105
const reducer = combineReducers({
27106
foo: modelReducer('foo'),

0 commit comments

Comments
 (0)