1
- import actionTypes from '../../ action-types' ;
2
- import updateField , { getFieldAndForm } from '../../ utils/update-field' ;
3
- import updateParentForms from '../../ utils/update-parent-forms' ;
4
- import updateSubFields from '../../ utils/update-sub-fields' ;
5
- import getFieldForm from '../../ utils/get-field-form' ;
6
- import isPristine from '../../ form/is-pristine' ;
7
- import map from '../../ utils/map' ;
1
+ import actionTypes from '../action-types' ;
2
+ import updateField , { getFieldAndForm } from '../utils/update-field' ;
3
+ import updateParentForms from '../utils/update-parent-forms' ;
4
+ import updateSubFields from '../utils/update-sub-fields' ;
5
+ import getFieldForm from '../utils/get-field-form' ;
6
+ import isPristine from '../form/is-pristine' ;
7
+ import map from '../utils/map' ;
8
8
import isPlainObject from 'lodash/isPlainObject' ;
9
- import mapValues from '../../utils/map-values' ;
10
- import inverse from '../../utils/inverse' ;
11
- import isValid , { fieldsValid } from '../../form/is-valid' ;
12
- import isValidityValid from '../../utils/is-validity-valid' ;
13
- import isValidityInvalid from '../../utils/is-validity-invalid' ;
14
- import fieldActions from '../../actions/field-actions' ;
15
- import toPath from '../../utils/to-path' ;
16
- import initialFieldState from '../../constants/initial-field-state' ;
9
+ import mapValues from '../utils/map-values' ;
10
+ import inverse from '../utils/inverse' ;
11
+ import isValid , { fieldsValid } from '../form/is-valid' ;
12
+ import isValidityValid from '../utils/is-validity-valid' ;
13
+ import isValidityInvalid from '../utils/is-validity-invalid' ;
14
+ import fieldActions from '../actions/field-actions' ;
15
+ import toPath from '../utils/to-path' ;
16
+ import initialFieldState from '../constants/initial-field-state' ;
17
+ import i from 'icepick' ;
18
+
19
+ const resetFieldState = ( field , key ) => {
20
+ if ( ! isPlainObject ( field ) || key === '$form' ) return field ;
21
+
22
+ if ( field . $form ) return mapValues ( field , resetFieldState ) ;
23
+
24
+ return i . merge ( initialFieldState , {
25
+ value : field . initialValue ,
26
+ model : field . model ,
27
+ intents : [ { type : 'validate' } ] ,
28
+ } ) ;
29
+ } ;
17
30
18
31
export default function setFocusActionReducer ( state , action , localPath ) {
19
32
const [ field ] = getFieldAndForm ( state , localPath ) ;
20
33
const fieldState = field && field . $form
21
34
? field . $form
22
35
: field ;
23
36
24
- const fieldUpdates = { } ;
25
- const subFieldUpdates = { } ;
37
+ let fieldUpdates = { } ;
38
+ let subFieldUpdates = { } ;
26
39
let parentFormUpdates ;
27
40
28
41
switch ( action . type ) {
29
42
case actionTypes . FOCUS : {
30
- Object . assign ( fieldUpdates , {
43
+ fieldUpdates = {
31
44
focus : true ,
32
45
intents : action . silent
33
46
? [ ]
34
47
: [ action ] ,
35
- } ) ;
48
+ } ;
36
49
37
50
break ;
38
51
}
@@ -41,24 +54,24 @@ export default function setFocusActionReducer(state, action, localPath) {
41
54
case actionTypes . SET_TOUCHED : {
42
55
const fieldForm = getFieldForm ( state , localPath ) . $form ;
43
56
44
- Object . assign ( fieldUpdates , {
57
+ fieldUpdates = {
45
58
focus : action . type === actionTypes . BLUR
46
59
? false
47
60
: field . focus ,
48
61
touched : true ,
49
62
retouched : fieldForm
50
63
? ! ! ( fieldForm . submitted || fieldForm . submitFailed )
51
64
: false ,
52
- } ) ;
65
+ } ;
53
66
54
67
break ;
55
68
}
56
69
57
70
case actionTypes . SET_UNTOUCHED : {
58
- Object . assign ( fieldUpdates , {
71
+ fieldUpdates = {
59
72
focus : false ,
60
73
touched : false ,
61
- } ) ;
74
+ } ;
62
75
63
76
break ;
64
77
}
@@ -67,24 +80,24 @@ export default function setFocusActionReducer(state, action, localPath) {
67
80
case actionTypes . SET_DIRTY : {
68
81
const pristine = action . type === actionTypes . SET_PRISTINE ;
69
82
70
- Object . assign ( fieldUpdates , {
83
+ fieldUpdates = {
71
84
pristine,
72
- } ) ;
85
+ } ;
73
86
74
- Object . assign ( subFieldUpdates , {
87
+ subFieldUpdates = {
75
88
pristine,
76
- } ) ;
89
+ } ;
77
90
78
91
parentFormUpdates = ( form ) => ( { pristine : isPristine ( form ) } ) ;
79
92
80
93
break ;
81
94
}
82
95
83
96
case actionTypes . SET_VALIDATING : {
84
- Object . assign ( fieldUpdates , {
97
+ fieldUpdates = {
85
98
validating : action . validating ,
86
99
validated : ! action . validating ,
87
- } ) ;
100
+ } ;
88
101
89
102
break ;
90
103
}
@@ -104,15 +117,15 @@ export default function setFocusActionReducer(state, action, localPath) {
104
117
? fieldsValid ( field )
105
118
: true ;
106
119
107
- Object . assign ( fieldUpdates , {
120
+ fieldUpdates = {
108
121
[ isErrors ? 'errors' : 'validity' ] : validity ,
109
122
[ isErrors ? 'validity' : 'errors' ] : inverseValidity ,
110
123
validating : false ,
111
124
validated : true ,
112
125
valid : areFieldsValid && ( isErrors
113
126
? ! isValidityInvalid ( validity )
114
127
: isValidityValid ( validity ) ) ,
115
- } ) ;
128
+ } ;
116
129
117
130
parentFormUpdates = ( form ) => ( { valid : isValid ( form ) } ) ;
118
131
@@ -129,28 +142,28 @@ export default function setFocusActionReducer(state, action, localPath) {
129
142
}
130
143
131
144
case actionTypes . RESET_VALIDITY : {
132
- Object . assign ( fieldUpdates , {
145
+ fieldUpdates = {
133
146
valid : initialFieldState . valid ,
134
147
validity : initialFieldState . validity ,
135
148
errors : initialFieldState . errors ,
136
- } ) ;
149
+ } ;
137
150
138
- Object . assign ( subFieldUpdates , {
151
+ subFieldUpdates = {
139
152
valid : initialFieldState . valid ,
140
153
validity : initialFieldState . validity ,
141
154
errors : initialFieldState . errors ,
142
- } ) ;
155
+ } ;
143
156
144
157
break ;
145
158
}
146
159
147
160
case actionTypes . SET_PENDING : {
148
- Object . assign ( fieldUpdates , {
161
+ fieldUpdates = {
149
162
pending : action . pending ,
150
163
submitted : false ,
151
164
submitFailed : false ,
152
165
retouched : false ,
153
- } ) ;
166
+ } ;
154
167
155
168
parentFormUpdates = { pending : action . pending } ;
156
169
@@ -160,51 +173,64 @@ export default function setFocusActionReducer(state, action, localPath) {
160
173
case actionTypes . SET_SUBMITTED : {
161
174
const submitted = ! ! action . submitted ;
162
175
163
- Object . assign ( fieldUpdates , {
176
+ fieldUpdates = {
164
177
pending : false ,
165
178
submitted,
166
179
submitFailed : submitted
167
180
? false
168
181
: fieldState && fieldState . submitFailed ,
169
182
touched : true ,
170
183
retouched : false ,
171
- } ) ;
184
+ } ;
172
185
173
- Object . assign ( subFieldUpdates , {
186
+ subFieldUpdates = {
174
187
submitted,
175
188
submitFailed : submitted
176
189
? false
177
190
: fieldUpdates . submitFailed ,
178
191
retouched : false ,
179
- } ) ;
192
+ } ;
180
193
181
194
break ;
182
195
}
183
196
184
197
case actionTypes . SET_SUBMIT_FAILED : {
185
- Object . assign ( fieldUpdates , {
198
+ fieldUpdates = {
186
199
pending : false ,
187
200
submitted : fieldState . submitted && ! action . submitFailed ,
188
201
submitFailed : ! ! action . submitFailed ,
189
202
touched : true ,
190
203
retouched : false ,
191
- } ) ;
204
+ } ;
192
205
193
- Object . assign ( subFieldUpdates , {
206
+ subFieldUpdates = {
194
207
pending : false ,
195
208
submitted : ! action . submitFailed ,
196
209
submitFailed : ! ! action . submitFailed ,
197
210
touched : true ,
198
211
retouched : false ,
199
- } ) ;
212
+ } ;
200
213
201
214
break ;
202
215
}
203
216
217
+ case actionTypes . RESET :
218
+ case actionTypes . SET_INITIAL : {
219
+ return updateField ( state , localPath , resetFieldState , resetFieldState ) ;
220
+ }
221
+
204
222
case actionTypes . ADD_INTENT : {
205
- Object . assign ( fieldUpdates , {
223
+ fieldUpdates = {
206
224
intents : [ action . intent ] ,
207
- } ) ;
225
+ } ;
226
+
227
+ break ;
228
+ }
229
+
230
+ case actionTypes . CLEAR_INTENTS : {
231
+ fieldUpdates = {
232
+ intents : [ ] ,
233
+ } ;
208
234
209
235
break ;
210
236
}
0 commit comments