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

Commit 2508a45

Browse files
committed
Removing use of i.merge in update-field
1 parent 65556d4 commit 2508a45

File tree

3 files changed

+27
-16
lines changed

3 files changed

+27
-16
lines changed

src/reducers/form-actions-reducer.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ export default function formActionsReducer(state, action, localPath) {
7474
const fieldState = field && field.$form
7575
? field.$form
7676
: field;
77+
7778
const { intents } = fieldState;
7879

7980
let fieldUpdates = {};

src/utils/create-field.js

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,16 @@ import mapValues from './map-values';
44

55
/* eslint-disable no-use-before-define */
66
export function fieldOrForm(model, value, customInitialFieldState) {
7+
// TODO: create toModel()
8+
const stringModel = Array.isArray(model)
9+
? model.join('.')
10+
: model;
11+
712
if (Array.isArray(value) || isPlainObject(value)) {
8-
return createFormState(model, value, customInitialFieldState);
13+
return createFormState(stringModel, value, customInitialFieldState);
914
}
1015

11-
return createFieldState(model, value, customInitialFieldState);
16+
return createFieldState(stringModel, value, customInitialFieldState);
1217
}
1318
/* eslint-enable no-use-before-define */
1419

@@ -58,3 +63,19 @@ export function createFormState(model, values, customInitialFieldState, options
5863

5964
return state;
6065
}
66+
67+
export function insertFormField(form, fieldModelPath, fieldValue) {
68+
const subPath = fieldModelPath[0];
69+
70+
if (form[subPath]) {
71+
return {
72+
...form,
73+
[subPath]: insertFormField(form[subPath], fieldModelPath.slice(1), fieldValue[subPath]),
74+
};
75+
}
76+
77+
return {
78+
...form,
79+
[subPath]: fieldOrForm(subPath, fieldValue),
80+
};
81+
}

src/utils/update-field.js

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,9 @@
1-
import i from 'icepick';
21
import get from './get';
32
import mapValues from './map-values';
4-
import { createInitialState } from '../reducers/form-reducer';
5-
import { updateFieldState } from './create-field';
3+
import { updateFieldState, fieldOrForm } from './create-field';
64
import assocIn from './assoc-in';
75
import invariant from 'invariant';
86

9-
function tempInitialState(path, initialValue = null) {
10-
if (path.length === 1) return { [path[0]]: initialValue };
11-
12-
return {
13-
[path[0]]: tempInitialState(path.slice(1), initialValue),
14-
};
15-
}
16-
177
export function getFieldAndForm(formState, modelPath) {
188
let field = get(formState, modelPath);
199
let form = formState;
@@ -24,10 +14,9 @@ export function getFieldAndForm(formState, modelPath) {
2414

2515
if (!field) {
2616
const initialValue = get(formState.$form.initialValue, modelPath);
17+
const formModel = formState.$form.model;
2718

28-
form = i.merge(createInitialState(
29-
formState.$form.model,
30-
tempInitialState(modelPath, initialValue)), formState);
19+
form = assocIn(formState, modelPath, fieldOrForm([formModel, ...modelPath], initialValue));
3120

3221
field = get(form, modelPath);
3322
}

0 commit comments

Comments
 (0)