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

Commit e4a64a7

Browse files
author
Mike Lin
committed
FormReducer now handles model at deep state path
1 parent e5f4f00 commit e4a64a7

File tree

5 files changed

+39
-16
lines changed

5 files changed

+39
-16
lines changed

dist/index.js

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/reducers/form-reducer.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ var _cloneDeep = require('lodash/cloneDeep');
4343

4444
var _cloneDeep2 = _interopRequireDefault(_cloneDeep);
4545

46+
var _isEqual = require('lodash/isEqual');
47+
48+
var _isEqual2 = _interopRequireDefault(_isEqual);
49+
4650
var _actionTypes = require('../action-types');
4751

4852
var actionTypes = _interopRequireWildcard(_actionTypes);
@@ -100,12 +104,11 @@ function createFormReducer(model) {
100104
var action = arguments[1];
101105

102106
var path = (0, _toPath2.default)(action.model);
103-
104-
if (path[0] !== model) {
107+
var modelPath = (0, _toPath2.default)(model);
108+
if (!(0, _isEqual2.default)(path.slice(0, modelPath.length), modelPath)) {
105109
return state;
106110
}
107-
108-
var localPath = path.slice(1);
111+
var localPath = path.slice(modelPath.length);
109112

110113
var form = (0, _cloneDeep2.default)(state);
111114

src/reducers/form-reducer.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import isBoolean from 'lodash/isBoolean';
77
import mapValues from 'lodash/mapValues';
88
import every from 'lodash/every';
99
import cloneDeep from 'lodash/cloneDeep';
10+
import isEqual from 'lodash/isEqual';
1011

1112
import * as actionTypes from '../action-types';
1213

@@ -65,12 +66,11 @@ function createInitialFormState(model) {
6566
function createFormReducer(model) {
6667
return (state = createInitialFormState(model), action) => {
6768
let path = toPath(action.model);
68-
69-
if (path[0] !== model) {
70-
return state;
69+
let modelPath = toPath(model);
70+
if (!isEqual(path.slice(0, modelPath.length), modelPath)) {
71+
return state;
7172
}
72-
73-
let localPath = path.slice(1);
73+
let localPath = path.slice(modelPath.length);
7474

7575
let form = cloneDeep(state);
7676

test/form-reducer-spec.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,14 @@ describe('createFormReducer()', () => {
4343
assert.isObject(getField(actual, 'foo').errors);
4444
});
4545
});
46+
47+
it('should be able to handle model at deep state path', () => {
48+
const reducer = createFormReducer('forms.test');
49+
let actual = reducer(undefined, actions.focus('forms.test.foo'));
50+
assert.deepEqual(actual.fields.foo, {
51+
...initialFieldState,
52+
focus: true,
53+
blur: false
54+
});
55+
});
4656
});

test/model-reducer-spec.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,14 @@ describe('createModelReducer()', () => {
7979
shallowReducer(undefined, actions.change('test.foo.bar[1]', 'two')),
8080
{ original: 'untouched', foo: { bar: [1, 'two', 3] } });
8181
});
82+
83+
it('should handle model at deep state path', () => {
84+
const reducer = createModelReducer('forms.test');
85+
assert.deepEqual(
86+
reducer(undefined, actions.change('forms.test.foo', 'new')),
87+
{ foo: 'new' }
88+
);
89+
90+
})
91+
8292
});

0 commit comments

Comments
 (0)