1
+ import every from 'lodash/every' ;
1
2
import get from 'lodash/get' ;
2
- import set from 'lodash/set' ;
3
- import merge from 'lodash/merge' ;
4
- import toPath from 'lodash/toPath' ;
5
- import isPlainObject from 'lodash/isPlainObject' ;
3
+ import icepick from 'icepick' ;
6
4
import isBoolean from 'lodash/isBoolean' ;
7
- import mapValues from 'lodash/mapValues' ;
8
- import every from 'lodash/every' ;
9
- import cloneDeep from 'lodash/cloneDeep' ;
10
5
import isEqual from 'lodash/isEqual' ;
6
+ import isPlainObject from 'lodash/isPlainObject' ;
7
+ import mapValues from 'lodash/mapValues' ;
8
+ import toPath from 'lodash/toPath' ;
11
9
12
10
import * as actionTypes from '../action-types' ;
13
11
14
12
function setField ( state , localPath , props ) {
15
13
if ( ! localPath . length ) {
16
- return merge ( cloneDeep ( state ) , props ) ;
17
- } ;
14
+ return icepick . merge ( state , props ) ;
15
+ }
18
16
19
- return merge ( cloneDeep ( state ) , {
17
+ return icepick . merge ( state , {
20
18
fields : {
21
19
[ localPath . join ( '.' ) ] : {
22
20
...initialFieldState ,
@@ -28,7 +26,7 @@ function setField(state, localPath, props) {
28
26
}
29
27
30
28
function getField ( state , path ) {
31
- let localPath = toPath ( path ) ;
29
+ const localPath = toPath ( path ) ;
32
30
33
31
return get (
34
32
state ,
@@ -59,7 +57,7 @@ const initialFormState = {
59
57
function createInitialFormState ( model ) {
60
58
return {
61
59
...initialFormState ,
62
- model : model
60
+ model
63
61
} ;
64
62
}
65
63
@@ -68,116 +66,112 @@ function createFormReducer(model) {
68
66
69
67
return ( state = createInitialFormState ( model ) , action ) => {
70
68
if ( ! action . model ) return state ;
71
-
72
- let path = toPath ( action . model ) ;
69
+
70
+ const path = toPath ( action . model ) ;
73
71
74
72
if ( ! isEqual ( path . slice ( 0 , modelPath . length ) , modelPath ) ) {
75
73
return state ;
76
74
}
77
75
78
- let localPath = path . slice ( modelPath . length ) ;
79
-
80
- let form = cloneDeep ( state ) ;
76
+ const localPath = path . slice ( modelPath . length ) ;
81
77
82
78
switch ( action . type ) {
83
79
case actionTypes . FOCUS :
84
- return setField ( form , localPath , {
80
+ return setField ( state , localPath , {
85
81
focus : true ,
86
82
blur : false
87
83
} ) ;
88
84
89
85
case actionTypes . CHANGE :
90
86
case actionTypes . SET_DIRTY :
91
- merge ( form , {
87
+ state = icepick . merge ( state , {
92
88
dirty : true ,
93
89
pristine : false ,
94
90
} ) ;
95
91
96
- return setField ( form , localPath , {
92
+ return setField ( state , localPath , {
97
93
dirty : true ,
98
94
pristine : false
99
95
} ) ;
100
96
101
97
case actionTypes . BLUR :
102
98
case actionTypes . SET_TOUCHED :
103
- return setField ( form , localPath , {
99
+ return setField ( state , localPath , {
104
100
touched : true ,
105
101
untouched : false ,
106
102
focus : false ,
107
103
blur : true
108
104
} ) ;
109
105
110
106
case actionTypes . SET_PENDING :
111
- return setField ( form , localPath , {
107
+ return setField ( state , localPath , {
112
108
pending : action . pending ,
113
109
submitted : false
114
110
} ) ;
115
111
116
112
case actionTypes . SET_VALIDITY :
117
- let errors = isPlainObject ( action . validity )
113
+ const errors = isPlainObject ( action . validity )
118
114
? {
119
- ...getField ( form , localPath ) . errors ,
115
+ ...getField ( state , localPath ) . errors ,
120
116
...mapValues ( action . validity , ( valid ) => ! valid )
121
117
}
122
118
: ! action . validity ;
123
119
124
- form = setField ( form , localPath , {
125
- errors : errors ,
120
+ state = setField ( state , localPath , {
121
+ errors,
126
122
valid : isBoolean ( errors )
127
123
? errors
128
124
: every ( errors , ( error ) => ! error )
129
125
} ) ;
130
126
131
- return merge ( form , {
132
- valid : every ( mapValues ( form . fields , ( field ) => field . valid ) )
133
- && every ( form . errors , ( error ) => ! error )
127
+ return icepick . merge ( state , {
128
+ valid : every ( mapValues ( state . fields , ( field ) => field . valid ) )
129
+ && every ( state . errors , ( error ) => ! error )
134
130
} ) ;
135
131
136
- break ;
137
-
138
132
case actionTypes . SET_PRISTINE :
139
- form = setField ( form , localPath , {
133
+ state = setField ( state , localPath , {
140
134
dirty : false ,
141
135
pristine : true
142
136
} ) ;
143
137
144
- let formIsPristine = every ( mapValues ( form . fields , ( field ) => field . pristine ) ) ;
138
+ const formIsPristine = every ( mapValues ( state . fields , ( field ) => field . pristine ) ) ;
145
139
146
- return merge ( form , {
140
+ return icepick . merge ( state , {
147
141
pristine : formIsPristine ,
148
142
dirty : ! formIsPristine
149
143
} ) ;
150
144
151
145
case actionTypes . SET_UNTOUCHED :
152
- return setField ( form , localPath , {
146
+ return setField ( state , localPath , {
153
147
touched : false ,
154
148
untouched : true
155
149
} ) ;
156
150
157
151
case actionTypes . SET_SUBMITTED :
158
- return setField ( form , localPath , {
152
+ return setField ( state , localPath , {
159
153
pending : false ,
160
154
submitted : ! ! action . submitted
161
155
} ) ;
162
156
163
157
case actionTypes . SET_INITIAL :
164
158
case actionTypes . RESET :
165
- return setField ( form , localPath , initialFieldState ) ;
159
+ return setField ( state , localPath , initialFieldState ) ;
166
160
167
161
case actionTypes . SET_VIEW_VALUE :
168
- return setField ( form , localPath , {
162
+ return setField ( state , localPath , {
169
163
viewValue : action . value
170
164
} ) ;
171
165
172
166
default :
173
- return form ;
167
+ return state ;
174
168
}
175
- }
169
+ } ;
176
170
}
177
171
178
172
export {
179
173
createFormReducer ,
180
174
initialFieldState ,
181
175
initialFormState ,
182
176
getField
183
- }
177
+ } ;
0 commit comments