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

Commit 65556d4

Browse files
committed
Mostly replacing icepick for form-related state (WIP)
1 parent 55360d9 commit 65556d4

File tree

5 files changed

+51
-45
lines changed

5 files changed

+51
-45
lines changed

src/reducers/form-actions-reducer.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ import isValidityInvalid from '../utils/is-validity-invalid';
1515
import fieldActions from '../actions/field-actions';
1616
import toPath from '../utils/to-path';
1717
import initialFieldState from '../constants/initial-field-state';
18-
import i from 'icepick';
19-
import { fieldOrForm, getMeta } from '../utils/create-field';
18+
import { fieldOrForm, getMeta, updateFieldState } from '../utils/create-field';
19+
import assocIn from '../utils/assoc-in';
2020

2121
const resetFieldState = (field) => {
2222
if (!isPlainObject(field)) return field;
@@ -43,15 +43,15 @@ const setInitialFieldState = (field, key) => {
4343
if (!isPlainObject(field)) return field;
4444

4545
if (key === '$form') {
46-
return i.assign(initialFieldState, {
46+
return updateFieldState(initialFieldState, {
4747
value: field.value,
4848
model: field.model,
4949
});
5050
}
5151

5252
if (field.$form) return mapValues(field, resetFieldState);
5353

54-
return i.assign(initialFieldState, {
54+
return updateFieldState(initialFieldState, {
5555
value: field.value,
5656
model: field.model,
5757
});
@@ -296,7 +296,7 @@ export default function formActionsReducer(state, action, localPath) {
296296

297297
case actionTypes.RESET: {
298298
return localPath.length
299-
? i.setIn(state, localPath, resetFieldState(field))
299+
? assocIn(state, localPath, resetFieldState(field))
300300
: resetFieldState(field);
301301
}
302302

src/utils/assoc-in.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import identity from './identity';
2+
3+
function objClone(obj) {
4+
const keys = Object.keys(obj);
5+
const length = keys.length;
6+
const result = {};
7+
let index = 0;
8+
let key;
9+
10+
for (; index < length; index += 1) {
11+
key = keys[index];
12+
result[key] = obj[key];
13+
}
14+
return result;
15+
}
16+
17+
export function assoc(state, key, value) {
18+
const newState = objClone(state);
19+
20+
newState[key] = value;
21+
22+
return newState;
23+
}
24+
25+
export default function assocIn(state, path, value, fn = identity) {
26+
if (!path.length) return value;
27+
28+
const key0 = path[0];
29+
30+
if (path.length === 1) {
31+
return fn(assoc(state, key0, value));
32+
}
33+
34+
return fn(assoc(state, key0, assocIn(state[key0] || {}, path.slice(1), value, fn)));
35+
}

src/utils/update-field.js

Lines changed: 1 addition & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -3,43 +3,9 @@ import get from './get';
33
import mapValues from './map-values';
44
import { createInitialState } from '../reducers/form-reducer';
55
import { updateFieldState } from './create-field';
6-
import identity from './identity';
6+
import assocIn from './assoc-in';
77
import invariant from 'invariant';
88

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;
33-
34-
const key0 = path[0];
35-
36-
if (path.length === 1) {
37-
return fn(assoc(state, key0, value));
38-
}
39-
40-
return fn(assoc(state, key0, assocIn(state[key0] || {}, path.slice(1), value, fn)));
41-
}
42-
439
function tempInitialState(path, initialValue = null) {
4410
if (path.length === 1) return { [path[0]]: initialValue };
4511

src/utils/update-parent-forms.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import i from 'icepick';
22
import get from './get';
3+
import assocIn from './assoc-in';
4+
// import { updateFieldState } from './create-field';
35

46
export default function updateParentForms(state, localPath, updater) {
57
const parentLocalPath = localPath.slice(0, -1);
@@ -16,7 +18,9 @@ export default function updateParentForms(state, localPath, updater) {
1618
? updater(value)
1719
: updater;
1820

19-
const newState = i.setIn(state, [...parentLocalPath, '$form'], i.merge(form, updatedValue));
21+
// const updatedForm = updateFieldState(form, updatedValue);
22+
23+
const newState = assocIn(state, [...parentLocalPath, '$form'], i.merge(form, updatedValue));
2024

2125
if (!parentLocalPath.length) return newState;
2226

src/utils/update-sub-fields.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import get from './get';
2-
import i from 'icepick';
2+
import assocIn from './assoc-in';
3+
import { updateFieldState } from './create-field';
34

45
function updateSubField(subField, newSubState) {
56
// Form
@@ -9,7 +10,7 @@ function updateSubField(subField, newSubState) {
910

1011
Object.keys(subField).forEach((key) => {
1112
if (key === '$form') {
12-
result.$form = i.assign(subField.$form, newSubState);
13+
result.$form = updateFieldState(subField.$form, newSubState);
1314
} else {
1415
result[key] = updateSubField(subField[key], newSubState);
1516
}
@@ -19,7 +20,7 @@ function updateSubField(subField, newSubState) {
1920
}
2021

2122
// Field
22-
return i.assign(subField, newSubState);
23+
return updateFieldState(subField, newSubState);
2324
}
2425

2526
export default function updateSubFields(state, localPath, newState) {
@@ -42,5 +43,5 @@ export default function updateSubFields(state, localPath, newState) {
4243

4344
if (!localPath.length) return updatedField;
4445

45-
return i.assocIn(state, localPath, updatedField);
46+
return assocIn(state, localPath, updatedField);
4647
}

0 commit comments

Comments
 (0)