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

Commit e51bbc5

Browse files
committed
Shortening icepick -> i, using updateParentForms in validity/pending reducers, adding tests
1 parent 1ef8d1e commit e51bbc5

11 files changed

+74
-51
lines changed

src/actions/model-actions.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import _get from '../utils/get';
22
import identity from 'lodash/identity';
3-
import icepick from 'icepick';
3+
import i from 'icepick';
44

55
import getValue from '../utils/get-value';
66
import isMulti from '../utils/is-multi';
@@ -11,10 +11,10 @@ import { trackable } from '../utils/track';
1111
const defaultStrategies = {
1212
get: _get,
1313
getValue,
14-
splice: icepick.splice,
15-
merge: icepick.merge,
16-
remove: icepick.dissoc,
17-
push: icepick.push,
14+
splice: i.splice,
15+
merge: i.merge,
16+
remove: i.dissoc,
17+
push: i.push,
1818
length: (value) => value.length,
1919
object: {},
2020
array: [],

src/components/control-component.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import _get from '../utils/get';
77
import merge from '../utils/merge';
88
import mapValues from '../utils/map-values';
99
import isPlainObject from 'lodash/isPlainObject';
10-
import icepick from 'icepick';
10+
import i from 'icepick';
1111
import omit from 'lodash/omit';
1212
import handleFocus from '../utils/handle-focus';
1313

@@ -213,7 +213,7 @@ class Control extends Component {
213213
};
214214

215215
if (isPlainObject(mapProps)) {
216-
return icepick.merge(originalProps,
216+
return i.merge(originalProps,
217217
mapValues(mapProps, (value, key) => {
218218
if (typeof value === 'function' && key !== 'component') {
219219
return value(originalProps);
@@ -294,7 +294,7 @@ class Control extends Component {
294294
(validator, key) => dispatch(actions.asyncSetValidity(model,
295295
(_, done) => {
296296
const outerDone = (valid) => {
297-
const validity = icepick.merge(fieldValue.validity, { [key]: valid });
297+
const validity = i.merge(fieldValue.validity, { [key]: valid });
298298

299299
done(validity);
300300
};

src/constants/control-props-map.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import isMulti from '../utils/is-multi';
2-
import icepick from 'icepick';
2+
import i from 'icepick';
33
import actions from '../actions';
44

55
const defaultStrategies = {
66
array: [],
77
object: {},
88
length: (val) => val.length,
9-
push: icepick.push,
9+
push: i.push,
1010
};
1111

1212
function createControlPropsMap(s = defaultStrategies) {

src/reducers/form-reducer.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import _get from '../utils/get';
2-
import icepick from 'icepick';
2+
import i from 'icepick';
33
import arraysEqual from '../utils/arrays-equal';
44
import isPlainObject from 'lodash/isPlainObject';
55
import isArray from 'lodash/isArray';
@@ -68,22 +68,22 @@ export function createInitialState(model, state, customInitialFieldState = {}) {
6868
initialState = mapValues(state, (subState, subModel) =>
6969
createInitialState(getSubModelString(model, subModel), subState, customInitialFieldState));
7070
} else {
71-
return icepick.merge(initialFieldState, {
71+
return i.merge(initialFieldState, {
7272
initialValue: state,
7373
value: state,
7474
model,
7575
...customInitialFieldState,
7676
});
7777
}
7878

79-
const initialForm = icepick.merge(initialFieldState, {
79+
const initialForm = i.merge(initialFieldState, {
8080
initialValue: state,
8181
value: state,
8282
model,
8383
...customInitialFieldState,
8484
});
8585

86-
return icepick.set(initialState, '$form', initialForm);
86+
return i.set(initialState, '$form', initialForm);
8787
}
8888

8989
function wrapFormReducer(plugin, modelPath, initialState) {

src/reducers/form/change-action-reducer.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import actionTypes from '../../action-types';
2-
import icepick from 'icepick';
2+
import i from 'icepick';
33
import get from '../../utils/get';
44
import shallowEqual from '../../utils/shallow-equal';
55
import isPlainObject from 'lodash/isPlainObject';
@@ -20,10 +20,10 @@ function updateFieldValue(field, action) {
2020
};
2121

2222
if (shallowEqual(field.value, value)) {
23-
return icepick.merge(field, changedFieldProps);
23+
return i.merge(field, changedFieldProps);
2424
}
2525

26-
if (silent) return icepick.set(field, 'value', value);
26+
if (silent) return i.set(field, 'value', value);
2727

2828
if (removeKeys) {
2929
const valueIsArray = Array.isArray(field.$form.value);
@@ -42,7 +42,7 @@ function updateFieldValue(field, action) {
4242
result[key] = field[key];
4343
});
4444

45-
return icepick.set(compact(result), '$form', field.$form);
45+
return i.set(compact(result), '$form', field.$form);
4646
}
4747

4848
result = { ...field };
@@ -57,7 +57,7 @@ function updateFieldValue(field, action) {
5757
}
5858

5959
if (!Array.isArray(value) && !isPlainObject(value)) {
60-
return icepick.merge(field, icepick.set(changedFieldProps, 'value', value));
60+
return i.merge(field, i.set(changedFieldProps, 'value', value));
6161
}
6262

6363
const updatedField = mapValues(value, (subValue, index) => {
@@ -71,16 +71,16 @@ function updateFieldValue(field, action) {
7171
return subField;
7272
}
7373

74-
return icepick.merge(subField, icepick.set(changedFieldProps, 'value', subValue));
74+
return i.merge(subField, i.set(changedFieldProps, 'value', subValue));
7575
});
7676

77-
const dirtyFormState = icepick.merge(field.$form || initialFieldState,
78-
icepick.set(changedFieldProps, 'retouched',
77+
const dirtyFormState = i.merge(field.$form || initialFieldState,
78+
i.set(changedFieldProps, 'retouched',
7979
field.submitted || (field.$form && field.$form.retouched)));
8080

8181

82-
return icepick.set(updatedField, '$form',
83-
icepick.set(dirtyFormState, 'value', value));
82+
return i.set(updatedField, '$form',
83+
i.set(dirtyFormState, 'value', value));
8484
}
8585

8686
export default function changeActionReducer(state, action, localPath) {
@@ -92,5 +92,5 @@ export default function changeActionReducer(state, action, localPath) {
9292

9393
if (!localPath.length) return updatedField;
9494

95-
return icepick.setIn(state, localPath, updatedField);
95+
return i.setIn(state, localPath, updatedField);
9696
}
Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
11
import actionTypes from '../../action-types';
22
import updateField from '../../utils/update-field';
3+
import updateParentForms from '../../utils/update-parent-forms';
34

45
export default function pendingActionReducer(state, action, localPath) {
56
if (action.type !== actionTypes.SET_PENDING) {
67
return state;
78
}
89

9-
return updateField(state, localPath, () => ({
10+
const newState = updateField(state, localPath, {
1011
pending: action.pending,
1112
submitted: false,
1213
submitFailed: false,
1314
retouched: false,
14-
}));
15+
});
16+
17+
return updateParentForms(newState, localPath, {
18+
pending: action.pending,
19+
});
1520
}

src/reducers/form/set-pristine-action-reducer.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ export default function setPristineActionReducer(state, action, localPath) {
88
return state;
99
}
1010

11-
const updatedField = updateField(state, localPath, {
11+
const newState = updateField(state, localPath, {
1212
pristine: true,
1313
}, {
1414
pristine: true,
1515
});
1616

17-
return updateParentForms(updatedField, localPath, (form) => ({
17+
return updateParentForms(newState, localPath, (form) => ({
1818
pristine: isPristine(form),
1919
}));
2020
}

src/reducers/form/set-validity-action-reducer.js

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ import toPath from '../../utils/to-path';
1010
import isValid from '../../form/is-valid';
1111
import isValidityValid from '../../utils/is-validity-valid';
1212
import isValidityInvalid from '../../utils/is-validity-invalid';
13-
14-
import icepick from 'icepick';
13+
import updateParentForms from '../../utils/update-parent-forms';
1514

1615
export default function setValidityActionReducer(state, action, localPath) {
1716
if (action.type === actionTypes.SET_FIELDS_VALIDITY) {
@@ -43,19 +42,17 @@ export default function setValidityActionReducer(state, action, localPath) {
4342
? mapValues(validity, inverse)
4443
: !validity;
4544

46-
return updateField(state, localPath, {
45+
const newState = updateField(state, localPath, {
4746
[isErrors ? 'errors' : 'validity']: validity,
4847
[isErrors ? 'validity' : 'errors']: inverseValidity,
4948
validating: false,
5049
validated: true,
5150
valid: isErrors
5251
? !isValidityInvalid(validity)
5352
: isValidityValid(validity),
54-
}, null, (node) => {
55-
if (node.$form) {
56-
return icepick.setIn(node, ['$form', 'valid'], isValid(node));
57-
}
58-
59-
return icepick.set(node, 'valid', isValid(node));
6053
});
54+
55+
return updateParentForms(newState, localPath, (form) => ({
56+
valid: isValid(form),
57+
}));
6158
}

src/reducers/model-reducer.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import _get from '../utils/get';
2-
import icepick from 'icepick';
2+
import i from 'icepick';
33
import arraysEqual from '../utils/arrays-equal';
44
import toPath from '../utils/to-path';
55

66
import actionTypes from '../action-types';
77
import createBatchReducer from '../enhancers/batched-enhancer';
88

99
function icepickSet(state, path, value) {
10-
return icepick.setIn(state, path, value);
10+
return i.setIn(state, path, value);
1111
}
1212

1313
const defaultStrategy = {

src/utils/update-field.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
import icepick from 'icepick';
1+
import i from 'icepick';
22
import get from './get';
33
import mapValues from './map-values';
44
import { createInitialState } from '../reducers/form-reducer';
55

66
function assocIn(state, path, value, fn) {
7-
if (!fn) return icepick.assocIn(state, path, value);
7+
if (!fn) return i.assocIn(state, path, value);
88

99
const key0 = path[0];
1010

1111
if (path.length === 1) {
12-
return fn(icepick.assoc(state, key0, value));
12+
return fn(i.assoc(state, key0, value));
1313
}
1414

15-
return fn(icepick.assoc(state, key0, assocIn(state[key0] || {}, path.slice(1), value, fn)));
15+
return fn(i.assoc(state, key0, assocIn(state[key0] || {}, path.slice(1), value, fn)));
1616
}
1717

1818
function tempInitialState(path) {
@@ -28,7 +28,7 @@ export default function updateField(state, path, newState, newSubState, updater)
2828
let fullState = state;
2929

3030
if (!field) {
31-
fullState = icepick.merge(state, createInitialState(
31+
fullState = i.merge(state, createInitialState(
3232
state.$form.model,
3333
tempInitialState(path)));
3434

@@ -37,7 +37,7 @@ export default function updateField(state, path, newState, newSubState, updater)
3737

3838
const isForm = field.hasOwnProperty('$form');
3939
const fieldPath = isForm
40-
? icepick.push(path, '$form')
40+
? i.push(path, '$form')
4141
: path;
4242

4343
const fieldState = isForm
@@ -51,7 +51,7 @@ export default function updateField(state, path, newState, newSubState, updater)
5151
if (isForm && newSubState) {
5252
const formState = mapValues(field, (subState, key) => {
5353
if (key === '$form') {
54-
return icepick.assign(
54+
return i.assign(
5555
fieldState,
5656
updatedFieldState);
5757
}
@@ -60,15 +60,15 @@ export default function updateField(state, path, newState, newSubState, updater)
6060
? newSubState(subState, updatedFieldState)
6161
: newSubState;
6262

63-
return icepick.assign(subState, updatedSubState);
63+
return i.assign(subState, updatedSubState);
6464
});
6565

6666
if (!path.length) return formState;
6767

6868
return assocIn(fullState, path, formState, updater);
6969
}
7070

71-
return assocIn(fullState, fieldPath, icepick.assign(
71+
return assocIn(fullState, fieldPath, i.assign(
7272
fieldState,
7373
updatedFieldState), updater);
7474
}

0 commit comments

Comments
 (0)