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

Commit 91224e7

Browse files
committed
Fixing lint issue and adding initialState to form reducer
1 parent de9f1f9 commit 91224e7

File tree

3 files changed

+45
-7
lines changed

3 files changed

+45
-7
lines changed

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
"eslint": "^2.2.0",
5151
"eslint-config-airbnb": "^6.0.2",
5252
"eslint-plugin-react": "^4.1.0",
53+
"estraverse-fb": "^1.3.1",
5354
"immutable": "^3.7.6",
5455
"jsdom": "^8.0.4",
5556
"mocha": "^2.4.5",
@@ -64,6 +65,7 @@
6465
"webpack": "^1.12.14"
6566
},
6667
"dependencies": {
68+
"flat": "^2.0.0",
6769
"icepick": "^1.1.0",
6870
"lodash": "^4.5.1"
6971
},

src/reducers/form-reducer.js

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import isEqual from 'lodash/isEqual';
66
import isPlainObject from 'lodash/isPlainObject';
77
import mapValues from 'lodash/mapValues';
88
import toPath from 'lodash/toPath';
9+
import flatten from 'flat';
910

1011
import actionTypes from '../action-types';
1112
import { isValid } from '../utils';
@@ -83,17 +84,26 @@ function formIsValid(formState) {
8384
}
8485

8586

86-
function createInitialFormState(model) {
87-
return {
88-
...initialFormState,
89-
model,
90-
};
87+
function createInitialFormState(model, initialState) {
88+
const formState = icepick.set(initialFormState,
89+
'model', model);
90+
91+
if (initialState) {
92+
return icepick.set(formState,
93+
'fields',
94+
mapValues(flatten(initialState), (initialValue) =>
95+
icepick.set(initialFieldState,
96+
'initialValue', initialValue))
97+
);
98+
}
99+
100+
return formState;
91101
}
92102

93-
function createFormReducer(model) {
103+
function createFormReducer(model, initialState) {
94104
const modelPath = toPath(model);
95105

96-
return (state = createInitialFormState(model), action) => {
106+
return (state = createInitialFormState(model, initialState), action) => {
97107
if (!action.model) {
98108
return state;
99109
}

test/form-reducer-spec.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,30 @@ describe('createFormReducer()', () => {
6060
blur: false,
6161
});
6262
});
63+
64+
it('should initialize fields given an initial state', () => {
65+
const reducer = createFormReducer('test', {
66+
foo: 'bar',
67+
deep: {
68+
one: 'one',
69+
two: {
70+
three: 'four',
71+
},
72+
},
73+
});
74+
75+
const actual = reducer(undefined, {});
76+
77+
assert.containSubset(actual.fields, {
78+
foo: {
79+
initialValue: 'bar',
80+
},
81+
'deep.one': {
82+
initialValue: 'one',
83+
},
84+
'deep.two.three': {
85+
initialValue: 'four',
86+
},
87+
});
88+
});
6389
});

0 commit comments

Comments
 (0)