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

Commit 55360d9

Browse files
committed
Refactoring assoc/assocIn for updating field state
1 parent febb719 commit 55360d9

File tree

2 files changed

+41
-11
lines changed

2 files changed

+41
-11
lines changed

src/utils/create-field.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,15 @@ function getSubModelString(model, subModel) {
2424
return `${model}.${subModel}`;
2525
}
2626

27+
export function updateFieldState(existingFieldState, updatedFieldState) {
28+
const newField = {
29+
...existingFieldState,
30+
...updatedFieldState,
31+
};
32+
33+
return newField;
34+
}
35+
2736
export default function createFieldState(model, value, customInitialFieldState) {
2837
return {
2938
...initialFieldState,

src/utils/update-field.js

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,42 @@ import i from 'icepick';
22
import get from './get';
33
import mapValues from './map-values';
44
import { createInitialState } from '../reducers/form-reducer';
5+
import { updateFieldState } from './create-field';
6+
import identity from './identity';
57
import invariant from 'invariant';
68

7-
function assocIn(state, path, value, fn) {
8-
if (!path.length) return i.assign(state, value);
9-
if (!fn) return i.assocIn(state, path, value);
9+
function objClone(obj) {
10+
const keys = Object.keys(obj);
11+
const length = keys.length;
12+
const result = {};
13+
let index = 0;
14+
let key;
15+
16+
for (; index < length; index += 1) {
17+
key = keys[index];
18+
result[key] = obj[key];
19+
}
20+
return result;
21+
}
22+
23+
function assoc(state, key, value) {
24+
const newState = objClone(state);
25+
26+
newState[key] = value;
27+
28+
return newState;
29+
}
30+
31+
function assocIn(state, path, value, fn = identity) {
32+
if (!path.length) return value;
1033

1134
const key0 = path[0];
1235

1336
if (path.length === 1) {
14-
return fn(i.assoc(state, key0, value));
37+
return fn(assoc(state, key0, value));
1538
}
1639

17-
return fn(i.assoc(state, key0, assocIn(state[key0] || {}, path.slice(1), value, fn)));
40+
return fn(assoc(state, key0, assocIn(state[key0] || {}, path.slice(1), value, fn)));
1841
}
1942

2043
function tempInitialState(path, initialValue = null) {
@@ -53,7 +76,7 @@ export default function updateField(state, path, newState, newSubState, updater)
5376

5477
const isForm = field.hasOwnProperty('$form');
5578
const fieldPath = isForm
56-
? i.push(path, '$form')
79+
? [...path, '$form']
5780
: path;
5881

5982
const fieldState = isForm
@@ -67,24 +90,22 @@ export default function updateField(state, path, newState, newSubState, updater)
6790
if (isForm && newSubState) {
6891
const formState = mapValues(field, (subState, key) => {
6992
if (key === '$form') {
70-
return i.assign(
71-
fieldState,
72-
updatedFieldState);
93+
return updateFieldState(fieldState, updatedFieldState);
7394
}
7495

7596
const updatedSubState = typeof newSubState === 'function'
7697
? newSubState(subState, updatedFieldState)
7798
: newSubState;
7899

79-
return i.assign(subState, updatedSubState);
100+
return updateFieldState(subState, updatedSubState);
80101
});
81102

82103
if (!path.length) return formState;
83104

84105
return assocIn(fullState, path, formState, updater);
85106
}
86107

87-
return assocIn(fullState, fieldPath, i.assign(
108+
return assocIn(fullState, fieldPath, updateFieldState(
88109
fieldState,
89110
updatedFieldState), updater);
90111
}

0 commit comments

Comments
 (0)