1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268 | 1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
14×
14×
14×
14×
14×
14×
13×
1×
14×
4×
4×
4×
16×
16×
1×
16×
16×
16×
10×
16×
12×
16×
32×
32×
32×
3×
32×
6×
6×
6×
6×
38×
38×
4×
4×
4×
4×
4×
2×
2×
2×
4×
5×
5×
8×
8×
6×
11×
11×
10×
10×
2×
4×
4×
4×
4×
1×
1×
1×
1×
1×
15×
15×
27×
27×
5×
5×
8×
10×
10×
183×
183×
183×
351×
351×
164×
187×
3×
1×
2×
2×
184×
4×
181×
1×
7×
1×
1×
1×
1×
7×
10×
10×
10×
10×
10×
10×
10×
10×
10×
2×
8×
7×
| import { ADD_ARRAY_VALUE, AUTOFILL, BLUR, CHANGE, DESTROY, FOCUS, INITIALIZE, REMOVE_ARRAY_VALUE, RESET, START_ASYNC_VALIDATION,
START_SUBMIT, STOP_ASYNC_VALIDATION, STOP_SUBMIT, SUBMIT_FAILED, SWAP_ARRAY_VALUES, TOUCH, UNTOUCH } from './actionTypes';
import mapValues from './mapValues';
import read from './read';
import write from './write';
import getValuesFromState from './getValuesFromState';
import initializeState from './initializeState';
import resetState from './resetState';
import setErrors from './setErrors';
import {makeFieldValue} from './fieldValue';
import normalizeFields from './normalizeFields';
export const globalErrorKey = '_error';
export const initialState = {
_active: undefined,
_asyncValidating: false,
[globalErrorKey]: undefined,
_initialized: false,
_submitting: false,
_submitFailed: false
};
const behaviors = {
[ADD_ARRAY_VALUE](state, {path, index, value, fields}) {
const array = read(path, state);
const stateCopy = {...state};
const arrayCopy = array ? [...array] : [];
const newValue = value !== null && typeof value === 'object' ?
initializeState(value, fields || Object.keys(value)) : makeFieldValue({value});
if (index === undefined) {
arrayCopy.push(newValue);
} else {
arrayCopy.splice(index, 0, newValue);
}
return write(path, arrayCopy, stateCopy);
},
[AUTOFILL](state, {field, value}) {
return write(field, previous => {
const {asyncError, submitError, ...result} = {...previous, value, autofilled: true};
return makeFieldValue(result);
}, state);
},
[BLUR](state, {field, value, touch}) {
const {_active, ...stateCopy} = state;
if (_active && _active !== field) {
// remove _active from state
stateCopy._active = _active;
}
return write(field, previous => {
const result = {...previous};
if (value !== undefined) {
result.value = value;
}
if (touch) {
result.touched = true;
}
return makeFieldValue(result);
}, stateCopy);
},
[CHANGE](state, {field, value, touch}) {
return write(field, previous => {
const {asyncError, submitError, autofilled, ...result} = {...previous, value};
if (touch) {
result.touched = true;
}
return makeFieldValue(result);
}, state);
},
[DESTROY]() {
return undefined;
},
[FOCUS](state, {field}) {
const stateCopy = write(field, previous => makeFieldValue({...previous, visited: true}), state);
stateCopy._active = field;
return stateCopy;
},
[INITIALIZE](state, {data, fields, overwriteValues}) {
return {
...initializeState(data, fields, state, overwriteValues),
_asyncValidating: false,
_active: undefined,
[globalErrorKey]: undefined,
_initialized: true,
_submitting: false,
_submitFailed: false
};
},
[REMOVE_ARRAY_VALUE](state, {path, index}) {
const array = read(path, state);
const stateCopy = {...state};
const arrayCopy = array ? [...array] : [];
if (index === undefined) {
arrayCopy.pop();
} else Iif (isNaN(index)) {
delete arrayCopy[index];
} else {
arrayCopy.splice(index, 1);
}
return write(path, arrayCopy, stateCopy);
},
[RESET](state) {
return {
...resetState(state),
_active: undefined,
_asyncValidating: false,
[globalErrorKey]: undefined,
_initialized: state._initialized,
_submitting: false,
_submitFailed: false
};
},
[START_ASYNC_VALIDATION](state, {field}) {
return {
...state,
_asyncValidating: field || true
};
},
[START_SUBMIT](state) {
return {
...state,
_submitting: true
};
},
[STOP_ASYNC_VALIDATION](state, {errors}) {
return {
...setErrors(state, errors, 'asyncError'),
_asyncValidating: false,
[globalErrorKey]: errors && errors[globalErrorKey]
};
},
[STOP_SUBMIT](state, {errors}) {
return {
...setErrors(state, errors, 'submitError'),
[globalErrorKey]: errors && errors[globalErrorKey],
_submitting: false,
_submitFailed: !!(errors && Object.keys(errors).length)
};
},
[SUBMIT_FAILED](state) {
return {
...state,
_submitFailed: true
};
},
[SWAP_ARRAY_VALUES](state, {path, indexA, indexB}) {
const array = read(path, state);
const arrayLength = array.length;
if (indexA === indexB || isNaN(indexA) || isNaN(indexB) || indexA >= arrayLength || indexB >= arrayLength ) {
return state; // do nothing
}
const stateCopy = {...state};
const arrayCopy = [...array];
arrayCopy[indexA] = array[indexB];
arrayCopy[indexB] = array[indexA];
return write(path, arrayCopy, stateCopy);
},
[TOUCH](state, {fields}) {
return {
...state,
...fields.reduce((accumulator, field) =>
write(field, value => makeFieldValue({...value, touched: true}), accumulator), state)
};
},
[UNTOUCH](state, {fields}) {
return {
...state,
...fields.reduce((accumulator, field) =>
write(field, value => {
Eif (value) {
const {touched, ...rest} = value;
return makeFieldValue(rest);
}
return makeFieldValue(value);
}, accumulator), state)
};
}
};
const reducer = (state = initialState, action = {}) => {
const behavior = behaviors[action.type];
return behavior ? behavior(state, action) : state;
};
function formReducer(state = {}, action = {}) {
const {form, key, ...rest} = action; // eslint-disable-line no-redeclare
if (!form) {
return state;
}
if (key) {
if (action.type === DESTROY) {
return {
...state,
[form]: state[form] && Object.keys(state[form]).reduce((accumulator, stateKey) =>
stateKey === key ? accumulator : {
...accumulator,
[stateKey]: state[form][stateKey]
}, {})
};
}
return {
...state,
[form]: {
...state[form],
[key]: reducer((state[form] || {})[key], rest)
}
};
}
if (action.type === DESTROY) {
return Object.keys(state).reduce((accumulator, formName) =>
formName === form ? accumulator : {
...accumulator,
[formName]: state[formName]
}, {});
}
return {
...state,
[form]: reducer(state[form], rest)
};
}
/**
* Adds additional functionality to the reducer
*/
function decorate(target) {
target.plugin = function plugin(reducers) { // use 'function' keyword to enable 'this'
return decorate((state = {}, action = {}) => {
const result = this(state, action);
return {
...result,
...mapValues(reducers, (pluginReducer, key) => pluginReducer(result[key] || initialState, action))
};
});
};
target.normalize = function normalize(normalizers) { // use 'function' keyword to enable 'this'
return decorate((state = {}, action = {}) => {
const result = this(state, action);
return {
...result,
...mapValues(normalizers, (formNormalizers, form) => {
const runNormalize = (previous, currentResult) => {
const previousValues = getValuesFromState({
...initialState, ...previous
});
const formResult = {
...initialState,
...currentResult
};
const values = getValuesFromState(formResult);
return normalizeFields(formNormalizers, formResult, previous, values, previousValues);
};
if (action.key) {
return {
...result[form], [action.key]: runNormalize(state[form][action.key], result[form][action.key])
};
}
return runNormalize(state[form], result[form]);
})
};
});
};
return target;
}
export default decorate(formReducer);
|