redux-freeform 5.4.0-beta.0 → 5.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/redux-freeform.cjs.js +373 -472
- package/dist/redux-freeform.esm.js +373 -472
- package/dist/redux-freeform.umd.js +375 -459
- package/package.json +4 -5
|
@@ -2,68 +2,10 @@
|
|
|
2
2
|
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
require('core-js/modules/es.parse-int.js');
|
|
10
|
-
require('core-js/modules/es.array.slice.js');
|
|
11
|
-
require('core-js/modules/es.regexp.constructor.js');
|
|
12
|
-
require('core-js/modules/es.regexp.to-string.js');
|
|
13
|
-
require('core-js/modules/es.array.map.js');
|
|
14
|
-
require('core-js/modules/es.string.split.js');
|
|
15
|
-
require('core-js/modules/es.array.filter.js');
|
|
16
|
-
require('core-js/modules/es.array.concat.js');
|
|
17
|
-
require('core-js/modules/es.object.keys.js');
|
|
18
|
-
require('core-js/modules/es.object.entries.js');
|
|
19
|
-
|
|
20
|
-
function _unsupportedIterableToArray(o, minLen) {
|
|
21
|
-
if (!o) return;
|
|
22
|
-
if (typeof o === "string") return _arrayLikeToArray(o, minLen);
|
|
23
|
-
var n = Object.prototype.toString.call(o).slice(8, -1);
|
|
24
|
-
if (n === "Object" && o.constructor) n = o.constructor.name;
|
|
25
|
-
if (n === "Map" || n === "Set") return Array.from(o);
|
|
26
|
-
if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
function _arrayLikeToArray(arr, len) {
|
|
30
|
-
if (len == null || len > arr.length) len = arr.length;
|
|
31
|
-
|
|
32
|
-
for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i];
|
|
33
|
-
|
|
34
|
-
return arr2;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
function _createForOfIteratorHelperLoose(o, allowArrayLike) {
|
|
38
|
-
var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"];
|
|
39
|
-
if (it) return (it = it.call(o)).next.bind(it);
|
|
40
|
-
|
|
41
|
-
if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") {
|
|
42
|
-
if (it) o = it;
|
|
43
|
-
var i = 0;
|
|
44
|
-
return function () {
|
|
45
|
-
if (i >= o.length) return {
|
|
46
|
-
done: true
|
|
47
|
-
};
|
|
48
|
-
return {
|
|
49
|
-
done: false,
|
|
50
|
-
value: o[i++]
|
|
51
|
-
};
|
|
52
|
-
};
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
var validatorToPredicate = function validatorToPredicate(validatorFn, emptyCase) {
|
|
59
|
-
return function (value) {
|
|
60
|
-
for (var _len = arguments.length, rest = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
|
|
61
|
-
rest[_key - 1] = arguments[_key];
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
return value === "" ? emptyCase : validatorFn.apply(void 0, [value].concat(rest));
|
|
65
|
-
};
|
|
66
|
-
};
|
|
5
|
+
const validatorToPredicate = (validatorFn, emptyCase) => (
|
|
6
|
+
value,
|
|
7
|
+
...rest
|
|
8
|
+
) => (value === "" ? emptyCase : validatorFn(value, ...rest));
|
|
67
9
|
|
|
68
10
|
var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};
|
|
69
11
|
|
|
@@ -99,19 +41,10 @@ var isSameOrAfter$1 = {exports: {}};
|
|
|
99
41
|
|
|
100
42
|
var isSameOrAfter = isSameOrAfter$1.exports;
|
|
101
43
|
|
|
102
|
-
|
|
103
|
-
var validator = function validator() {
|
|
104
|
-
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
|
|
105
|
-
args[_key] = arguments[_key];
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
return {
|
|
109
|
-
type: type,
|
|
110
|
-
args: args,
|
|
111
|
-
error: error
|
|
112
|
-
};
|
|
113
|
-
};
|
|
44
|
+
/* eslint-disable no-unused-vars */
|
|
114
45
|
|
|
46
|
+
const createValidator = (type, error) => {
|
|
47
|
+
let validator = (...args) => ({ type, args, error });
|
|
115
48
|
validator.error = error;
|
|
116
49
|
return validator;
|
|
117
50
|
};
|
|
@@ -119,408 +52,428 @@ var createValidator = function createValidator(type, error) {
|
|
|
119
52
|
dayjs.extend(customParseFormat);
|
|
120
53
|
dayjs.extend(isSameOrBefore);
|
|
121
54
|
dayjs.extend(isSameOrAfter);
|
|
122
|
-
var validatorFns = {};
|
|
123
|
-
var REQUIRED = "validator/REQUIRED";
|
|
124
|
-
var REQUIRED_ERROR = "error/REQUIRED";
|
|
125
|
-
var required = createValidator(REQUIRED, REQUIRED_ERROR);
|
|
126
|
-
|
|
127
|
-
validatorFns[REQUIRED] = function (value, args, form) {
|
|
128
|
-
return value !== "";
|
|
129
|
-
};
|
|
130
|
-
|
|
131
|
-
var ONLY_INTEGERS = "validator/ONLY_INTEGERS";
|
|
132
|
-
var ONLY_INTEGERS_ERROR = "error/ONLY_INTEGERS";
|
|
133
|
-
var onlyIntegers = createValidator(ONLY_INTEGERS, ONLY_INTEGERS_ERROR);
|
|
134
|
-
|
|
135
|
-
validatorFns[ONLY_INTEGERS] = function (value, args, form) {
|
|
136
|
-
return /^(-?\d+)?$/.test(value);
|
|
137
|
-
};
|
|
138
|
-
|
|
139
|
-
var ONLY_NATURALS = "validator/ONLY_NATURALS";
|
|
140
|
-
var ONLY_NATURALS_ERROR = "error/ONLY_NATURALS";
|
|
141
|
-
var onlyNaturals = createValidator(ONLY_NATURALS, ONLY_NATURALS_ERROR);
|
|
142
|
-
|
|
143
|
-
validatorFns[ONLY_NATURALS] = function (value, args, form) {
|
|
144
|
-
return /^(\d+)?$/.test(value);
|
|
145
|
-
};
|
|
146
55
|
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
56
|
+
let validatorFns = {};
|
|
57
|
+
|
|
58
|
+
const REQUIRED = "validator/REQUIRED";
|
|
59
|
+
const REQUIRED_ERROR = "error/REQUIRED";
|
|
60
|
+
const required = createValidator(REQUIRED, REQUIRED_ERROR);
|
|
61
|
+
validatorFns[REQUIRED] = (value, args, form) => value !== "";
|
|
62
|
+
|
|
63
|
+
const ONLY_INTEGERS = "validator/ONLY_INTEGERS";
|
|
64
|
+
const ONLY_INTEGERS_ERROR = "error/ONLY_INTEGERS";
|
|
65
|
+
const onlyIntegers = createValidator(ONLY_INTEGERS, ONLY_INTEGERS_ERROR);
|
|
66
|
+
validatorFns[ONLY_INTEGERS] = (value, args, form) => /^(-?\d+)?$/.test(value);
|
|
67
|
+
|
|
68
|
+
const ONLY_NATURALS = "validator/ONLY_NATURALS";
|
|
69
|
+
const ONLY_NATURALS_ERROR = "error/ONLY_NATURALS";
|
|
70
|
+
const onlyNaturals = createValidator(ONLY_NATURALS, ONLY_NATURALS_ERROR);
|
|
71
|
+
validatorFns[ONLY_NATURALS] = (value, args, form) => /^(\d+)?$/.test(value);
|
|
72
|
+
|
|
73
|
+
const NUMBER_LESS_THAN = "validator/NUMBER_LESS_THAN";
|
|
74
|
+
const NUMBER_LESS_THAN_ERROR = "error/NUMBER_LESS_THAN";
|
|
75
|
+
const numberLessThan = createValidator(
|
|
76
|
+
NUMBER_LESS_THAN,
|
|
77
|
+
NUMBER_LESS_THAN_ERROR
|
|
78
|
+
);
|
|
79
|
+
validatorFns[NUMBER_LESS_THAN] = (value, args, form) => {
|
|
152
80
|
if (value === "") {
|
|
153
81
|
return true;
|
|
154
82
|
}
|
|
155
|
-
|
|
156
83
|
return Number(value) < args[0];
|
|
157
84
|
};
|
|
158
85
|
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
86
|
+
const NUMBER_LESS_THAN_OR_EQUAL_TO =
|
|
87
|
+
"validator/NUMBER_LESS_THAN_OR_EQUAL_TO";
|
|
88
|
+
const NUMBER_LESS_THAN_OR_EQUAL_TO_ERROR =
|
|
89
|
+
"error/NUMBER_LESS_THAN_OR_EQUAL_TO";
|
|
90
|
+
const numberLessThanOrEqualTo = createValidator(
|
|
91
|
+
NUMBER_LESS_THAN_OR_EQUAL_TO,
|
|
92
|
+
NUMBER_LESS_THAN_OR_EQUAL_TO_ERROR
|
|
93
|
+
);
|
|
94
|
+
validatorFns[NUMBER_LESS_THAN_OR_EQUAL_TO] = (value, args, form) => {
|
|
164
95
|
if (value === "") {
|
|
165
96
|
return true;
|
|
166
97
|
}
|
|
167
|
-
|
|
168
98
|
return Number(value) <= args[0];
|
|
169
99
|
};
|
|
170
100
|
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
101
|
+
const NUMBER_GREATER_THAN = "validator/NUMBER_GREATER_THAN";
|
|
102
|
+
const NUMBER_GREATER_THAN_ERROR = "error/NUMBER_GREATER_THAN";
|
|
103
|
+
const numberGreaterThan = createValidator(
|
|
104
|
+
NUMBER_GREATER_THAN,
|
|
105
|
+
NUMBER_GREATER_THAN_ERROR
|
|
106
|
+
);
|
|
107
|
+
validatorFns[NUMBER_GREATER_THAN] = (value, args, form) => {
|
|
176
108
|
if (value === "") {
|
|
177
109
|
return true;
|
|
178
110
|
}
|
|
179
|
-
|
|
180
111
|
return Number(value) > args[0];
|
|
181
112
|
};
|
|
182
113
|
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
114
|
+
const NUMBER_GREATER_THAN_OR_EQUAL_TO =
|
|
115
|
+
"validator/NUMBER_GREATER_THAN_OR_EQUAL_TO";
|
|
116
|
+
const NUMBER_GREATER_THAN_OR_EQUAL_TO_ERROR =
|
|
117
|
+
"error/NUMBER_GREATER_THAN_OR_EQUAL_TO";
|
|
118
|
+
const numberGreaterThanOrEqualTo = createValidator(
|
|
119
|
+
NUMBER_GREATER_THAN_OR_EQUAL_TO,
|
|
120
|
+
NUMBER_GREATER_THAN_OR_EQUAL_TO_ERROR
|
|
121
|
+
);
|
|
122
|
+
validatorFns[NUMBER_GREATER_THAN_OR_EQUAL_TO] = (value, args, form) => {
|
|
188
123
|
if (value === "") {
|
|
189
124
|
return true;
|
|
190
125
|
}
|
|
191
|
-
|
|
192
126
|
return Number(value) >= args[0];
|
|
193
127
|
};
|
|
194
128
|
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
var _dependentField$rawVa;
|
|
201
|
-
|
|
202
|
-
var dependentField = form[args[0]];
|
|
203
|
-
var dependentFieldValue = (_dependentField$rawVa = dependentField == null ? void 0 : dependentField.rawValue) != null ? _dependentField$rawVa : "";
|
|
204
|
-
|
|
129
|
+
const MATCHES_FIELD = "validator/MATCHES_FIELD";
|
|
130
|
+
const MATCHES_FIELD_ERROR = "error/MATCHES_FIELD";
|
|
131
|
+
const matchesField = createValidator(MATCHES_FIELD, MATCHES_FIELD_ERROR);
|
|
132
|
+
validatorFns[MATCHES_FIELD] = (value, args, form) => {
|
|
133
|
+
const dependentField = form[args[0]];
|
|
205
134
|
if (dependentField === undefined) {
|
|
206
|
-
throw new Error(
|
|
135
|
+
throw new Error(
|
|
136
|
+
`${args[0]} was passed to matchesField, but that field does not exist in the form`
|
|
137
|
+
);
|
|
207
138
|
}
|
|
208
|
-
|
|
139
|
+
const dependentFieldValue = dependentField.rawValue;
|
|
209
140
|
return value === dependentFieldValue;
|
|
210
141
|
};
|
|
211
142
|
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
143
|
+
const validateWhenErrorMessage = type =>
|
|
144
|
+
`${type} was passed to validateWhen, but that validator type does not exist.
|
|
145
|
+
Please check that you are only calling validator creator functions exported from
|
|
146
|
+
redux-freeform in your form config and that you didn't forget to
|
|
147
|
+
invoke the validator creator (you cannot pass the functions themselves to
|
|
148
|
+
createFormState). Also make sure you aren't passing validateWhen() to validateWhen
|
|
149
|
+
as the primary validator.`;
|
|
150
|
+
|
|
151
|
+
const VALIDATE_WHEN = "validator/VALIDATE_WHEN";
|
|
152
|
+
const VALIDATE_WHEN_ERROR = "error/VALIDATE_WHEN";
|
|
153
|
+
const validateWhen = (
|
|
154
|
+
dependentValidator,
|
|
155
|
+
primaryValidator,
|
|
156
|
+
optionalFieldName
|
|
157
|
+
) => ({
|
|
158
|
+
type: VALIDATE_WHEN,
|
|
159
|
+
args: [dependentValidator, primaryValidator, optionalFieldName],
|
|
160
|
+
error: dependentValidator.error
|
|
161
|
+
});
|
|
226
162
|
validateWhen.error = VALIDATE_WHEN_ERROR;
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
163
|
+
validatorFns[VALIDATE_WHEN] = (value, args, form) => {
|
|
164
|
+
const [dependentValidator, primaryValidator, optionalFieldName] = args;
|
|
165
|
+
const dependsOnOtherField = typeof optionalFieldName === "string";
|
|
166
|
+
|
|
167
|
+
if (
|
|
168
|
+
primaryValidator.type === undefined ||
|
|
169
|
+
typeof validatorFns[primaryValidator.type] !== "function"
|
|
170
|
+
) {
|
|
235
171
|
throw new Error(validateWhenErrorMessage(primaryValidator.type));
|
|
236
172
|
}
|
|
237
|
-
|
|
238
173
|
if (dependsOnOtherField && form[optionalFieldName] === undefined) {
|
|
239
|
-
throw new Error(
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
174
|
+
throw new Error(
|
|
175
|
+
`${args[2]} was passed to matchesField, but that field does not exist in the form`
|
|
176
|
+
);
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
const primaryPredicate = validatorToPredicate(
|
|
180
|
+
validatorFns[primaryValidator.type],
|
|
181
|
+
false
|
|
182
|
+
);
|
|
183
|
+
const primaryValue = dependsOnOtherField
|
|
184
|
+
? form[optionalFieldName].rawValue
|
|
185
|
+
: value;
|
|
186
|
+
const primaryPredicatePassed = primaryPredicate(
|
|
187
|
+
primaryValue,
|
|
188
|
+
primaryValidator.args,
|
|
189
|
+
form
|
|
190
|
+
);
|
|
191
|
+
|
|
192
|
+
return primaryPredicatePassed
|
|
193
|
+
? validatorFns[dependentValidator.type](
|
|
194
|
+
value,
|
|
195
|
+
dependentValidator.args,
|
|
196
|
+
form
|
|
197
|
+
)
|
|
198
|
+
: true;
|
|
260
199
|
};
|
|
261
200
|
|
|
201
|
+
const validateSumErrorMessage = type =>
|
|
202
|
+
`${type} was passed to validateSum, but that validator type does not exist.
|
|
203
|
+
Please check that you are only calling validator creator functions exported from
|
|
204
|
+
redux-freeform in your form config and that you didn't forget to
|
|
205
|
+
invoke the validator creator (you cannot pass the functions themselves to
|
|
206
|
+
createFormState).`;
|
|
207
|
+
const VALIDATE_SUM = "validator/VALIDATE_SUM";
|
|
208
|
+
const VALIDATE_SUM_ERROR = "error/VALIDATE_SUM";
|
|
209
|
+
const validateSum = (validator, fieldNamesArray) => ({
|
|
210
|
+
type: VALIDATE_SUM,
|
|
211
|
+
args: [validator, fieldNamesArray],
|
|
212
|
+
error: validator.error
|
|
213
|
+
});
|
|
262
214
|
validateSum.error = VALIDATE_SUM_ERROR;
|
|
215
|
+
validatorFns[VALIDATE_SUM] = (value, args, form) => {
|
|
216
|
+
const [validator, fieldNamesArray] = args;
|
|
263
217
|
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
if (validator.type === undefined || typeof validatorFns[validator.type] !== "function") {
|
|
218
|
+
if (
|
|
219
|
+
validator.type === undefined ||
|
|
220
|
+
typeof validatorFns[validator.type] !== "function"
|
|
221
|
+
) {
|
|
269
222
|
throw new Error(validateSumErrorMessage(validator.type));
|
|
270
223
|
}
|
|
271
224
|
|
|
272
|
-
for (
|
|
273
|
-
var fieldName = _step.value;
|
|
274
|
-
|
|
225
|
+
for (const fieldName of fieldNamesArray) {
|
|
275
226
|
if (form[fieldName] === undefined) {
|
|
276
|
-
throw new Error(
|
|
227
|
+
throw new Error(
|
|
228
|
+
`${fieldName} was passed to matchesField, but that field does not exist in the form`
|
|
229
|
+
);
|
|
277
230
|
}
|
|
278
231
|
}
|
|
279
232
|
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
233
|
+
const sum = fieldNamesArray.reduce(
|
|
234
|
+
(acc, curr) => acc + Number(form[curr].rawValue),
|
|
235
|
+
Number(value)
|
|
236
|
+
);
|
|
237
|
+
|
|
283
238
|
return validatorFns[validator.type](sum, validator.args, form);
|
|
284
239
|
};
|
|
285
240
|
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
validatorFns[HAS_LENGTH] = function (value, args, form) {
|
|
241
|
+
const HAS_LENGTH = "validator/HAS_LENGTH";
|
|
242
|
+
const HAS_LENGTH_ERROR = "error/HAS_LENGTH";
|
|
243
|
+
const hasLength = createValidator(HAS_LENGTH, HAS_LENGTH_ERROR);
|
|
244
|
+
validatorFns[HAS_LENGTH] = (value, args, form) => {
|
|
291
245
|
if (value === "") {
|
|
292
246
|
return true;
|
|
293
247
|
}
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
var max = args[1];
|
|
297
|
-
|
|
248
|
+
const min = args[0];
|
|
249
|
+
const max = args[1];
|
|
298
250
|
if (max == undefined || min == undefined) {
|
|
299
|
-
throw new Error(
|
|
251
|
+
throw new Error(
|
|
252
|
+
"Max and min need to be defined for hasLength, both or one of them is undefined"
|
|
253
|
+
);
|
|
300
254
|
}
|
|
301
|
-
|
|
302
255
|
if (max < min) {
|
|
303
|
-
throw new Error(
|
|
256
|
+
throw new Error(
|
|
257
|
+
"hasLength validator was passed a min greater than the max"
|
|
258
|
+
);
|
|
304
259
|
}
|
|
305
|
-
|
|
306
|
-
var valueLength = value.length;
|
|
260
|
+
const valueLength = value.length;
|
|
307
261
|
return max >= valueLength && valueLength >= min;
|
|
308
262
|
};
|
|
309
263
|
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
264
|
+
const DATE_BEFORE_TODAY = "validator/DATE_BEFORE_TODAY";
|
|
265
|
+
const DATE_BEFORE_TODAY_ERROR = "error/DATE_BEFORE_TODAY";
|
|
266
|
+
const dateBeforeToday = createValidator(
|
|
267
|
+
DATE_BEFORE_TODAY,
|
|
268
|
+
DATE_BEFORE_TODAY_ERROR
|
|
269
|
+
);
|
|
270
|
+
validatorFns[DATE_BEFORE_TODAY] = (value, args, form) => {
|
|
315
271
|
if (value === "") {
|
|
316
272
|
return true;
|
|
317
273
|
}
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
var inclusive = args[2] || false;
|
|
274
|
+
const dateFormat = args[0];
|
|
275
|
+
const unit = args[1];
|
|
276
|
+
const inclusive = args[2] || false;
|
|
322
277
|
|
|
323
278
|
if (dateFormat == undefined || unit == undefined) {
|
|
324
|
-
throw new Error(
|
|
279
|
+
throw new Error(
|
|
280
|
+
"Date format and unit need to be defined for dateBeforeToday, one or both are undefined"
|
|
281
|
+
);
|
|
325
282
|
}
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
var dateValue = dayjs(value, dateFormat);
|
|
283
|
+
const now = dayjs();
|
|
284
|
+
const dateValue = dayjs(value, dateFormat);
|
|
329
285
|
|
|
330
286
|
if (inclusive === true) {
|
|
331
287
|
return dateValue.isSameOrBefore(now, unit);
|
|
332
288
|
}
|
|
333
|
-
|
|
334
289
|
return dateValue.isBefore(now, unit);
|
|
335
290
|
};
|
|
336
291
|
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
292
|
+
const DATE_AFTER_TODAY = "validator/DATE_AFTER_TODAY";
|
|
293
|
+
const DATE_AFTER_TODAY_ERROR = "error/DATE_AFTER_TODAY";
|
|
294
|
+
const dateAfterToday = createValidator(
|
|
295
|
+
DATE_AFTER_TODAY,
|
|
296
|
+
DATE_AFTER_TODAY_ERROR
|
|
297
|
+
);
|
|
298
|
+
validatorFns[DATE_AFTER_TODAY] = (value, args, form) => {
|
|
342
299
|
if (value === "") {
|
|
343
300
|
return true;
|
|
344
301
|
}
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
var inclusive = args[2] || false;
|
|
302
|
+
const dateFormat = args[0];
|
|
303
|
+
const unit = args[1];
|
|
304
|
+
const inclusive = args[2] || false;
|
|
349
305
|
|
|
350
306
|
if (dateFormat == undefined || unit == undefined) {
|
|
351
|
-
throw new Error(
|
|
307
|
+
throw new Error(
|
|
308
|
+
"Date format and unit need to be defined for dateAfterToday, one or both are undefined"
|
|
309
|
+
);
|
|
352
310
|
}
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
var dateValue = dayjs(value, dateFormat);
|
|
311
|
+
const now = dayjs();
|
|
312
|
+
const dateValue = dayjs(value, dateFormat);
|
|
356
313
|
|
|
357
314
|
if (inclusive === true) {
|
|
358
315
|
return dateValue.isSameOrAfter(now, unit);
|
|
359
316
|
}
|
|
360
|
-
|
|
361
317
|
return dateValue.isAfter(now, unit);
|
|
362
318
|
};
|
|
363
319
|
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
320
|
+
const IS_VALID_MONTH = "validator/IS_VALID_MONTH";
|
|
321
|
+
const IS_VALID_MONTH_ERROR = "error/IS_VALID_MONTH";
|
|
322
|
+
const isValidMonth = createValidator(
|
|
323
|
+
IS_VALID_MONTH,
|
|
324
|
+
IS_VALID_MONTH_ERROR
|
|
325
|
+
);
|
|
326
|
+
validatorFns[IS_VALID_MONTH] = (value, args, form) => {
|
|
369
327
|
if (value === "") {
|
|
370
328
|
return true;
|
|
371
|
-
}
|
|
329
|
+
}
|
|
330
|
+
// Function takes one argument representing the character position
|
|
372
331
|
// In a date string to identify where the month is
|
|
373
332
|
// Eg "10/21/2021" - start position is 0
|
|
374
333
|
// Or "18/03/1990" - start position is 3
|
|
375
334
|
// Only works with two digit months (01, 02, 03, etc)
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
var monthStartPosition = parseInt(args[0]);
|
|
379
|
-
var monthEndPosition = monthStartPosition + 2;
|
|
380
|
-
|
|
335
|
+
const monthStartPosition = parseInt(args[0]);
|
|
336
|
+
const monthEndPosition = monthStartPosition + 2;
|
|
381
337
|
if (monthStartPosition === NaN) {
|
|
382
338
|
throw new Error("Month start position has to be a valid integer string");
|
|
383
339
|
}
|
|
384
|
-
|
|
385
|
-
var month = parseInt(value.slice(monthStartPosition, monthEndPosition));
|
|
386
|
-
|
|
340
|
+
const month = parseInt(value.slice(monthStartPosition, monthEndPosition));
|
|
387
341
|
if (month === NaN) {
|
|
388
342
|
return false;
|
|
389
343
|
}
|
|
390
|
-
|
|
391
344
|
return month >= 1 && month <= 12;
|
|
392
345
|
};
|
|
393
346
|
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
validatorFns[MATCHES_REGEX] = function (value, args, form) {
|
|
347
|
+
const MATCHES_REGEX = "validator/MATCHES_REGEX";
|
|
348
|
+
const MATCHES_REGEX_ERROR = "error/MATCHES_REGEX";
|
|
349
|
+
const matchesRegex = createValidator(MATCHES_REGEX, MATCHES_REGEX_ERROR);
|
|
350
|
+
validatorFns[MATCHES_REGEX] = (value, args, form) => {
|
|
399
351
|
if (value === "") {
|
|
400
352
|
return true;
|
|
401
353
|
}
|
|
402
|
-
|
|
403
354
|
return new RegExp(args[0]).test(value); // new RexExp never throws an error, no matter the input
|
|
404
|
-
};
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
var IS_ROUTING_NUMBER = "validator/IS_ROUTING_NUMBER";
|
|
408
|
-
var IS_ROUTING_NUMBER_ERROR = "error/IS_ROUTING_NUMBER";
|
|
409
|
-
var isRoutingNumber = createValidator(IS_ROUTING_NUMBER, IS_ROUTING_NUMBER_ERROR);
|
|
355
|
+
};
|
|
410
356
|
|
|
411
|
-
|
|
357
|
+
// based on http://www.brainjar.com/js/validation/
|
|
358
|
+
const IS_ROUTING_NUMBER = "validator/IS_ROUTING_NUMBER";
|
|
359
|
+
const IS_ROUTING_NUMBER_ERROR = "error/IS_ROUTING_NUMBER";
|
|
360
|
+
const isRoutingNumber = createValidator(
|
|
361
|
+
IS_ROUTING_NUMBER,
|
|
362
|
+
IS_ROUTING_NUMBER_ERROR
|
|
363
|
+
);
|
|
364
|
+
validatorFns[IS_ROUTING_NUMBER] = (value, args, form) => {
|
|
412
365
|
if (value === "") {
|
|
413
366
|
return true;
|
|
414
367
|
}
|
|
415
|
-
|
|
416
368
|
if (value.length != 9) {
|
|
417
369
|
return false;
|
|
418
370
|
}
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
}
|
|
433
|
-
}, 0);
|
|
371
|
+
const sum = value
|
|
372
|
+
.split("")
|
|
373
|
+
.map(ch => parseInt(ch))
|
|
374
|
+
.reduce((acc, cur, idx) => {
|
|
375
|
+
switch (idx % 3) {
|
|
376
|
+
case 0:
|
|
377
|
+
return acc + 3 * cur;
|
|
378
|
+
case 1:
|
|
379
|
+
return acc + 7 * cur;
|
|
380
|
+
case 2:
|
|
381
|
+
return acc + 1 * cur;
|
|
382
|
+
}
|
|
383
|
+
}, 0);
|
|
434
384
|
return sum != 0 && sum % 10 == 0;
|
|
435
385
|
};
|
|
436
386
|
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
validatorFns[HAS_NUMBER] = function (value, args, form) {
|
|
387
|
+
const HAS_NUMBER = "validator/HAS_NUMBER";
|
|
388
|
+
const HAS_NUMBER_ERROR = "error/HAS_NUMBER";
|
|
389
|
+
const hasNumber = createValidator(HAS_NUMBER, HAS_NUMBER_ERROR);
|
|
390
|
+
validatorFns[HAS_NUMBER] = (value, args, form) => {
|
|
442
391
|
if (value === "") {
|
|
443
392
|
return true;
|
|
444
393
|
}
|
|
445
|
-
|
|
446
394
|
return new RegExp(/[0-9]/).test(value);
|
|
447
395
|
};
|
|
448
396
|
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
397
|
+
const HAS_LOWERCASE_LETTER = "validator/HAS_LOWERCASE_LETTER";
|
|
398
|
+
const HAS_LOWERCASE_LETTER_ERROR = "error/HAS_LOWERCASE_LETTER";
|
|
399
|
+
const hasLowercaseLetter = createValidator(
|
|
400
|
+
HAS_LOWERCASE_LETTER,
|
|
401
|
+
HAS_LOWERCASE_LETTER_ERROR
|
|
402
|
+
);
|
|
403
|
+
validatorFns[HAS_LOWERCASE_LETTER] = (value, args, form) => {
|
|
454
404
|
if (value === "") {
|
|
455
405
|
return true;
|
|
456
406
|
}
|
|
457
|
-
|
|
458
407
|
return new RegExp(/[a-z]/).test(value);
|
|
459
408
|
};
|
|
460
409
|
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
410
|
+
const HAS_UPPERCASE_LETTER = "validator/HAS_UPPERCASE_LETTER";
|
|
411
|
+
const HAS_UPPERCASE_LETTER_ERROR = "error/HAS_UPPERCASE_LETTER";
|
|
412
|
+
const hasUppercaseLetter = createValidator(
|
|
413
|
+
HAS_UPPERCASE_LETTER,
|
|
414
|
+
HAS_UPPERCASE_LETTER_ERROR
|
|
415
|
+
);
|
|
416
|
+
validatorFns[HAS_UPPERCASE_LETTER] = (value, args, form) => {
|
|
466
417
|
if (value === "") {
|
|
467
418
|
return true;
|
|
468
419
|
}
|
|
469
|
-
|
|
470
420
|
return new RegExp(/[A-Z]/).test(value);
|
|
471
421
|
};
|
|
472
422
|
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
423
|
+
const HAS_SPECIAL_CHARACTER = "validator/HAS_SPECIAL_CHARACTER";
|
|
424
|
+
const HAS_SPECIAL_CHARACTER_ERROR = "error/HAS_SPECIAL_CHARACTER";
|
|
425
|
+
const hasSpecialCharacter = createValidator(
|
|
426
|
+
HAS_SPECIAL_CHARACTER,
|
|
427
|
+
HAS_SPECIAL_CHARACTER_ERROR
|
|
428
|
+
);
|
|
429
|
+
validatorFns[HAS_SPECIAL_CHARACTER] = (value, args, form) => {
|
|
478
430
|
if (value === "") {
|
|
479
431
|
return true;
|
|
480
432
|
}
|
|
481
|
-
|
|
482
433
|
return new RegExp(/[!@#$%^&*.?]/).test(value);
|
|
483
434
|
};
|
|
484
435
|
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
436
|
+
const IS_PROBABLY_EMAIL = "validator/IS_PROBABLY_EMAIL";
|
|
437
|
+
const IS_PROBABLY_EMAIL_ERROR = "error/IS_PROBABLY_EMAIL";
|
|
438
|
+
const isProbablyEmail = createValidator(
|
|
439
|
+
IS_PROBABLY_EMAIL,
|
|
440
|
+
IS_PROBABLY_EMAIL_ERROR
|
|
441
|
+
);
|
|
442
|
+
validatorFns[IS_PROBABLY_EMAIL] = (value, args, form) => {
|
|
490
443
|
if (value === "") {
|
|
491
444
|
return true;
|
|
492
445
|
}
|
|
493
|
-
|
|
494
446
|
return new RegExp(/^\S+@\S+\.\S+$/).test(value);
|
|
495
447
|
};
|
|
496
448
|
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
449
|
+
const runValidatorErrorMessage = type =>
|
|
450
|
+
`${type} was passed to runValidator, but that validator type does not exist.
|
|
451
|
+
Please check that you are only calling validator creator functions exported from
|
|
452
|
+
redux-freeform in your form config and that you didn't forget to
|
|
453
|
+
invoke the validator creator (you cannot pass the functions themselves to
|
|
454
|
+
createFormState)`;
|
|
502
455
|
|
|
456
|
+
const runValidator = (validator, value, form) => {
|
|
457
|
+
const validatorFn = validatorFns[validator.type];
|
|
503
458
|
if (validatorFn === undefined) {
|
|
504
459
|
throw new Error(runValidatorErrorMessage(validator.type));
|
|
505
460
|
}
|
|
506
|
-
|
|
507
461
|
return validatorFn(value, validator.args, form) ? null : validator.error;
|
|
508
462
|
};
|
|
509
463
|
|
|
510
|
-
|
|
511
|
-
return validators
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
return x !== null;
|
|
515
|
-
});
|
|
464
|
+
const _computeErrors = (fieldName, form, validators) => {
|
|
465
|
+
return validators
|
|
466
|
+
.map(v => runValidator(v, form[fieldName].rawValue, form))
|
|
467
|
+
.filter(x => x !== null);
|
|
516
468
|
};
|
|
517
469
|
|
|
518
|
-
|
|
519
|
-
|
|
470
|
+
const computeConstraints = (fieldName, form) => {
|
|
471
|
+
const constraints = form[fieldName].constraints;
|
|
520
472
|
return _computeErrors(fieldName, form, constraints);
|
|
521
473
|
};
|
|
522
|
-
|
|
523
|
-
|
|
474
|
+
|
|
475
|
+
const computeErrors = (fieldName, form) => {
|
|
476
|
+
const validators = form[fieldName].validators;
|
|
524
477
|
return _computeErrors(fieldName, form, validators);
|
|
525
478
|
};
|
|
526
479
|
|
|
@@ -740,7 +693,7 @@ function get(state, prop) {
|
|
|
740
693
|
return value;
|
|
741
694
|
}
|
|
742
695
|
|
|
743
|
-
function set(state, prop, value) {
|
|
696
|
+
function set$1(state, prop, value) {
|
|
744
697
|
assertUnrevoked(state);
|
|
745
698
|
state.assigned[prop] = true;
|
|
746
699
|
|
|
@@ -791,8 +744,8 @@ function proxyProperty(draft, prop, enumerable) {
|
|
|
791
744
|
return get(this[DRAFT_STATE], prop);
|
|
792
745
|
},
|
|
793
746
|
|
|
794
|
-
set: function set$1(value) {
|
|
795
|
-
set(this[DRAFT_STATE], prop, value);
|
|
747
|
+
set: function set$1$1(value) {
|
|
748
|
+
set$1(this[DRAFT_STATE], prop, value);
|
|
796
749
|
}
|
|
797
750
|
|
|
798
751
|
};
|
|
@@ -973,7 +926,7 @@ var objectTraps = {
|
|
|
973
926
|
return Reflect.ownKeys(source$1(target));
|
|
974
927
|
},
|
|
975
928
|
|
|
976
|
-
set: set$1,
|
|
929
|
+
set: set$1$1,
|
|
977
930
|
deleteProperty: deleteProperty,
|
|
978
931
|
getOwnPropertyDescriptor: getOwnPropertyDescriptor,
|
|
979
932
|
|
|
@@ -1051,7 +1004,7 @@ function get$1(state, prop) {
|
|
|
1051
1004
|
return drafts[prop] = createProxy$1(value, state);
|
|
1052
1005
|
}
|
|
1053
1006
|
|
|
1054
|
-
function set$1(state, prop, value) {
|
|
1007
|
+
function set$1$1(state, prop, value) {
|
|
1055
1008
|
if (!state.modified) {
|
|
1056
1009
|
var baseValue = peek$1(state.base, prop); // Optimize based on value's truthiness. Truthy values are guaranteed to
|
|
1057
1010
|
// never be undefined, so we can avoid the `in` operator. Lastly, truthy
|
|
@@ -1658,179 +1611,127 @@ immer.createDraft.bind(immer);
|
|
|
1658
1611
|
|
|
1659
1612
|
immer.finishDraft.bind(immer);
|
|
1660
1613
|
|
|
1661
|
-
|
|
1662
|
-
|
|
1663
|
-
|
|
1664
|
-
|
|
1665
|
-
for (var _i = 0, _formConfigKeys = formConfigKeys; _i < _formConfigKeys.length; _i++) {
|
|
1666
|
-
var formKey = _formConfigKeys[_i];
|
|
1614
|
+
const createInitialState = formConfig => {
|
|
1615
|
+
let initialForm = {};
|
|
1616
|
+
const formConfigKeys = Object.keys(formConfig);
|
|
1617
|
+
for (let formKey of formConfigKeys) {
|
|
1667
1618
|
initialForm[formKey] = {
|
|
1668
1619
|
dirty: false,
|
|
1669
1620
|
rawValue: formConfig[formKey].defaultValue || "",
|
|
1670
1621
|
validators: formConfig[formKey].validators || [],
|
|
1671
1622
|
constraints: formConfig[formKey].constraints || []
|
|
1672
1623
|
};
|
|
1673
|
-
}
|
|
1624
|
+
}
|
|
1625
|
+
// Because validators require the entire form we have to do a
|
|
1674
1626
|
// second pass to add errors once the initial form has been
|
|
1675
1627
|
// constructed
|
|
1676
|
-
|
|
1677
|
-
|
|
1678
|
-
|
|
1679
|
-
|
|
1680
|
-
var errors = computeErrors(_formKey, initialForm);
|
|
1681
|
-
initialForm[_formKey].errors = errors;
|
|
1682
|
-
initialForm[_formKey].hasErrors = errors.length > 0;
|
|
1628
|
+
for (let formKey of formConfigKeys) {
|
|
1629
|
+
let errors = computeErrors(formKey, initialForm);
|
|
1630
|
+
initialForm[formKey].errors = errors;
|
|
1631
|
+
initialForm[formKey].hasErrors = errors.length > 0;
|
|
1683
1632
|
}
|
|
1684
|
-
|
|
1685
1633
|
return initialForm;
|
|
1686
1634
|
};
|
|
1687
|
-
var SET = "field/SET";
|
|
1688
|
-
|
|
1689
|
-
var _set = function set(fieldName) {
|
|
1690
|
-
return function (value) {
|
|
1691
|
-
return {
|
|
1692
|
-
type: SET,
|
|
1693
|
-
payload: {
|
|
1694
|
-
fieldName: fieldName,
|
|
1695
|
-
value: value
|
|
1696
|
-
}
|
|
1697
|
-
};
|
|
1698
|
-
};
|
|
1699
|
-
};
|
|
1700
|
-
var CLEAR = "form/CLEAR";
|
|
1701
1635
|
|
|
1702
|
-
|
|
1703
|
-
|
|
1704
|
-
|
|
1705
|
-
}
|
|
1706
|
-
};
|
|
1707
|
-
var ADD_VALIDATOR = "field/ADD_VALIDATOR";
|
|
1708
|
-
|
|
1709
|
-
var _addValidator = function addValidator(fieldName) {
|
|
1710
|
-
return function (validator) {
|
|
1711
|
-
return {
|
|
1712
|
-
type: ADD_VALIDATOR,
|
|
1713
|
-
payload: {
|
|
1714
|
-
fieldName: fieldName,
|
|
1715
|
-
validator: validator
|
|
1716
|
-
}
|
|
1717
|
-
};
|
|
1718
|
-
};
|
|
1719
|
-
};
|
|
1720
|
-
var createFormReducer = function createFormReducer(formConfig) {
|
|
1721
|
-
return function (state, action) {
|
|
1722
|
-
if (state === void 0) {
|
|
1723
|
-
state = createInitialState(formConfig);
|
|
1724
|
-
}
|
|
1636
|
+
const SET = "field/SET";
|
|
1637
|
+
const set = fieldName => value => ({
|
|
1638
|
+
type: SET,
|
|
1639
|
+
payload: { fieldName, value }
|
|
1640
|
+
});
|
|
1725
1641
|
|
|
1726
|
-
|
|
1727
|
-
|
|
1728
|
-
var changedFieldName = action.payload.fieldName;
|
|
1729
|
-
var newRawValue = action.payload.value;
|
|
1730
|
-
return produce(state, function (draftState) {
|
|
1731
|
-
var originalValue = draftState[changedFieldName].rawValue;
|
|
1732
|
-
draftState[changedFieldName].rawValue = newRawValue;
|
|
1733
|
-
|
|
1734
|
-
if (computeConstraints(changedFieldName, draftState).length > 0) {
|
|
1735
|
-
// If the change violates constraints, revert the change
|
|
1736
|
-
draftState[changedFieldName].rawValue = originalValue;
|
|
1737
|
-
return draftState;
|
|
1738
|
-
}
|
|
1739
|
-
|
|
1740
|
-
var fields = Object.entries(draftState);
|
|
1741
|
-
|
|
1742
|
-
for (var _i3 = 0, _fields = fields; _i3 < _fields.length; _i3++) {
|
|
1743
|
-
var entry = _fields[_i3];
|
|
1744
|
-
var fieldName = entry[0];
|
|
1745
|
-
var field = entry[1];
|
|
1746
|
-
var errors = computeErrors(fieldName, draftState);
|
|
1747
|
-
var dirty = fieldName === changedFieldName ? true : field.dirty;
|
|
1748
|
-
draftState[fieldName].errors = errors;
|
|
1749
|
-
draftState[fieldName].dirty = dirty;
|
|
1750
|
-
draftState[fieldName].hasErrors = errors.length > 0;
|
|
1751
|
-
}
|
|
1752
|
-
});
|
|
1642
|
+
const CLEAR = "form/CLEAR";
|
|
1643
|
+
const clear = () => ({ type: CLEAR });
|
|
1753
1644
|
|
|
1754
|
-
|
|
1755
|
-
|
|
1756
|
-
|
|
1757
|
-
|
|
1758
|
-
|
|
1759
|
-
var newValidator = action.payload.validator;
|
|
1760
|
-
return produce(state, function (draftState) {
|
|
1761
|
-
draftState[fieldWithOverride].validators.push(newValidator);
|
|
1762
|
-
var fields = Object.entries(draftState);
|
|
1763
|
-
|
|
1764
|
-
for (var _i4 = 0, _fields2 = fields; _i4 < _fields2.length; _i4++) {
|
|
1765
|
-
var entry = _fields2[_i4];
|
|
1766
|
-
var fieldName = entry[0];
|
|
1767
|
-
var field = entry[1];
|
|
1768
|
-
var errors = computeErrors(fieldName, draftState);
|
|
1769
|
-
var dirty = field.dirty;
|
|
1770
|
-
draftState[fieldName].errors = errors;
|
|
1771
|
-
draftState[fieldName].dirty = dirty;
|
|
1772
|
-
draftState[fieldName].hasErrors = errors.length > 0;
|
|
1773
|
-
}
|
|
1774
|
-
});
|
|
1645
|
+
const ADD_VALIDATOR = "field/ADD_VALIDATOR";
|
|
1646
|
+
const addValidator = fieldName => validator => ({
|
|
1647
|
+
type: ADD_VALIDATOR,
|
|
1648
|
+
payload: { fieldName, validator }
|
|
1649
|
+
});
|
|
1775
1650
|
|
|
1776
|
-
|
|
1777
|
-
|
|
1778
|
-
|
|
1779
|
-
|
|
1651
|
+
const createFormReducer = formConfig => (
|
|
1652
|
+
state = createInitialState(formConfig),
|
|
1653
|
+
action
|
|
1654
|
+
) => {
|
|
1655
|
+
switch (action.type) {
|
|
1656
|
+
case SET:
|
|
1657
|
+
const changedFieldName = action.payload.fieldName;
|
|
1658
|
+
const newRawValue = action.payload.value;
|
|
1659
|
+
|
|
1660
|
+
return produce(state, draftState => {
|
|
1661
|
+
let originalValue = draftState[changedFieldName].rawValue;
|
|
1662
|
+
draftState[changedFieldName].rawValue = newRawValue;
|
|
1663
|
+
if (computeConstraints(changedFieldName, draftState).length > 0) {
|
|
1664
|
+
// If the change violates constraints, revert the change
|
|
1665
|
+
draftState[changedFieldName].rawValue = originalValue;
|
|
1666
|
+
return draftState;
|
|
1667
|
+
}
|
|
1668
|
+
|
|
1669
|
+
const fields = Object.entries(draftState);
|
|
1670
|
+
for (let entry of fields) {
|
|
1671
|
+
let fieldName = entry[0];
|
|
1672
|
+
let field = entry[1];
|
|
1673
|
+
let errors = computeErrors(fieldName, draftState);
|
|
1674
|
+
let dirty = fieldName === changedFieldName ? true : field.dirty;
|
|
1675
|
+
draftState[fieldName].errors = errors;
|
|
1676
|
+
draftState[fieldName].dirty = dirty;
|
|
1677
|
+
draftState[fieldName].hasErrors = errors.length > 0;
|
|
1678
|
+
}
|
|
1679
|
+
});
|
|
1680
|
+
case CLEAR:
|
|
1681
|
+
return createInitialState(formConfig);
|
|
1682
|
+
case ADD_VALIDATOR:
|
|
1683
|
+
const fieldWithOverride = action.payload.fieldName;
|
|
1684
|
+
const newValidator = action.payload.validator;
|
|
1685
|
+
|
|
1686
|
+
return produce(state, draftState => {
|
|
1687
|
+
draftState[fieldWithOverride].validators.push(newValidator);
|
|
1688
|
+
const fields = Object.entries(draftState);
|
|
1689
|
+
for (let entry of fields) {
|
|
1690
|
+
let fieldName = entry[0];
|
|
1691
|
+
let field = entry[1];
|
|
1692
|
+
let errors = computeErrors(fieldName, draftState);
|
|
1693
|
+
let dirty = field.dirty;
|
|
1694
|
+
draftState[fieldName].errors = errors;
|
|
1695
|
+
draftState[fieldName].dirty = dirty;
|
|
1696
|
+
draftState[fieldName].hasErrors = errors.length > 0;
|
|
1697
|
+
}
|
|
1698
|
+
});
|
|
1699
|
+
default:
|
|
1700
|
+
return state;
|
|
1701
|
+
}
|
|
1780
1702
|
};
|
|
1781
|
-
|
|
1703
|
+
|
|
1704
|
+
const createMapDispatchToProps = formConfig => {
|
|
1782
1705
|
// Do memo-ization
|
|
1783
|
-
|
|
1784
|
-
|
|
1785
|
-
return
|
|
1706
|
+
let cachedDispatch;
|
|
1707
|
+
let cacheValue;
|
|
1708
|
+
return dispatch => {
|
|
1786
1709
|
if (dispatch == cachedDispatch) {
|
|
1787
1710
|
return cacheValue;
|
|
1788
1711
|
}
|
|
1789
|
-
|
|
1790
|
-
var dispatchObj = {};
|
|
1712
|
+
let dispatchObj = {};
|
|
1791
1713
|
dispatchObj.fields = {};
|
|
1792
|
-
|
|
1793
|
-
|
|
1794
|
-
var _loop = function _loop() {
|
|
1795
|
-
var fieldName = _keys[_i5];
|
|
1714
|
+
const keys = Object.keys(formConfig);
|
|
1715
|
+
for (let fieldName of keys) {
|
|
1796
1716
|
dispatchObj.fields[fieldName] = {
|
|
1797
|
-
set:
|
|
1798
|
-
|
|
1799
|
-
},
|
|
1800
|
-
addValidator: function addValidator(validator) {
|
|
1801
|
-
return dispatch(_addValidator(fieldName)(validator));
|
|
1802
|
-
}
|
|
1717
|
+
set: value => dispatch(set(fieldName)(value)),
|
|
1718
|
+
addValidator: validator => dispatch(addValidator(fieldName)(validator))
|
|
1803
1719
|
};
|
|
1804
|
-
};
|
|
1805
|
-
|
|
1806
|
-
for (var _i5 = 0, _keys = keys; _i5 < _keys.length; _i5++) {
|
|
1807
|
-
_loop();
|
|
1808
1720
|
}
|
|
1809
|
-
|
|
1810
|
-
dispatchObj.form = {
|
|
1811
|
-
clear: function clear() {
|
|
1812
|
-
return dispatch(_clear());
|
|
1813
|
-
}
|
|
1814
|
-
};
|
|
1721
|
+
dispatchObj.form = { clear: () => dispatch(clear()) };
|
|
1815
1722
|
cachedDispatch = dispatch;
|
|
1816
|
-
cacheValue = {
|
|
1817
|
-
actions: dispatchObj
|
|
1818
|
-
};
|
|
1723
|
+
cacheValue = { actions: dispatchObj };
|
|
1819
1724
|
return cacheValue;
|
|
1820
1725
|
};
|
|
1821
1726
|
};
|
|
1822
|
-
|
|
1823
|
-
|
|
1824
|
-
|
|
1825
|
-
|
|
1826
|
-
|
|
1827
|
-
|
|
1828
|
-
|
|
1829
|
-
|
|
1830
|
-
mapDispatchToProps: createMapDispatchToProps(formConfig),
|
|
1831
|
-
mapStateToProps: mapStateToProps
|
|
1832
|
-
};
|
|
1833
|
-
};
|
|
1727
|
+
|
|
1728
|
+
const mapStateToProps = state => ({ fields: state });
|
|
1729
|
+
|
|
1730
|
+
const createFormState = formConfig => ({
|
|
1731
|
+
reducer: createFormReducer(formConfig),
|
|
1732
|
+
mapDispatchToProps: createMapDispatchToProps(formConfig),
|
|
1733
|
+
mapStateToProps: mapStateToProps
|
|
1734
|
+
});
|
|
1834
1735
|
|
|
1835
1736
|
exports.createFormState = createFormState;
|
|
1836
1737
|
exports.dateAfterToday = dateAfterToday;
|