tp-react-elements-dev 1.10.5 → 1.10.6
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/index.d.ts +2 -0
- package/dist/index.esm.js +681 -601
- package/dist/index.esm.js.map +1 -1
- package/dist/index.js +681 -600
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
package/dist/index.js
CHANGED
|
@@ -57,13 +57,14 @@ var isWeb = typeof window !== 'undefined' &&
|
|
|
57
57
|
function cloneObject(data) {
|
|
58
58
|
let copy;
|
|
59
59
|
const isArray = Array.isArray(data);
|
|
60
|
+
const isFileListInstance = typeof FileList !== 'undefined' ? data instanceof FileList : false;
|
|
60
61
|
if (data instanceof Date) {
|
|
61
62
|
copy = new Date(data);
|
|
62
63
|
}
|
|
63
64
|
else if (data instanceof Set) {
|
|
64
65
|
copy = new Set(data);
|
|
65
66
|
}
|
|
66
|
-
else if (!(isWeb && (data instanceof Blob ||
|
|
67
|
+
else if (!(isWeb && (data instanceof Blob || isFileListInstance)) &&
|
|
67
68
|
(isArray || isObject$1(data))) {
|
|
68
69
|
copy = isArray ? [] : {};
|
|
69
70
|
if (!isArray && !isPlainObject$1(data)) {
|
|
@@ -122,13 +123,12 @@ var set = (object, path, value) => {
|
|
|
122
123
|
? []
|
|
123
124
|
: {};
|
|
124
125
|
}
|
|
125
|
-
if (key === '__proto__') {
|
|
126
|
+
if (key === '__proto__' || key === 'constructor' || key === 'prototype') {
|
|
126
127
|
return;
|
|
127
128
|
}
|
|
128
129
|
object[key] = newValue;
|
|
129
130
|
object = object[key];
|
|
130
131
|
}
|
|
131
|
-
return object;
|
|
132
132
|
};
|
|
133
133
|
|
|
134
134
|
const EVENTS = {
|
|
@@ -205,43 +205,6 @@ var getProxyFormState = (formState, control, localProxyFormState, isRoot = true)
|
|
|
205
205
|
return result;
|
|
206
206
|
};
|
|
207
207
|
|
|
208
|
-
var isEmptyObject = (value) => isObject$1(value) && !Object.keys(value).length;
|
|
209
|
-
|
|
210
|
-
var shouldRenderFormState = (formStateData, _proxyFormState, updateFormState, isRoot) => {
|
|
211
|
-
updateFormState(formStateData);
|
|
212
|
-
const { name, ...formState } = formStateData;
|
|
213
|
-
return (isEmptyObject(formState) ||
|
|
214
|
-
Object.keys(formState).length >= Object.keys(_proxyFormState).length ||
|
|
215
|
-
Object.keys(formState).find((key) => _proxyFormState[key] ===
|
|
216
|
-
(!isRoot || VALIDATION_MODE.all)));
|
|
217
|
-
};
|
|
218
|
-
|
|
219
|
-
var convertToArrayPayload = (value) => (Array.isArray(value) ? value : [value]);
|
|
220
|
-
|
|
221
|
-
var shouldSubscribeByName = (name, signalName, exact) => !name ||
|
|
222
|
-
!signalName ||
|
|
223
|
-
name === signalName ||
|
|
224
|
-
convertToArrayPayload(name).some((currentName) => currentName &&
|
|
225
|
-
(exact
|
|
226
|
-
? currentName === signalName
|
|
227
|
-
: currentName.startsWith(signalName) ||
|
|
228
|
-
signalName.startsWith(currentName)));
|
|
229
|
-
|
|
230
|
-
function useSubscribe(props) {
|
|
231
|
-
const _props = React$1.useRef(props);
|
|
232
|
-
_props.current = props;
|
|
233
|
-
React$1.useEffect(() => {
|
|
234
|
-
const subscription = !props.disabled &&
|
|
235
|
-
_props.current.subject &&
|
|
236
|
-
_props.current.subject.subscribe({
|
|
237
|
-
next: _props.current.next,
|
|
238
|
-
});
|
|
239
|
-
return () => {
|
|
240
|
-
subscription && subscription.unsubscribe();
|
|
241
|
-
};
|
|
242
|
-
}, [props.disabled]);
|
|
243
|
-
}
|
|
244
|
-
|
|
245
208
|
/**
|
|
246
209
|
* This custom hook allows you to subscribe to each form state, and isolate the re-render at the custom hook level. It has its scope in terms of form state subscription, so it would not affect other useFormState and useForm. Using this hook can reduce the re-render impact on large and complex form application.
|
|
247
210
|
*
|
|
@@ -276,7 +239,6 @@ function useFormState(props) {
|
|
|
276
239
|
const methods = useFormContext();
|
|
277
240
|
const { control = methods.control, disabled, name, exact } = props || {};
|
|
278
241
|
const [formState, updateFormState] = React$1.useState(control._formState);
|
|
279
|
-
const _mounted = React$1.useRef(true);
|
|
280
242
|
const _localProxyFormState = React$1.useRef({
|
|
281
243
|
isDirty: false,
|
|
282
244
|
isLoading: false,
|
|
@@ -289,25 +251,22 @@ function useFormState(props) {
|
|
|
289
251
|
});
|
|
290
252
|
const _name = React$1.useRef(name);
|
|
291
253
|
_name.current = name;
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
254
|
+
React$1.useEffect(() => control._subscribe({
|
|
255
|
+
name: _name.current,
|
|
256
|
+
formState: _localProxyFormState.current,
|
|
257
|
+
exact,
|
|
258
|
+
callback: (formState) => {
|
|
259
|
+
!disabled &&
|
|
260
|
+
updateFormState({
|
|
261
|
+
...control._formState,
|
|
262
|
+
...formState,
|
|
263
|
+
});
|
|
264
|
+
},
|
|
265
|
+
}), [control, disabled, exact]);
|
|
303
266
|
React$1.useEffect(() => {
|
|
304
|
-
|
|
305
|
-
_localProxyFormState.current.isValid && control._updateValid(true);
|
|
306
|
-
return () => {
|
|
307
|
-
_mounted.current = false;
|
|
308
|
-
};
|
|
267
|
+
_localProxyFormState.current.isValid && control._setValid(true);
|
|
309
268
|
}, [control]);
|
|
310
|
-
return getProxyFormState(formState, control, _localProxyFormState.current, false);
|
|
269
|
+
return React$1.useMemo(() => getProxyFormState(formState, control, _localProxyFormState.current, false), [formState, control]);
|
|
311
270
|
}
|
|
312
271
|
|
|
313
272
|
var isString = (value) => typeof value === 'string';
|
|
@@ -344,16 +303,17 @@ function useWatch(props) {
|
|
|
344
303
|
const methods = useFormContext();
|
|
345
304
|
const { control = methods.control, name, defaultValue, disabled, exact, } = props || {};
|
|
346
305
|
const _name = React$1.useRef(name);
|
|
306
|
+
const _defaultValue = React$1.useRef(defaultValue);
|
|
347
307
|
_name.current = name;
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
if (shouldSubscribeByName(_name.current, formState.name, exact)) {
|
|
353
|
-
updateValue(cloneObject(generateWatchOutput(_name.current, control._names, formState.values || control._formValues, false, defaultValue)));
|
|
354
|
-
}
|
|
308
|
+
React$1.useEffect(() => control._subscribe({
|
|
309
|
+
name: _name.current,
|
|
310
|
+
formState: {
|
|
311
|
+
values: true,
|
|
355
312
|
},
|
|
356
|
-
|
|
313
|
+
exact,
|
|
314
|
+
callback: (formState) => !disabled &&
|
|
315
|
+
updateValue(generateWatchOutput(_name.current, control._names, formState.values || control._formValues, false, _defaultValue.current)),
|
|
316
|
+
}), [control, disabled, exact]);
|
|
357
317
|
const [value, updateValue] = React$1.useState(control._getWatch(name, defaultValue));
|
|
358
318
|
React$1.useEffect(() => control._removeUnmounted());
|
|
359
319
|
return value;
|
|
@@ -396,17 +356,82 @@ function useController(props) {
|
|
|
396
356
|
const formState = useFormState({
|
|
397
357
|
control,
|
|
398
358
|
name,
|
|
359
|
+
exact: true,
|
|
399
360
|
});
|
|
361
|
+
const _props = React$1.useRef(props);
|
|
400
362
|
const _registerProps = React$1.useRef(control.register(name, {
|
|
401
363
|
...props.rules,
|
|
402
364
|
value,
|
|
403
365
|
...(isBoolean(props.disabled) ? { disabled: props.disabled } : {}),
|
|
404
366
|
}));
|
|
367
|
+
const fieldState = React$1.useMemo(() => Object.defineProperties({}, {
|
|
368
|
+
invalid: {
|
|
369
|
+
enumerable: true,
|
|
370
|
+
get: () => !!get(formState.errors, name),
|
|
371
|
+
},
|
|
372
|
+
isDirty: {
|
|
373
|
+
enumerable: true,
|
|
374
|
+
get: () => !!get(formState.dirtyFields, name),
|
|
375
|
+
},
|
|
376
|
+
isTouched: {
|
|
377
|
+
enumerable: true,
|
|
378
|
+
get: () => !!get(formState.touchedFields, name),
|
|
379
|
+
},
|
|
380
|
+
isValidating: {
|
|
381
|
+
enumerable: true,
|
|
382
|
+
get: () => !!get(formState.validatingFields, name),
|
|
383
|
+
},
|
|
384
|
+
error: {
|
|
385
|
+
enumerable: true,
|
|
386
|
+
get: () => get(formState.errors, name),
|
|
387
|
+
},
|
|
388
|
+
}), [formState, name]);
|
|
389
|
+
const onChange = React$1.useCallback((event) => _registerProps.current.onChange({
|
|
390
|
+
target: {
|
|
391
|
+
value: getEventValue(event),
|
|
392
|
+
name: name,
|
|
393
|
+
},
|
|
394
|
+
type: EVENTS.CHANGE,
|
|
395
|
+
}), [name]);
|
|
396
|
+
const onBlur = React$1.useCallback(() => _registerProps.current.onBlur({
|
|
397
|
+
target: {
|
|
398
|
+
value: get(control._formValues, name),
|
|
399
|
+
name: name,
|
|
400
|
+
},
|
|
401
|
+
type: EVENTS.BLUR,
|
|
402
|
+
}), [name, control._formValues]);
|
|
403
|
+
const ref = React$1.useCallback((elm) => {
|
|
404
|
+
const field = get(control._fields, name);
|
|
405
|
+
if (field && elm) {
|
|
406
|
+
field._f.ref = {
|
|
407
|
+
focus: () => elm.focus(),
|
|
408
|
+
select: () => elm.select(),
|
|
409
|
+
setCustomValidity: (message) => elm.setCustomValidity(message),
|
|
410
|
+
reportValidity: () => elm.reportValidity(),
|
|
411
|
+
};
|
|
412
|
+
}
|
|
413
|
+
}, [control._fields, name]);
|
|
414
|
+
const field = React$1.useMemo(() => ({
|
|
415
|
+
name,
|
|
416
|
+
value,
|
|
417
|
+
...(isBoolean(disabled) || formState.disabled
|
|
418
|
+
? { disabled: formState.disabled || disabled }
|
|
419
|
+
: {}),
|
|
420
|
+
onChange,
|
|
421
|
+
onBlur,
|
|
422
|
+
ref,
|
|
423
|
+
}), [name, disabled, formState.disabled, onChange, onBlur, ref, value]);
|
|
405
424
|
React$1.useEffect(() => {
|
|
406
425
|
const _shouldUnregisterField = control._options.shouldUnregister || shouldUnregister;
|
|
426
|
+
control.register(name, {
|
|
427
|
+
..._props.current.rules,
|
|
428
|
+
...(isBoolean(_props.current.disabled)
|
|
429
|
+
? { disabled: _props.current.disabled }
|
|
430
|
+
: {}),
|
|
431
|
+
});
|
|
407
432
|
const updateMounted = (name, value) => {
|
|
408
433
|
const field = get(control._fields, name);
|
|
409
|
-
if (field) {
|
|
434
|
+
if (field && field._f) {
|
|
410
435
|
field._f.mount = value;
|
|
411
436
|
}
|
|
412
437
|
};
|
|
@@ -418,6 +443,7 @@ function useController(props) {
|
|
|
418
443
|
set(control._formValues, name, value);
|
|
419
444
|
}
|
|
420
445
|
}
|
|
446
|
+
!isArrayField && control.register(name);
|
|
421
447
|
return () => {
|
|
422
448
|
(isArrayField
|
|
423
449
|
? _shouldUnregisterField && !control._state.action
|
|
@@ -427,72 +453,16 @@ function useController(props) {
|
|
|
427
453
|
};
|
|
428
454
|
}, [name, control, isArrayField, shouldUnregister]);
|
|
429
455
|
React$1.useEffect(() => {
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
disabled,
|
|
433
|
-
fields: control._fields,
|
|
434
|
-
name,
|
|
435
|
-
value: get(control._fields, name)._f.value,
|
|
436
|
-
});
|
|
437
|
-
}
|
|
438
|
-
}, [disabled, name, control]);
|
|
439
|
-
return {
|
|
440
|
-
field: {
|
|
456
|
+
control._setDisabledField({
|
|
457
|
+
disabled,
|
|
441
458
|
name,
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
onChange: React$1.useCallback((event) => _registerProps.current.onChange({
|
|
447
|
-
target: {
|
|
448
|
-
value: getEventValue(event),
|
|
449
|
-
name: name,
|
|
450
|
-
},
|
|
451
|
-
type: EVENTS.CHANGE,
|
|
452
|
-
}), [name]),
|
|
453
|
-
onBlur: React$1.useCallback(() => _registerProps.current.onBlur({
|
|
454
|
-
target: {
|
|
455
|
-
value: get(control._formValues, name),
|
|
456
|
-
name: name,
|
|
457
|
-
},
|
|
458
|
-
type: EVENTS.BLUR,
|
|
459
|
-
}), [name, control]),
|
|
460
|
-
ref: (elm) => {
|
|
461
|
-
const field = get(control._fields, name);
|
|
462
|
-
if (field && elm) {
|
|
463
|
-
field._f.ref = {
|
|
464
|
-
focus: () => elm.focus(),
|
|
465
|
-
select: () => elm.select(),
|
|
466
|
-
setCustomValidity: (message) => elm.setCustomValidity(message),
|
|
467
|
-
reportValidity: () => elm.reportValidity(),
|
|
468
|
-
};
|
|
469
|
-
}
|
|
470
|
-
},
|
|
471
|
-
},
|
|
459
|
+
});
|
|
460
|
+
}, [disabled, name, control]);
|
|
461
|
+
return React$1.useMemo(() => ({
|
|
462
|
+
field,
|
|
472
463
|
formState,
|
|
473
|
-
fieldState
|
|
474
|
-
|
|
475
|
-
enumerable: true,
|
|
476
|
-
get: () => !!get(formState.errors, name),
|
|
477
|
-
},
|
|
478
|
-
isDirty: {
|
|
479
|
-
enumerable: true,
|
|
480
|
-
get: () => !!get(formState.dirtyFields, name),
|
|
481
|
-
},
|
|
482
|
-
isTouched: {
|
|
483
|
-
enumerable: true,
|
|
484
|
-
get: () => !!get(formState.touchedFields, name),
|
|
485
|
-
},
|
|
486
|
-
isValidating: {
|
|
487
|
-
enumerable: true,
|
|
488
|
-
get: () => !!get(formState.validatingFields, name),
|
|
489
|
-
},
|
|
490
|
-
error: {
|
|
491
|
-
enumerable: true,
|
|
492
|
-
get: () => get(formState.errors, name),
|
|
493
|
-
},
|
|
494
|
-
}),
|
|
495
|
-
};
|
|
464
|
+
fieldState,
|
|
465
|
+
}), [field, formState, fieldState]);
|
|
496
466
|
}
|
|
497
467
|
|
|
498
468
|
/**
|
|
@@ -549,340 +519,7 @@ var appendErrors = (name, validateAllFieldCriteria, errors, type, message) => va
|
|
|
549
519
|
}
|
|
550
520
|
: {};
|
|
551
521
|
|
|
552
|
-
var
|
|
553
|
-
isOnSubmit: !mode || mode === VALIDATION_MODE.onSubmit,
|
|
554
|
-
isOnBlur: mode === VALIDATION_MODE.onBlur,
|
|
555
|
-
isOnChange: mode === VALIDATION_MODE.onChange,
|
|
556
|
-
isOnAll: mode === VALIDATION_MODE.all,
|
|
557
|
-
isOnTouch: mode === VALIDATION_MODE.onTouched,
|
|
558
|
-
});
|
|
559
|
-
|
|
560
|
-
var isWatched = (name, _names, isBlurEvent) => !isBlurEvent &&
|
|
561
|
-
(_names.watchAll ||
|
|
562
|
-
_names.watch.has(name) ||
|
|
563
|
-
[..._names.watch].some((watchName) => name.startsWith(watchName) &&
|
|
564
|
-
/^\.\w+/.test(name.slice(watchName.length))));
|
|
565
|
-
|
|
566
|
-
const iterateFieldsByAction = (fields, action, fieldsNames, abortEarly) => {
|
|
567
|
-
for (const key of fieldsNames || Object.keys(fields)) {
|
|
568
|
-
const field = get(fields, key);
|
|
569
|
-
if (field) {
|
|
570
|
-
const { _f, ...currentField } = field;
|
|
571
|
-
if (_f) {
|
|
572
|
-
if (_f.refs && _f.refs[0] && action(_f.refs[0], key) && !abortEarly) {
|
|
573
|
-
break;
|
|
574
|
-
}
|
|
575
|
-
else if (_f.ref && action(_f.ref, _f.name) && !abortEarly) {
|
|
576
|
-
break;
|
|
577
|
-
}
|
|
578
|
-
else {
|
|
579
|
-
iterateFieldsByAction(currentField, action);
|
|
580
|
-
}
|
|
581
|
-
}
|
|
582
|
-
else if (isObject$1(currentField)) {
|
|
583
|
-
iterateFieldsByAction(currentField, action);
|
|
584
|
-
}
|
|
585
|
-
}
|
|
586
|
-
}
|
|
587
|
-
};
|
|
588
|
-
|
|
589
|
-
var updateFieldArrayRootError = (errors, error, name) => {
|
|
590
|
-
const fieldArrayErrors = compact(get(errors, name));
|
|
591
|
-
set(fieldArrayErrors, 'root', error[name]);
|
|
592
|
-
set(errors, name, fieldArrayErrors);
|
|
593
|
-
return errors;
|
|
594
|
-
};
|
|
595
|
-
|
|
596
|
-
var isFileInput = (element) => element.type === 'file';
|
|
597
|
-
|
|
598
|
-
var isFunction = (value) => typeof value === 'function';
|
|
599
|
-
|
|
600
|
-
var isHTMLElement$2 = (value) => {
|
|
601
|
-
if (!isWeb) {
|
|
602
|
-
return false;
|
|
603
|
-
}
|
|
604
|
-
const owner = value ? value.ownerDocument : 0;
|
|
605
|
-
return (value instanceof
|
|
606
|
-
(owner && owner.defaultView ? owner.defaultView.HTMLElement : HTMLElement));
|
|
607
|
-
};
|
|
608
|
-
|
|
609
|
-
var isMessage = (value) => isString(value);
|
|
610
|
-
|
|
611
|
-
var isRadioInput = (element) => element.type === 'radio';
|
|
612
|
-
|
|
613
|
-
var isRegex = (value) => value instanceof RegExp;
|
|
614
|
-
|
|
615
|
-
const defaultResult = {
|
|
616
|
-
value: false,
|
|
617
|
-
isValid: false,
|
|
618
|
-
};
|
|
619
|
-
const validResult = { value: true, isValid: true };
|
|
620
|
-
var getCheckboxValue = (options) => {
|
|
621
|
-
if (Array.isArray(options)) {
|
|
622
|
-
if (options.length > 1) {
|
|
623
|
-
const values = options
|
|
624
|
-
.filter((option) => option && option.checked && !option.disabled)
|
|
625
|
-
.map((option) => option.value);
|
|
626
|
-
return { value: values, isValid: !!values.length };
|
|
627
|
-
}
|
|
628
|
-
return options[0].checked && !options[0].disabled
|
|
629
|
-
? // @ts-expect-error expected to work in the browser
|
|
630
|
-
options[0].attributes && !isUndefined(options[0].attributes.value)
|
|
631
|
-
? isUndefined(options[0].value) || options[0].value === ''
|
|
632
|
-
? validResult
|
|
633
|
-
: { value: options[0].value, isValid: true }
|
|
634
|
-
: validResult
|
|
635
|
-
: defaultResult;
|
|
636
|
-
}
|
|
637
|
-
return defaultResult;
|
|
638
|
-
};
|
|
639
|
-
|
|
640
|
-
const defaultReturn = {
|
|
641
|
-
isValid: false,
|
|
642
|
-
value: null,
|
|
643
|
-
};
|
|
644
|
-
var getRadioValue = (options) => Array.isArray(options)
|
|
645
|
-
? options.reduce((previous, option) => option && option.checked && !option.disabled
|
|
646
|
-
? {
|
|
647
|
-
isValid: true,
|
|
648
|
-
value: option.value,
|
|
649
|
-
}
|
|
650
|
-
: previous, defaultReturn)
|
|
651
|
-
: defaultReturn;
|
|
652
|
-
|
|
653
|
-
function getValidateError(result, ref, type = 'validate') {
|
|
654
|
-
if (isMessage(result) ||
|
|
655
|
-
(Array.isArray(result) && result.every(isMessage)) ||
|
|
656
|
-
(isBoolean(result) && !result)) {
|
|
657
|
-
return {
|
|
658
|
-
type,
|
|
659
|
-
message: isMessage(result) ? result : '',
|
|
660
|
-
ref,
|
|
661
|
-
};
|
|
662
|
-
}
|
|
663
|
-
}
|
|
664
|
-
|
|
665
|
-
var getValueAndMessage = (validationData) => isObject$1(validationData) && !isRegex(validationData)
|
|
666
|
-
? validationData
|
|
667
|
-
: {
|
|
668
|
-
value: validationData,
|
|
669
|
-
message: '',
|
|
670
|
-
};
|
|
671
|
-
|
|
672
|
-
var validateField = async (field, formValues, validateAllFieldCriteria, shouldUseNativeValidation, isFieldArray) => {
|
|
673
|
-
const { ref, refs, required, maxLength, minLength, min, max, pattern, validate, name, valueAsNumber, mount, disabled, } = field._f;
|
|
674
|
-
const inputValue = get(formValues, name);
|
|
675
|
-
if (!mount || disabled) {
|
|
676
|
-
return {};
|
|
677
|
-
}
|
|
678
|
-
const inputRef = refs ? refs[0] : ref;
|
|
679
|
-
const setCustomValidity = (message) => {
|
|
680
|
-
if (shouldUseNativeValidation && inputRef.reportValidity) {
|
|
681
|
-
inputRef.setCustomValidity(isBoolean(message) ? '' : message || '');
|
|
682
|
-
inputRef.reportValidity();
|
|
683
|
-
}
|
|
684
|
-
};
|
|
685
|
-
const error = {};
|
|
686
|
-
const isRadio = isRadioInput(ref);
|
|
687
|
-
const isCheckBox = isCheckBoxInput(ref);
|
|
688
|
-
const isRadioOrCheckbox = isRadio || isCheckBox;
|
|
689
|
-
const isEmpty = ((valueAsNumber || isFileInput(ref)) &&
|
|
690
|
-
isUndefined(ref.value) &&
|
|
691
|
-
isUndefined(inputValue)) ||
|
|
692
|
-
(isHTMLElement$2(ref) && ref.value === '') ||
|
|
693
|
-
inputValue === '' ||
|
|
694
|
-
(Array.isArray(inputValue) && !inputValue.length);
|
|
695
|
-
const appendErrorsCurry = appendErrors.bind(null, name, validateAllFieldCriteria, error);
|
|
696
|
-
const getMinMaxMessage = (exceedMax, maxLengthMessage, minLengthMessage, maxType = INPUT_VALIDATION_RULES.maxLength, minType = INPUT_VALIDATION_RULES.minLength) => {
|
|
697
|
-
const message = exceedMax ? maxLengthMessage : minLengthMessage;
|
|
698
|
-
error[name] = {
|
|
699
|
-
type: exceedMax ? maxType : minType,
|
|
700
|
-
message,
|
|
701
|
-
ref,
|
|
702
|
-
...appendErrorsCurry(exceedMax ? maxType : minType, message),
|
|
703
|
-
};
|
|
704
|
-
};
|
|
705
|
-
if (isFieldArray
|
|
706
|
-
? !Array.isArray(inputValue) || !inputValue.length
|
|
707
|
-
: required &&
|
|
708
|
-
((!isRadioOrCheckbox && (isEmpty || isNullOrUndefined(inputValue))) ||
|
|
709
|
-
(isBoolean(inputValue) && !inputValue) ||
|
|
710
|
-
(isCheckBox && !getCheckboxValue(refs).isValid) ||
|
|
711
|
-
(isRadio && !getRadioValue(refs).isValid))) {
|
|
712
|
-
const { value, message } = isMessage(required)
|
|
713
|
-
? { value: !!required, message: required }
|
|
714
|
-
: getValueAndMessage(required);
|
|
715
|
-
if (value) {
|
|
716
|
-
error[name] = {
|
|
717
|
-
type: INPUT_VALIDATION_RULES.required,
|
|
718
|
-
message,
|
|
719
|
-
ref: inputRef,
|
|
720
|
-
...appendErrorsCurry(INPUT_VALIDATION_RULES.required, message),
|
|
721
|
-
};
|
|
722
|
-
if (!validateAllFieldCriteria) {
|
|
723
|
-
setCustomValidity(message);
|
|
724
|
-
return error;
|
|
725
|
-
}
|
|
726
|
-
}
|
|
727
|
-
}
|
|
728
|
-
if (!isEmpty && (!isNullOrUndefined(min) || !isNullOrUndefined(max))) {
|
|
729
|
-
let exceedMax;
|
|
730
|
-
let exceedMin;
|
|
731
|
-
const maxOutput = getValueAndMessage(max);
|
|
732
|
-
const minOutput = getValueAndMessage(min);
|
|
733
|
-
if (!isNullOrUndefined(inputValue) && !isNaN(inputValue)) {
|
|
734
|
-
const valueNumber = ref.valueAsNumber ||
|
|
735
|
-
(inputValue ? +inputValue : inputValue);
|
|
736
|
-
if (!isNullOrUndefined(maxOutput.value)) {
|
|
737
|
-
exceedMax = valueNumber > maxOutput.value;
|
|
738
|
-
}
|
|
739
|
-
if (!isNullOrUndefined(minOutput.value)) {
|
|
740
|
-
exceedMin = valueNumber < minOutput.value;
|
|
741
|
-
}
|
|
742
|
-
}
|
|
743
|
-
else {
|
|
744
|
-
const valueDate = ref.valueAsDate || new Date(inputValue);
|
|
745
|
-
const convertTimeToDate = (time) => new Date(new Date().toDateString() + ' ' + time);
|
|
746
|
-
const isTime = ref.type == 'time';
|
|
747
|
-
const isWeek = ref.type == 'week';
|
|
748
|
-
if (isString(maxOutput.value) && inputValue) {
|
|
749
|
-
exceedMax = isTime
|
|
750
|
-
? convertTimeToDate(inputValue) > convertTimeToDate(maxOutput.value)
|
|
751
|
-
: isWeek
|
|
752
|
-
? inputValue > maxOutput.value
|
|
753
|
-
: valueDate > new Date(maxOutput.value);
|
|
754
|
-
}
|
|
755
|
-
if (isString(minOutput.value) && inputValue) {
|
|
756
|
-
exceedMin = isTime
|
|
757
|
-
? convertTimeToDate(inputValue) < convertTimeToDate(minOutput.value)
|
|
758
|
-
: isWeek
|
|
759
|
-
? inputValue < minOutput.value
|
|
760
|
-
: valueDate < new Date(minOutput.value);
|
|
761
|
-
}
|
|
762
|
-
}
|
|
763
|
-
if (exceedMax || exceedMin) {
|
|
764
|
-
getMinMaxMessage(!!exceedMax, maxOutput.message, minOutput.message, INPUT_VALIDATION_RULES.max, INPUT_VALIDATION_RULES.min);
|
|
765
|
-
if (!validateAllFieldCriteria) {
|
|
766
|
-
setCustomValidity(error[name].message);
|
|
767
|
-
return error;
|
|
768
|
-
}
|
|
769
|
-
}
|
|
770
|
-
}
|
|
771
|
-
if ((maxLength || minLength) &&
|
|
772
|
-
!isEmpty &&
|
|
773
|
-
(isString(inputValue) || (isFieldArray && Array.isArray(inputValue)))) {
|
|
774
|
-
const maxLengthOutput = getValueAndMessage(maxLength);
|
|
775
|
-
const minLengthOutput = getValueAndMessage(minLength);
|
|
776
|
-
const exceedMax = !isNullOrUndefined(maxLengthOutput.value) &&
|
|
777
|
-
inputValue.length > +maxLengthOutput.value;
|
|
778
|
-
const exceedMin = !isNullOrUndefined(minLengthOutput.value) &&
|
|
779
|
-
inputValue.length < +minLengthOutput.value;
|
|
780
|
-
if (exceedMax || exceedMin) {
|
|
781
|
-
getMinMaxMessage(exceedMax, maxLengthOutput.message, minLengthOutput.message);
|
|
782
|
-
if (!validateAllFieldCriteria) {
|
|
783
|
-
setCustomValidity(error[name].message);
|
|
784
|
-
return error;
|
|
785
|
-
}
|
|
786
|
-
}
|
|
787
|
-
}
|
|
788
|
-
if (pattern && !isEmpty && isString(inputValue)) {
|
|
789
|
-
const { value: patternValue, message } = getValueAndMessage(pattern);
|
|
790
|
-
if (isRegex(patternValue) && !inputValue.match(patternValue)) {
|
|
791
|
-
error[name] = {
|
|
792
|
-
type: INPUT_VALIDATION_RULES.pattern,
|
|
793
|
-
message,
|
|
794
|
-
ref,
|
|
795
|
-
...appendErrorsCurry(INPUT_VALIDATION_RULES.pattern, message),
|
|
796
|
-
};
|
|
797
|
-
if (!validateAllFieldCriteria) {
|
|
798
|
-
setCustomValidity(message);
|
|
799
|
-
return error;
|
|
800
|
-
}
|
|
801
|
-
}
|
|
802
|
-
}
|
|
803
|
-
if (validate) {
|
|
804
|
-
if (isFunction(validate)) {
|
|
805
|
-
const result = await validate(inputValue, formValues);
|
|
806
|
-
const validateError = getValidateError(result, inputRef);
|
|
807
|
-
if (validateError) {
|
|
808
|
-
error[name] = {
|
|
809
|
-
...validateError,
|
|
810
|
-
...appendErrorsCurry(INPUT_VALIDATION_RULES.validate, validateError.message),
|
|
811
|
-
};
|
|
812
|
-
if (!validateAllFieldCriteria) {
|
|
813
|
-
setCustomValidity(validateError.message);
|
|
814
|
-
return error;
|
|
815
|
-
}
|
|
816
|
-
}
|
|
817
|
-
}
|
|
818
|
-
else if (isObject$1(validate)) {
|
|
819
|
-
let validationResult = {};
|
|
820
|
-
for (const key in validate) {
|
|
821
|
-
if (!isEmptyObject(validationResult) && !validateAllFieldCriteria) {
|
|
822
|
-
break;
|
|
823
|
-
}
|
|
824
|
-
const validateError = getValidateError(await validate[key](inputValue, formValues), inputRef, key);
|
|
825
|
-
if (validateError) {
|
|
826
|
-
validationResult = {
|
|
827
|
-
...validateError,
|
|
828
|
-
...appendErrorsCurry(key, validateError.message),
|
|
829
|
-
};
|
|
830
|
-
setCustomValidity(validateError.message);
|
|
831
|
-
if (validateAllFieldCriteria) {
|
|
832
|
-
error[name] = validationResult;
|
|
833
|
-
}
|
|
834
|
-
}
|
|
835
|
-
}
|
|
836
|
-
if (!isEmptyObject(validationResult)) {
|
|
837
|
-
error[name] = {
|
|
838
|
-
ref: inputRef,
|
|
839
|
-
...validationResult,
|
|
840
|
-
};
|
|
841
|
-
if (!validateAllFieldCriteria) {
|
|
842
|
-
return error;
|
|
843
|
-
}
|
|
844
|
-
}
|
|
845
|
-
}
|
|
846
|
-
}
|
|
847
|
-
setCustomValidity(true);
|
|
848
|
-
return error;
|
|
849
|
-
};
|
|
850
|
-
|
|
851
|
-
function baseGet(object, updatePath) {
|
|
852
|
-
const length = updatePath.slice(0, -1).length;
|
|
853
|
-
let index = 0;
|
|
854
|
-
while (index < length) {
|
|
855
|
-
object = isUndefined(object) ? index++ : object[updatePath[index++]];
|
|
856
|
-
}
|
|
857
|
-
return object;
|
|
858
|
-
}
|
|
859
|
-
function isEmptyArray(obj) {
|
|
860
|
-
for (const key in obj) {
|
|
861
|
-
if (obj.hasOwnProperty(key) && !isUndefined(obj[key])) {
|
|
862
|
-
return false;
|
|
863
|
-
}
|
|
864
|
-
}
|
|
865
|
-
return true;
|
|
866
|
-
}
|
|
867
|
-
function unset(object, path) {
|
|
868
|
-
const paths = Array.isArray(path)
|
|
869
|
-
? path
|
|
870
|
-
: isKey(path)
|
|
871
|
-
? [path]
|
|
872
|
-
: stringToPath(path);
|
|
873
|
-
const childObject = paths.length === 1 ? object : baseGet(object, paths);
|
|
874
|
-
const index = paths.length - 1;
|
|
875
|
-
const key = paths[index];
|
|
876
|
-
if (childObject) {
|
|
877
|
-
delete childObject[key];
|
|
878
|
-
}
|
|
879
|
-
if (index !== 0 &&
|
|
880
|
-
((isObject$1(childObject) && isEmptyObject(childObject)) ||
|
|
881
|
-
(Array.isArray(childObject) && isEmptyArray(childObject)))) {
|
|
882
|
-
unset(object, paths.slice(0, -1));
|
|
883
|
-
}
|
|
884
|
-
return object;
|
|
885
|
-
}
|
|
522
|
+
var convertToArrayPayload = (value) => (Array.isArray(value) ? value : [value]);
|
|
886
523
|
|
|
887
524
|
var createSubject = () => {
|
|
888
525
|
let _observers = [];
|
|
@@ -945,12 +582,65 @@ function deepEqual(object1, object2) {
|
|
|
945
582
|
return true;
|
|
946
583
|
}
|
|
947
584
|
|
|
585
|
+
var isEmptyObject = (value) => isObject$1(value) && !Object.keys(value).length;
|
|
586
|
+
|
|
587
|
+
var isFileInput = (element) => element.type === 'file';
|
|
588
|
+
|
|
589
|
+
var isFunction = (value) => typeof value === 'function';
|
|
590
|
+
|
|
591
|
+
var isHTMLElement$2 = (value) => {
|
|
592
|
+
if (!isWeb) {
|
|
593
|
+
return false;
|
|
594
|
+
}
|
|
595
|
+
const owner = value ? value.ownerDocument : 0;
|
|
596
|
+
return (value instanceof
|
|
597
|
+
(owner && owner.defaultView ? owner.defaultView.HTMLElement : HTMLElement));
|
|
598
|
+
};
|
|
599
|
+
|
|
948
600
|
var isMultipleSelect = (element) => element.type === `select-multiple`;
|
|
949
601
|
|
|
602
|
+
var isRadioInput = (element) => element.type === 'radio';
|
|
603
|
+
|
|
950
604
|
var isRadioOrCheckbox = (ref) => isRadioInput(ref) || isCheckBoxInput(ref);
|
|
951
605
|
|
|
952
606
|
var live = (ref) => isHTMLElement$2(ref) && ref.isConnected;
|
|
953
607
|
|
|
608
|
+
function baseGet(object, updatePath) {
|
|
609
|
+
const length = updatePath.slice(0, -1).length;
|
|
610
|
+
let index = 0;
|
|
611
|
+
while (index < length) {
|
|
612
|
+
object = isUndefined(object) ? index++ : object[updatePath[index++]];
|
|
613
|
+
}
|
|
614
|
+
return object;
|
|
615
|
+
}
|
|
616
|
+
function isEmptyArray(obj) {
|
|
617
|
+
for (const key in obj) {
|
|
618
|
+
if (obj.hasOwnProperty(key) && !isUndefined(obj[key])) {
|
|
619
|
+
return false;
|
|
620
|
+
}
|
|
621
|
+
}
|
|
622
|
+
return true;
|
|
623
|
+
}
|
|
624
|
+
function unset(object, path) {
|
|
625
|
+
const paths = Array.isArray(path)
|
|
626
|
+
? path
|
|
627
|
+
: isKey(path)
|
|
628
|
+
? [path]
|
|
629
|
+
: stringToPath(path);
|
|
630
|
+
const childObject = paths.length === 1 ? object : baseGet(object, paths);
|
|
631
|
+
const index = paths.length - 1;
|
|
632
|
+
const key = paths[index];
|
|
633
|
+
if (childObject) {
|
|
634
|
+
delete childObject[key];
|
|
635
|
+
}
|
|
636
|
+
if (index !== 0 &&
|
|
637
|
+
((isObject$1(childObject) && isEmptyObject(childObject)) ||
|
|
638
|
+
(Array.isArray(childObject) && isEmptyArray(childObject)))) {
|
|
639
|
+
unset(object, paths.slice(0, -1));
|
|
640
|
+
}
|
|
641
|
+
return object;
|
|
642
|
+
}
|
|
643
|
+
|
|
954
644
|
var objectHasFunction = (data) => {
|
|
955
645
|
for (const key in data) {
|
|
956
646
|
if (isFunction(data[key])) {
|
|
@@ -1001,6 +691,31 @@ function getDirtyFieldsFromDefaultValues(data, formValues, dirtyFieldsFromValues
|
|
|
1001
691
|
}
|
|
1002
692
|
var getDirtyFields = (defaultValues, formValues) => getDirtyFieldsFromDefaultValues(defaultValues, formValues, markFieldsDirty(formValues));
|
|
1003
693
|
|
|
694
|
+
const defaultResult = {
|
|
695
|
+
value: false,
|
|
696
|
+
isValid: false,
|
|
697
|
+
};
|
|
698
|
+
const validResult = { value: true, isValid: true };
|
|
699
|
+
var getCheckboxValue = (options) => {
|
|
700
|
+
if (Array.isArray(options)) {
|
|
701
|
+
if (options.length > 1) {
|
|
702
|
+
const values = options
|
|
703
|
+
.filter((option) => option && option.checked && !option.disabled)
|
|
704
|
+
.map((option) => option.value);
|
|
705
|
+
return { value: values, isValid: !!values.length };
|
|
706
|
+
}
|
|
707
|
+
return options[0].checked && !options[0].disabled
|
|
708
|
+
? // @ts-expect-error expected to work in the browser
|
|
709
|
+
options[0].attributes && !isUndefined(options[0].attributes.value)
|
|
710
|
+
? isUndefined(options[0].value) || options[0].value === ''
|
|
711
|
+
? validResult
|
|
712
|
+
: { value: options[0].value, isValid: true }
|
|
713
|
+
: validResult
|
|
714
|
+
: defaultResult;
|
|
715
|
+
}
|
|
716
|
+
return defaultResult;
|
|
717
|
+
};
|
|
718
|
+
|
|
1004
719
|
var getFieldValueAs = (value, { valueAsNumber, valueAsDate, setValueAs }) => isUndefined(value)
|
|
1005
720
|
? value
|
|
1006
721
|
: valueAsNumber
|
|
@@ -1015,11 +730,21 @@ var getFieldValueAs = (value, { valueAsNumber, valueAsDate, setValueAs }) => isU
|
|
|
1015
730
|
? setValueAs(value)
|
|
1016
731
|
: value;
|
|
1017
732
|
|
|
733
|
+
const defaultReturn = {
|
|
734
|
+
isValid: false,
|
|
735
|
+
value: null,
|
|
736
|
+
};
|
|
737
|
+
var getRadioValue = (options) => Array.isArray(options)
|
|
738
|
+
? options.reduce((previous, option) => option && option.checked && !option.disabled
|
|
739
|
+
? {
|
|
740
|
+
isValid: true,
|
|
741
|
+
value: option.value,
|
|
742
|
+
}
|
|
743
|
+
: previous, defaultReturn)
|
|
744
|
+
: defaultReturn;
|
|
745
|
+
|
|
1018
746
|
function getFieldValue(_f) {
|
|
1019
747
|
const ref = _f.ref;
|
|
1020
|
-
if (_f.refs ? _f.refs.every((ref) => ref.disabled) : ref.disabled) {
|
|
1021
|
-
return;
|
|
1022
|
-
}
|
|
1023
748
|
if (isFileInput(ref)) {
|
|
1024
749
|
return ref.files;
|
|
1025
750
|
}
|
|
@@ -1049,6 +774,8 @@ var getResolverOptions = (fieldsNames, _fields, criteriaMode, shouldUseNativeVal
|
|
|
1049
774
|
};
|
|
1050
775
|
};
|
|
1051
776
|
|
|
777
|
+
var isRegex = (value) => value instanceof RegExp;
|
|
778
|
+
|
|
1052
779
|
var getRuleValue = (rule) => isUndefined(rule)
|
|
1053
780
|
? rule
|
|
1054
781
|
: isRegex(rule)
|
|
@@ -1059,6 +786,22 @@ var getRuleValue = (rule) => isUndefined(rule)
|
|
|
1059
786
|
: rule.value
|
|
1060
787
|
: rule;
|
|
1061
788
|
|
|
789
|
+
var getValidationModes = (mode) => ({
|
|
790
|
+
isOnSubmit: !mode || mode === VALIDATION_MODE.onSubmit,
|
|
791
|
+
isOnBlur: mode === VALIDATION_MODE.onBlur,
|
|
792
|
+
isOnChange: mode === VALIDATION_MODE.onChange,
|
|
793
|
+
isOnAll: mode === VALIDATION_MODE.all,
|
|
794
|
+
isOnTouch: mode === VALIDATION_MODE.onTouched,
|
|
795
|
+
});
|
|
796
|
+
|
|
797
|
+
const ASYNC_FUNCTION = 'AsyncFunction';
|
|
798
|
+
var hasPromiseValidation = (fieldReference) => !!fieldReference &&
|
|
799
|
+
!!fieldReference.validate &&
|
|
800
|
+
!!((isFunction(fieldReference.validate) &&
|
|
801
|
+
fieldReference.validate.constructor.name === ASYNC_FUNCTION) ||
|
|
802
|
+
(isObject$1(fieldReference.validate) &&
|
|
803
|
+
Object.values(fieldReference.validate).find((validateFunction) => validateFunction.constructor.name === ASYNC_FUNCTION)));
|
|
804
|
+
|
|
1062
805
|
var hasValidation = (options) => options.mount &&
|
|
1063
806
|
(options.required ||
|
|
1064
807
|
options.min ||
|
|
@@ -1068,6 +811,40 @@ var hasValidation = (options) => options.mount &&
|
|
|
1068
811
|
options.pattern ||
|
|
1069
812
|
options.validate);
|
|
1070
813
|
|
|
814
|
+
var isWatched = (name, _names, isBlurEvent) => !isBlurEvent &&
|
|
815
|
+
(_names.watchAll ||
|
|
816
|
+
_names.watch.has(name) ||
|
|
817
|
+
[..._names.watch].some((watchName) => name.startsWith(watchName) &&
|
|
818
|
+
/^\.\w+/.test(name.slice(watchName.length))));
|
|
819
|
+
|
|
820
|
+
const iterateFieldsByAction = (fields, action, fieldsNames, abortEarly) => {
|
|
821
|
+
for (const key of fieldsNames || Object.keys(fields)) {
|
|
822
|
+
const field = get(fields, key);
|
|
823
|
+
if (field) {
|
|
824
|
+
const { _f, ...currentField } = field;
|
|
825
|
+
if (_f) {
|
|
826
|
+
if (_f.refs && _f.refs[0] && action(_f.refs[0], key) && !abortEarly) {
|
|
827
|
+
return true;
|
|
828
|
+
}
|
|
829
|
+
else if (_f.ref && action(_f.ref, _f.name) && !abortEarly) {
|
|
830
|
+
return true;
|
|
831
|
+
}
|
|
832
|
+
else {
|
|
833
|
+
if (iterateFieldsByAction(currentField, action)) {
|
|
834
|
+
break;
|
|
835
|
+
}
|
|
836
|
+
}
|
|
837
|
+
}
|
|
838
|
+
else if (isObject$1(currentField)) {
|
|
839
|
+
if (iterateFieldsByAction(currentField, action)) {
|
|
840
|
+
break;
|
|
841
|
+
}
|
|
842
|
+
}
|
|
843
|
+
}
|
|
844
|
+
}
|
|
845
|
+
return;
|
|
846
|
+
};
|
|
847
|
+
|
|
1071
848
|
function schemaErrorLookup(errors, _fields, name) {
|
|
1072
849
|
const error = get(errors, name);
|
|
1073
850
|
if (error || isKey(name)) {
|
|
@@ -1097,6 +874,24 @@ function schemaErrorLookup(errors, _fields, name) {
|
|
|
1097
874
|
};
|
|
1098
875
|
}
|
|
1099
876
|
|
|
877
|
+
var shouldRenderFormState = (formStateData, _proxyFormState, updateFormState, isRoot) => {
|
|
878
|
+
updateFormState(formStateData);
|
|
879
|
+
const { name, ...formState } = formStateData;
|
|
880
|
+
return (isEmptyObject(formState) ||
|
|
881
|
+
Object.keys(formState).length >= Object.keys(_proxyFormState).length ||
|
|
882
|
+
Object.keys(formState).find((key) => _proxyFormState[key] ===
|
|
883
|
+
(!isRoot || VALIDATION_MODE.all)));
|
|
884
|
+
};
|
|
885
|
+
|
|
886
|
+
var shouldSubscribeByName = (name, signalName, exact) => !name ||
|
|
887
|
+
!signalName ||
|
|
888
|
+
name === signalName ||
|
|
889
|
+
convertToArrayPayload(name).some((currentName) => currentName &&
|
|
890
|
+
(exact
|
|
891
|
+
? currentName === signalName
|
|
892
|
+
: currentName.startsWith(signalName) ||
|
|
893
|
+
signalName.startsWith(currentName)));
|
|
894
|
+
|
|
1100
895
|
var skipValidation = (isBlurEvent, isTouched, isSubmitted, reValidateMode, mode) => {
|
|
1101
896
|
if (mode.isOnAll) {
|
|
1102
897
|
return false;
|
|
@@ -1115,6 +910,213 @@ var skipValidation = (isBlurEvent, isTouched, isSubmitted, reValidateMode, mode)
|
|
|
1115
910
|
|
|
1116
911
|
var unsetEmptyArray = (ref, name) => !compact(get(ref, name)).length && unset(ref, name);
|
|
1117
912
|
|
|
913
|
+
var updateFieldArrayRootError = (errors, error, name) => {
|
|
914
|
+
const fieldArrayErrors = convertToArrayPayload(get(errors, name));
|
|
915
|
+
set(fieldArrayErrors, 'root', error[name]);
|
|
916
|
+
set(errors, name, fieldArrayErrors);
|
|
917
|
+
return errors;
|
|
918
|
+
};
|
|
919
|
+
|
|
920
|
+
var isMessage = (value) => isString(value);
|
|
921
|
+
|
|
922
|
+
function getValidateError(result, ref, type = 'validate') {
|
|
923
|
+
if (isMessage(result) ||
|
|
924
|
+
(Array.isArray(result) && result.every(isMessage)) ||
|
|
925
|
+
(isBoolean(result) && !result)) {
|
|
926
|
+
return {
|
|
927
|
+
type,
|
|
928
|
+
message: isMessage(result) ? result : '',
|
|
929
|
+
ref,
|
|
930
|
+
};
|
|
931
|
+
}
|
|
932
|
+
}
|
|
933
|
+
|
|
934
|
+
var getValueAndMessage = (validationData) => isObject$1(validationData) && !isRegex(validationData)
|
|
935
|
+
? validationData
|
|
936
|
+
: {
|
|
937
|
+
value: validationData,
|
|
938
|
+
message: '',
|
|
939
|
+
};
|
|
940
|
+
|
|
941
|
+
var validateField = async (field, disabledFieldNames, formValues, validateAllFieldCriteria, shouldUseNativeValidation, isFieldArray) => {
|
|
942
|
+
const { ref, refs, required, maxLength, minLength, min, max, pattern, validate, name, valueAsNumber, mount, } = field._f;
|
|
943
|
+
const inputValue = get(formValues, name);
|
|
944
|
+
if (!mount || disabledFieldNames.has(name)) {
|
|
945
|
+
return {};
|
|
946
|
+
}
|
|
947
|
+
const inputRef = refs ? refs[0] : ref;
|
|
948
|
+
const setCustomValidity = (message) => {
|
|
949
|
+
if (shouldUseNativeValidation && inputRef.reportValidity) {
|
|
950
|
+
inputRef.setCustomValidity(isBoolean(message) ? '' : message || '');
|
|
951
|
+
inputRef.reportValidity();
|
|
952
|
+
}
|
|
953
|
+
};
|
|
954
|
+
const error = {};
|
|
955
|
+
const isRadio = isRadioInput(ref);
|
|
956
|
+
const isCheckBox = isCheckBoxInput(ref);
|
|
957
|
+
const isRadioOrCheckbox = isRadio || isCheckBox;
|
|
958
|
+
const isEmpty = ((valueAsNumber || isFileInput(ref)) &&
|
|
959
|
+
isUndefined(ref.value) &&
|
|
960
|
+
isUndefined(inputValue)) ||
|
|
961
|
+
(isHTMLElement$2(ref) && ref.value === '') ||
|
|
962
|
+
inputValue === '' ||
|
|
963
|
+
(Array.isArray(inputValue) && !inputValue.length);
|
|
964
|
+
const appendErrorsCurry = appendErrors.bind(null, name, validateAllFieldCriteria, error);
|
|
965
|
+
const getMinMaxMessage = (exceedMax, maxLengthMessage, minLengthMessage, maxType = INPUT_VALIDATION_RULES.maxLength, minType = INPUT_VALIDATION_RULES.minLength) => {
|
|
966
|
+
const message = exceedMax ? maxLengthMessage : minLengthMessage;
|
|
967
|
+
error[name] = {
|
|
968
|
+
type: exceedMax ? maxType : minType,
|
|
969
|
+
message,
|
|
970
|
+
ref,
|
|
971
|
+
...appendErrorsCurry(exceedMax ? maxType : minType, message),
|
|
972
|
+
};
|
|
973
|
+
};
|
|
974
|
+
if (isFieldArray
|
|
975
|
+
? !Array.isArray(inputValue) || !inputValue.length
|
|
976
|
+
: required &&
|
|
977
|
+
((!isRadioOrCheckbox && (isEmpty || isNullOrUndefined(inputValue))) ||
|
|
978
|
+
(isBoolean(inputValue) && !inputValue) ||
|
|
979
|
+
(isCheckBox && !getCheckboxValue(refs).isValid) ||
|
|
980
|
+
(isRadio && !getRadioValue(refs).isValid))) {
|
|
981
|
+
const { value, message } = isMessage(required)
|
|
982
|
+
? { value: !!required, message: required }
|
|
983
|
+
: getValueAndMessage(required);
|
|
984
|
+
if (value) {
|
|
985
|
+
error[name] = {
|
|
986
|
+
type: INPUT_VALIDATION_RULES.required,
|
|
987
|
+
message,
|
|
988
|
+
ref: inputRef,
|
|
989
|
+
...appendErrorsCurry(INPUT_VALIDATION_RULES.required, message),
|
|
990
|
+
};
|
|
991
|
+
if (!validateAllFieldCriteria) {
|
|
992
|
+
setCustomValidity(message);
|
|
993
|
+
return error;
|
|
994
|
+
}
|
|
995
|
+
}
|
|
996
|
+
}
|
|
997
|
+
if (!isEmpty && (!isNullOrUndefined(min) || !isNullOrUndefined(max))) {
|
|
998
|
+
let exceedMax;
|
|
999
|
+
let exceedMin;
|
|
1000
|
+
const maxOutput = getValueAndMessage(max);
|
|
1001
|
+
const minOutput = getValueAndMessage(min);
|
|
1002
|
+
if (!isNullOrUndefined(inputValue) && !isNaN(inputValue)) {
|
|
1003
|
+
const valueNumber = ref.valueAsNumber ||
|
|
1004
|
+
(inputValue ? +inputValue : inputValue);
|
|
1005
|
+
if (!isNullOrUndefined(maxOutput.value)) {
|
|
1006
|
+
exceedMax = valueNumber > maxOutput.value;
|
|
1007
|
+
}
|
|
1008
|
+
if (!isNullOrUndefined(minOutput.value)) {
|
|
1009
|
+
exceedMin = valueNumber < minOutput.value;
|
|
1010
|
+
}
|
|
1011
|
+
}
|
|
1012
|
+
else {
|
|
1013
|
+
const valueDate = ref.valueAsDate || new Date(inputValue);
|
|
1014
|
+
const convertTimeToDate = (time) => new Date(new Date().toDateString() + ' ' + time);
|
|
1015
|
+
const isTime = ref.type == 'time';
|
|
1016
|
+
const isWeek = ref.type == 'week';
|
|
1017
|
+
if (isString(maxOutput.value) && inputValue) {
|
|
1018
|
+
exceedMax = isTime
|
|
1019
|
+
? convertTimeToDate(inputValue) > convertTimeToDate(maxOutput.value)
|
|
1020
|
+
: isWeek
|
|
1021
|
+
? inputValue > maxOutput.value
|
|
1022
|
+
: valueDate > new Date(maxOutput.value);
|
|
1023
|
+
}
|
|
1024
|
+
if (isString(minOutput.value) && inputValue) {
|
|
1025
|
+
exceedMin = isTime
|
|
1026
|
+
? convertTimeToDate(inputValue) < convertTimeToDate(minOutput.value)
|
|
1027
|
+
: isWeek
|
|
1028
|
+
? inputValue < minOutput.value
|
|
1029
|
+
: valueDate < new Date(minOutput.value);
|
|
1030
|
+
}
|
|
1031
|
+
}
|
|
1032
|
+
if (exceedMax || exceedMin) {
|
|
1033
|
+
getMinMaxMessage(!!exceedMax, maxOutput.message, minOutput.message, INPUT_VALIDATION_RULES.max, INPUT_VALIDATION_RULES.min);
|
|
1034
|
+
if (!validateAllFieldCriteria) {
|
|
1035
|
+
setCustomValidity(error[name].message);
|
|
1036
|
+
return error;
|
|
1037
|
+
}
|
|
1038
|
+
}
|
|
1039
|
+
}
|
|
1040
|
+
if ((maxLength || minLength) &&
|
|
1041
|
+
!isEmpty &&
|
|
1042
|
+
(isString(inputValue) || (isFieldArray && Array.isArray(inputValue)))) {
|
|
1043
|
+
const maxLengthOutput = getValueAndMessage(maxLength);
|
|
1044
|
+
const minLengthOutput = getValueAndMessage(minLength);
|
|
1045
|
+
const exceedMax = !isNullOrUndefined(maxLengthOutput.value) &&
|
|
1046
|
+
inputValue.length > +maxLengthOutput.value;
|
|
1047
|
+
const exceedMin = !isNullOrUndefined(minLengthOutput.value) &&
|
|
1048
|
+
inputValue.length < +minLengthOutput.value;
|
|
1049
|
+
if (exceedMax || exceedMin) {
|
|
1050
|
+
getMinMaxMessage(exceedMax, maxLengthOutput.message, minLengthOutput.message);
|
|
1051
|
+
if (!validateAllFieldCriteria) {
|
|
1052
|
+
setCustomValidity(error[name].message);
|
|
1053
|
+
return error;
|
|
1054
|
+
}
|
|
1055
|
+
}
|
|
1056
|
+
}
|
|
1057
|
+
if (pattern && !isEmpty && isString(inputValue)) {
|
|
1058
|
+
const { value: patternValue, message } = getValueAndMessage(pattern);
|
|
1059
|
+
if (isRegex(patternValue) && !inputValue.match(patternValue)) {
|
|
1060
|
+
error[name] = {
|
|
1061
|
+
type: INPUT_VALIDATION_RULES.pattern,
|
|
1062
|
+
message,
|
|
1063
|
+
ref,
|
|
1064
|
+
...appendErrorsCurry(INPUT_VALIDATION_RULES.pattern, message),
|
|
1065
|
+
};
|
|
1066
|
+
if (!validateAllFieldCriteria) {
|
|
1067
|
+
setCustomValidity(message);
|
|
1068
|
+
return error;
|
|
1069
|
+
}
|
|
1070
|
+
}
|
|
1071
|
+
}
|
|
1072
|
+
if (validate) {
|
|
1073
|
+
if (isFunction(validate)) {
|
|
1074
|
+
const result = await validate(inputValue, formValues);
|
|
1075
|
+
const validateError = getValidateError(result, inputRef);
|
|
1076
|
+
if (validateError) {
|
|
1077
|
+
error[name] = {
|
|
1078
|
+
...validateError,
|
|
1079
|
+
...appendErrorsCurry(INPUT_VALIDATION_RULES.validate, validateError.message),
|
|
1080
|
+
};
|
|
1081
|
+
if (!validateAllFieldCriteria) {
|
|
1082
|
+
setCustomValidity(validateError.message);
|
|
1083
|
+
return error;
|
|
1084
|
+
}
|
|
1085
|
+
}
|
|
1086
|
+
}
|
|
1087
|
+
else if (isObject$1(validate)) {
|
|
1088
|
+
let validationResult = {};
|
|
1089
|
+
for (const key in validate) {
|
|
1090
|
+
if (!isEmptyObject(validationResult) && !validateAllFieldCriteria) {
|
|
1091
|
+
break;
|
|
1092
|
+
}
|
|
1093
|
+
const validateError = getValidateError(await validate[key](inputValue, formValues), inputRef, key);
|
|
1094
|
+
if (validateError) {
|
|
1095
|
+
validationResult = {
|
|
1096
|
+
...validateError,
|
|
1097
|
+
...appendErrorsCurry(key, validateError.message),
|
|
1098
|
+
};
|
|
1099
|
+
setCustomValidity(validateError.message);
|
|
1100
|
+
if (validateAllFieldCriteria) {
|
|
1101
|
+
error[name] = validationResult;
|
|
1102
|
+
}
|
|
1103
|
+
}
|
|
1104
|
+
}
|
|
1105
|
+
if (!isEmptyObject(validationResult)) {
|
|
1106
|
+
error[name] = {
|
|
1107
|
+
ref: inputRef,
|
|
1108
|
+
...validationResult,
|
|
1109
|
+
};
|
|
1110
|
+
if (!validateAllFieldCriteria) {
|
|
1111
|
+
return error;
|
|
1112
|
+
}
|
|
1113
|
+
}
|
|
1114
|
+
}
|
|
1115
|
+
}
|
|
1116
|
+
setCustomValidity(true);
|
|
1117
|
+
return error;
|
|
1118
|
+
};
|
|
1119
|
+
|
|
1118
1120
|
const defaultOptions$2 = {
|
|
1119
1121
|
mode: VALIDATION_MODE.onSubmit,
|
|
1120
1122
|
reValidateMode: VALIDATION_MODE.onChange,
|
|
@@ -1140,9 +1142,9 @@ function createFormControl(props = {}) {
|
|
|
1140
1142
|
errors: _options.errors || {},
|
|
1141
1143
|
disabled: _options.disabled || false,
|
|
1142
1144
|
};
|
|
1143
|
-
|
|
1145
|
+
const _fields = {};
|
|
1144
1146
|
let _defaultValues = isObject$1(_options.defaultValues) || isObject$1(_options.values)
|
|
1145
|
-
? cloneObject(_options.
|
|
1147
|
+
? cloneObject(_options.values || _options.defaultValues) || {}
|
|
1146
1148
|
: {};
|
|
1147
1149
|
let _formValues = _options.shouldUnregister
|
|
1148
1150
|
? {}
|
|
@@ -1154,6 +1156,7 @@ function createFormControl(props = {}) {
|
|
|
1154
1156
|
};
|
|
1155
1157
|
let _names = {
|
|
1156
1158
|
mount: new Set(),
|
|
1159
|
+
disabled: new Set(),
|
|
1157
1160
|
unMount: new Set(),
|
|
1158
1161
|
array: new Set(),
|
|
1159
1162
|
watch: new Set(),
|
|
@@ -1169,8 +1172,10 @@ function createFormControl(props = {}) {
|
|
|
1169
1172
|
isValid: false,
|
|
1170
1173
|
errors: false,
|
|
1171
1174
|
};
|
|
1175
|
+
let _proxySubscribeFormState = {
|
|
1176
|
+
..._proxyFormState,
|
|
1177
|
+
};
|
|
1172
1178
|
const _subjects = {
|
|
1173
|
-
values: createSubject(),
|
|
1174
1179
|
array: createSubject(),
|
|
1175
1180
|
state: createSubject(),
|
|
1176
1181
|
};
|
|
@@ -1181,10 +1186,13 @@ function createFormControl(props = {}) {
|
|
|
1181
1186
|
clearTimeout(timer);
|
|
1182
1187
|
timer = setTimeout(callback, wait);
|
|
1183
1188
|
};
|
|
1184
|
-
const
|
|
1185
|
-
if (
|
|
1189
|
+
const _setValid = async (shouldUpdateValid) => {
|
|
1190
|
+
if (!_options.disabled &&
|
|
1191
|
+
(_proxyFormState.isValid ||
|
|
1192
|
+
_proxySubscribeFormState.isValid ||
|
|
1193
|
+
shouldUpdateValid)) {
|
|
1186
1194
|
const isValid = _options.resolver
|
|
1187
|
-
? isEmptyObject((await
|
|
1195
|
+
? isEmptyObject((await _runSchema()).errors)
|
|
1188
1196
|
: await executeBuiltInValidation(_fields, true);
|
|
1189
1197
|
if (isValid !== _formState.isValid) {
|
|
1190
1198
|
_subjects.state.next({
|
|
@@ -1194,7 +1202,11 @@ function createFormControl(props = {}) {
|
|
|
1194
1202
|
}
|
|
1195
1203
|
};
|
|
1196
1204
|
const _updateIsValidating = (names, isValidating) => {
|
|
1197
|
-
if (
|
|
1205
|
+
if (!_options.disabled &&
|
|
1206
|
+
(_proxyFormState.isValidating ||
|
|
1207
|
+
_proxyFormState.validatingFields ||
|
|
1208
|
+
_proxySubscribeFormState.isValidating ||
|
|
1209
|
+
_proxySubscribeFormState.validatingFields)) {
|
|
1198
1210
|
(names || Array.from(_names.mount)).forEach((name) => {
|
|
1199
1211
|
if (name) {
|
|
1200
1212
|
isValidating
|
|
@@ -1208,8 +1220,8 @@ function createFormControl(props = {}) {
|
|
|
1208
1220
|
});
|
|
1209
1221
|
}
|
|
1210
1222
|
};
|
|
1211
|
-
const
|
|
1212
|
-
if (args && method) {
|
|
1223
|
+
const _setFieldArray = (name, values = [], method, args, shouldSetValues = true, shouldUpdateFieldsAndState = true) => {
|
|
1224
|
+
if (args && method && !_options.disabled) {
|
|
1213
1225
|
_state.action = true;
|
|
1214
1226
|
if (shouldUpdateFieldsAndState && Array.isArray(get(_fields, name))) {
|
|
1215
1227
|
const fieldValues = method(get(_fields, name), args.argA, args.argB);
|
|
@@ -1221,13 +1233,14 @@ function createFormControl(props = {}) {
|
|
|
1221
1233
|
shouldSetValues && set(_formState.errors, name, errors);
|
|
1222
1234
|
unsetEmptyArray(_formState.errors, name);
|
|
1223
1235
|
}
|
|
1224
|
-
if (_proxyFormState.touchedFields
|
|
1236
|
+
if ((_proxyFormState.touchedFields ||
|
|
1237
|
+
_proxySubscribeFormState.touchedFields) &&
|
|
1225
1238
|
shouldUpdateFieldsAndState &&
|
|
1226
1239
|
Array.isArray(get(_formState.touchedFields, name))) {
|
|
1227
1240
|
const touchedFields = method(get(_formState.touchedFields, name), args.argA, args.argB);
|
|
1228
1241
|
shouldSetValues && set(_formState.touchedFields, name, touchedFields);
|
|
1229
1242
|
}
|
|
1230
|
-
if (_proxyFormState.dirtyFields) {
|
|
1243
|
+
if (_proxyFormState.dirtyFields || _proxySubscribeFormState.dirtyFields) {
|
|
1231
1244
|
_formState.dirtyFields = getDirtyFields(_defaultValues, _formValues);
|
|
1232
1245
|
}
|
|
1233
1246
|
_subjects.state.next({
|
|
@@ -1264,7 +1277,7 @@ function createFormControl(props = {}) {
|
|
|
1264
1277
|
shouldSkipSetValueAs
|
|
1265
1278
|
? set(_formValues, name, shouldSkipSetValueAs ? defaultValue : getFieldValue(field._f))
|
|
1266
1279
|
: setFieldValue(name, defaultValue);
|
|
1267
|
-
_state.mount &&
|
|
1280
|
+
_state.mount && _setValid();
|
|
1268
1281
|
}
|
|
1269
1282
|
};
|
|
1270
1283
|
const updateTouchAndDirty = (name, fieldValue, isBlurEvent, shouldDirty, shouldRender) => {
|
|
@@ -1273,46 +1286,49 @@ function createFormControl(props = {}) {
|
|
|
1273
1286
|
const output = {
|
|
1274
1287
|
name,
|
|
1275
1288
|
};
|
|
1276
|
-
|
|
1277
|
-
|
|
1278
|
-
|
|
1279
|
-
|
|
1280
|
-
|
|
1281
|
-
|
|
1282
|
-
|
|
1283
|
-
|
|
1284
|
-
|
|
1285
|
-
|
|
1286
|
-
|
|
1287
|
-
|
|
1288
|
-
|
|
1289
|
-
shouldUpdateField =
|
|
1290
|
-
shouldUpdateField ||
|
|
1291
|
-
(_proxyFormState.dirtyFields &&
|
|
1292
|
-
isPreviousDirty !== !isCurrentFieldPristine);
|
|
1293
|
-
}
|
|
1294
|
-
if (isBlurEvent) {
|
|
1295
|
-
const isPreviousFieldTouched = get(_formState.touchedFields, name);
|
|
1296
|
-
if (!isPreviousFieldTouched) {
|
|
1297
|
-
set(_formState.touchedFields, name, isBlurEvent);
|
|
1298
|
-
output.touchedFields = _formState.touchedFields;
|
|
1289
|
+
if (!_options.disabled) {
|
|
1290
|
+
if (!isBlurEvent || shouldDirty) {
|
|
1291
|
+
if (_proxyFormState.isDirty || _proxySubscribeFormState.isDirty) {
|
|
1292
|
+
isPreviousDirty = _formState.isDirty;
|
|
1293
|
+
_formState.isDirty = output.isDirty = _getDirty();
|
|
1294
|
+
shouldUpdateField = isPreviousDirty !== output.isDirty;
|
|
1295
|
+
}
|
|
1296
|
+
const isCurrentFieldPristine = deepEqual(get(_defaultValues, name), fieldValue);
|
|
1297
|
+
isPreviousDirty = !!get(_formState.dirtyFields, name);
|
|
1298
|
+
isCurrentFieldPristine
|
|
1299
|
+
? unset(_formState.dirtyFields, name)
|
|
1300
|
+
: set(_formState.dirtyFields, name, true);
|
|
1301
|
+
output.dirtyFields = _formState.dirtyFields;
|
|
1299
1302
|
shouldUpdateField =
|
|
1300
1303
|
shouldUpdateField ||
|
|
1301
|
-
(_proxyFormState.
|
|
1302
|
-
|
|
1304
|
+
((_proxyFormState.dirtyFields ||
|
|
1305
|
+
_proxySubscribeFormState.dirtyFields) &&
|
|
1306
|
+
isPreviousDirty !== !isCurrentFieldPristine);
|
|
1303
1307
|
}
|
|
1308
|
+
if (isBlurEvent) {
|
|
1309
|
+
const isPreviousFieldTouched = get(_formState.touchedFields, name);
|
|
1310
|
+
if (!isPreviousFieldTouched) {
|
|
1311
|
+
set(_formState.touchedFields, name, isBlurEvent);
|
|
1312
|
+
output.touchedFields = _formState.touchedFields;
|
|
1313
|
+
shouldUpdateField =
|
|
1314
|
+
shouldUpdateField ||
|
|
1315
|
+
((_proxyFormState.touchedFields ||
|
|
1316
|
+
_proxySubscribeFormState.touchedFields) &&
|
|
1317
|
+
isPreviousFieldTouched !== isBlurEvent);
|
|
1318
|
+
}
|
|
1319
|
+
}
|
|
1320
|
+
shouldUpdateField && shouldRender && _subjects.state.next(output);
|
|
1304
1321
|
}
|
|
1305
|
-
shouldUpdateField && shouldRender && _subjects.state.next(output);
|
|
1306
1322
|
return shouldUpdateField ? output : {};
|
|
1307
1323
|
};
|
|
1308
1324
|
const shouldRenderByError = (name, isValid, error, fieldState) => {
|
|
1309
1325
|
const previousFieldError = get(_formState.errors, name);
|
|
1310
|
-
const shouldUpdateValid = _proxyFormState.isValid &&
|
|
1326
|
+
const shouldUpdateValid = (_proxyFormState.isValid || _proxySubscribeFormState.isValid) &&
|
|
1311
1327
|
isBoolean(isValid) &&
|
|
1312
1328
|
_formState.isValid !== isValid;
|
|
1313
|
-
if (
|
|
1329
|
+
if (_options.delayError && error) {
|
|
1314
1330
|
delayErrorCallback = debounce(() => updateErrors(name, error));
|
|
1315
|
-
delayErrorCallback(
|
|
1331
|
+
delayErrorCallback(_options.delayError);
|
|
1316
1332
|
}
|
|
1317
1333
|
else {
|
|
1318
1334
|
clearTimeout(timer);
|
|
@@ -1337,14 +1353,14 @@ function createFormControl(props = {}) {
|
|
|
1337
1353
|
_subjects.state.next(updatedFormState);
|
|
1338
1354
|
}
|
|
1339
1355
|
};
|
|
1340
|
-
const
|
|
1356
|
+
const _runSchema = async (name) => {
|
|
1341
1357
|
_updateIsValidating(name, true);
|
|
1342
1358
|
const result = await _options.resolver(_formValues, _options.context, getResolverOptions(name || _names.mount, _fields, _options.criteriaMode, _options.shouldUseNativeValidation));
|
|
1343
1359
|
_updateIsValidating(name);
|
|
1344
1360
|
return result;
|
|
1345
1361
|
};
|
|
1346
1362
|
const executeSchemaAndUpdateState = async (names) => {
|
|
1347
|
-
const { errors } = await
|
|
1363
|
+
const { errors } = await _runSchema(names);
|
|
1348
1364
|
if (names) {
|
|
1349
1365
|
for (const name of names) {
|
|
1350
1366
|
const error = get(errors, name);
|
|
@@ -1367,9 +1383,14 @@ function createFormControl(props = {}) {
|
|
|
1367
1383
|
const { _f, ...fieldValue } = field;
|
|
1368
1384
|
if (_f) {
|
|
1369
1385
|
const isFieldArrayRoot = _names.array.has(_f.name);
|
|
1370
|
-
|
|
1371
|
-
|
|
1372
|
-
|
|
1386
|
+
const isPromiseFunction = field._f && hasPromiseValidation(field._f);
|
|
1387
|
+
if (isPromiseFunction && _proxyFormState.validatingFields) {
|
|
1388
|
+
_updateIsValidating([name], true);
|
|
1389
|
+
}
|
|
1390
|
+
const fieldError = await validateField(field, _names.disabled, _formValues, shouldDisplayAllAssociatedErrors, _options.shouldUseNativeValidation && !shouldOnlyCheckValid, isFieldArrayRoot);
|
|
1391
|
+
if (isPromiseFunction && _proxyFormState.validatingFields) {
|
|
1392
|
+
_updateIsValidating([name]);
|
|
1393
|
+
}
|
|
1373
1394
|
if (fieldError[_f.name]) {
|
|
1374
1395
|
context.valid = false;
|
|
1375
1396
|
if (shouldOnlyCheckValid) {
|
|
@@ -1383,7 +1404,7 @@ function createFormControl(props = {}) {
|
|
|
1383
1404
|
: set(_formState.errors, _f.name, fieldError[_f.name])
|
|
1384
1405
|
: unset(_formState.errors, _f.name));
|
|
1385
1406
|
}
|
|
1386
|
-
fieldValue &&
|
|
1407
|
+
!isEmptyObject(fieldValue) &&
|
|
1387
1408
|
(await executeBuiltInValidation(fieldValue, shouldOnlyCheckValid, context));
|
|
1388
1409
|
}
|
|
1389
1410
|
}
|
|
@@ -1400,8 +1421,9 @@ function createFormControl(props = {}) {
|
|
|
1400
1421
|
}
|
|
1401
1422
|
_names.unMount = new Set();
|
|
1402
1423
|
};
|
|
1403
|
-
const _getDirty = (name, data) =>
|
|
1404
|
-
|
|
1424
|
+
const _getDirty = (name, data) => !_options.disabled &&
|
|
1425
|
+
(name && data && set(_formValues, name, data),
|
|
1426
|
+
!deepEqual(getValues(), _defaultValues));
|
|
1405
1427
|
const _getWatch = (names, defaultValue, isGlobal) => generateWatchOutput(names, _names, {
|
|
1406
1428
|
...(_state.mount
|
|
1407
1429
|
? _formValues
|
|
@@ -1411,7 +1433,7 @@ function createFormControl(props = {}) {
|
|
|
1411
1433
|
? { [names]: defaultValue }
|
|
1412
1434
|
: defaultValue),
|
|
1413
1435
|
}, isGlobal, defaultValue);
|
|
1414
|
-
const _getFieldArray = (name) => compact(get(_state.mount ? _formValues : _defaultValues, name,
|
|
1436
|
+
const _getFieldArray = (name) => compact(get(_state.mount ? _formValues : _defaultValues, name, _options.shouldUnregister ? get(_defaultValues, name, []) : []));
|
|
1415
1437
|
const setFieldValue = (name, value, options = {}) => {
|
|
1416
1438
|
const field = get(_fields, name);
|
|
1417
1439
|
let fieldValue = value;
|
|
@@ -1447,9 +1469,9 @@ function createFormControl(props = {}) {
|
|
|
1447
1469
|
else {
|
|
1448
1470
|
fieldReference.ref.value = fieldValue;
|
|
1449
1471
|
if (!fieldReference.ref.type) {
|
|
1450
|
-
_subjects.
|
|
1472
|
+
_subjects.state.next({
|
|
1451
1473
|
name,
|
|
1452
|
-
values:
|
|
1474
|
+
values: cloneObject(_formValues),
|
|
1453
1475
|
});
|
|
1454
1476
|
}
|
|
1455
1477
|
}
|
|
@@ -1465,7 +1487,7 @@ function createFormControl(props = {}) {
|
|
|
1465
1487
|
const fieldName = `${name}.${fieldKey}`;
|
|
1466
1488
|
const field = get(_fields, fieldName);
|
|
1467
1489
|
(_names.array.has(name) ||
|
|
1468
|
-
|
|
1490
|
+
isObject$1(fieldValue) ||
|
|
1469
1491
|
(field && !field._f)) &&
|
|
1470
1492
|
!isDateObject(fieldValue)
|
|
1471
1493
|
? setValues(fieldName, fieldValue, options)
|
|
@@ -1480,9 +1502,12 @@ function createFormControl(props = {}) {
|
|
|
1480
1502
|
if (isFieldArray) {
|
|
1481
1503
|
_subjects.array.next({
|
|
1482
1504
|
name,
|
|
1483
|
-
values:
|
|
1505
|
+
values: cloneObject(_formValues),
|
|
1484
1506
|
});
|
|
1485
|
-
if ((_proxyFormState.isDirty ||
|
|
1507
|
+
if ((_proxyFormState.isDirty ||
|
|
1508
|
+
_proxyFormState.dirtyFields ||
|
|
1509
|
+
_proxySubscribeFormState.isDirty ||
|
|
1510
|
+
_proxySubscribeFormState.dirtyFields) &&
|
|
1486
1511
|
options.shouldDirty) {
|
|
1487
1512
|
_subjects.state.next({
|
|
1488
1513
|
name,
|
|
@@ -1497,9 +1522,9 @@ function createFormControl(props = {}) {
|
|
|
1497
1522
|
: setFieldValue(name, cloneValue, options);
|
|
1498
1523
|
}
|
|
1499
1524
|
isWatched(name, _names) && _subjects.state.next({ ..._formState });
|
|
1500
|
-
_subjects.
|
|
1525
|
+
_subjects.state.next({
|
|
1501
1526
|
name: _state.mount ? name : undefined,
|
|
1502
|
-
values:
|
|
1527
|
+
values: cloneObject(_formValues),
|
|
1503
1528
|
});
|
|
1504
1529
|
};
|
|
1505
1530
|
const onChange = async (event) => {
|
|
@@ -1508,16 +1533,18 @@ function createFormControl(props = {}) {
|
|
|
1508
1533
|
let name = target.name;
|
|
1509
1534
|
let isFieldValueUpdated = true;
|
|
1510
1535
|
const field = get(_fields, name);
|
|
1511
|
-
const getCurrentFieldValue = () => target.type ? getFieldValue(field._f) : getEventValue(event);
|
|
1512
1536
|
const _updateIsFieldValueUpdated = (fieldValue) => {
|
|
1513
1537
|
isFieldValueUpdated =
|
|
1514
1538
|
Number.isNaN(fieldValue) ||
|
|
1515
|
-
fieldValue
|
|
1539
|
+
(isDateObject(fieldValue) && isNaN(fieldValue.getTime())) ||
|
|
1540
|
+
deepEqual(fieldValue, get(_formValues, name, fieldValue));
|
|
1516
1541
|
};
|
|
1517
1542
|
if (field) {
|
|
1518
1543
|
let error;
|
|
1519
1544
|
let isValid;
|
|
1520
|
-
const fieldValue =
|
|
1545
|
+
const fieldValue = target.type
|
|
1546
|
+
? getFieldValue(field._f)
|
|
1547
|
+
: getEventValue(event);
|
|
1521
1548
|
const isBlurEvent = event.type === EVENTS.BLUR || event.type === EVENTS.FOCUS_OUT;
|
|
1522
1549
|
const shouldSkipValidation = (!hasValidation(field._f) &&
|
|
1523
1550
|
!_options.resolver &&
|
|
@@ -1533,22 +1560,31 @@ function createFormControl(props = {}) {
|
|
|
1533
1560
|
else if (field._f.onChange) {
|
|
1534
1561
|
field._f.onChange(event);
|
|
1535
1562
|
}
|
|
1536
|
-
const fieldState = updateTouchAndDirty(name, fieldValue, isBlurEvent
|
|
1563
|
+
const fieldState = updateTouchAndDirty(name, fieldValue, isBlurEvent);
|
|
1537
1564
|
const shouldRender = !isEmptyObject(fieldState) || watched;
|
|
1538
1565
|
!isBlurEvent &&
|
|
1539
|
-
_subjects.
|
|
1566
|
+
_subjects.state.next({
|
|
1540
1567
|
name,
|
|
1541
1568
|
type: event.type,
|
|
1542
|
-
values:
|
|
1569
|
+
values: cloneObject(_formValues),
|
|
1543
1570
|
});
|
|
1544
1571
|
if (shouldSkipValidation) {
|
|
1545
|
-
_proxyFormState.isValid
|
|
1572
|
+
if (_proxyFormState.isValid || _proxySubscribeFormState.isValid) {
|
|
1573
|
+
if (_options.mode === 'onBlur') {
|
|
1574
|
+
if (isBlurEvent) {
|
|
1575
|
+
_setValid();
|
|
1576
|
+
}
|
|
1577
|
+
}
|
|
1578
|
+
else if (!isBlurEvent) {
|
|
1579
|
+
_setValid();
|
|
1580
|
+
}
|
|
1581
|
+
}
|
|
1546
1582
|
return (shouldRender &&
|
|
1547
1583
|
_subjects.state.next({ name, ...(watched ? {} : fieldState) }));
|
|
1548
1584
|
}
|
|
1549
1585
|
!isBlurEvent && watched && _subjects.state.next({ ..._formState });
|
|
1550
1586
|
if (_options.resolver) {
|
|
1551
|
-
const { errors } = await
|
|
1587
|
+
const { errors } = await _runSchema([name]);
|
|
1552
1588
|
_updateIsFieldValueUpdated(fieldValue);
|
|
1553
1589
|
if (isFieldValueUpdated) {
|
|
1554
1590
|
const previousErrorLookupResult = schemaErrorLookup(_formState.errors, _fields, name);
|
|
@@ -1560,14 +1596,15 @@ function createFormControl(props = {}) {
|
|
|
1560
1596
|
}
|
|
1561
1597
|
else {
|
|
1562
1598
|
_updateIsValidating([name], true);
|
|
1563
|
-
error = (await validateField(field, _formValues, shouldDisplayAllAssociatedErrors, _options.shouldUseNativeValidation))[name];
|
|
1599
|
+
error = (await validateField(field, _names.disabled, _formValues, shouldDisplayAllAssociatedErrors, _options.shouldUseNativeValidation))[name];
|
|
1564
1600
|
_updateIsValidating([name]);
|
|
1565
1601
|
_updateIsFieldValueUpdated(fieldValue);
|
|
1566
1602
|
if (isFieldValueUpdated) {
|
|
1567
1603
|
if (error) {
|
|
1568
1604
|
isValid = false;
|
|
1569
1605
|
}
|
|
1570
|
-
else if (_proxyFormState.isValid
|
|
1606
|
+
else if (_proxyFormState.isValid ||
|
|
1607
|
+
_proxySubscribeFormState.isValid) {
|
|
1571
1608
|
isValid = await executeBuiltInValidation(_fields, true);
|
|
1572
1609
|
}
|
|
1573
1610
|
}
|
|
@@ -1602,14 +1639,15 @@ function createFormControl(props = {}) {
|
|
|
1602
1639
|
const field = get(_fields, fieldName);
|
|
1603
1640
|
return await executeBuiltInValidation(field && field._f ? { [fieldName]: field } : field);
|
|
1604
1641
|
}))).every(Boolean);
|
|
1605
|
-
!(!validationResult && !_formState.isValid) &&
|
|
1642
|
+
!(!validationResult && !_formState.isValid) && _setValid();
|
|
1606
1643
|
}
|
|
1607
1644
|
else {
|
|
1608
1645
|
validationResult = isValid = await executeBuiltInValidation(_fields);
|
|
1609
1646
|
}
|
|
1610
1647
|
_subjects.state.next({
|
|
1611
1648
|
...(!isString(name) ||
|
|
1612
|
-
(_proxyFormState.isValid
|
|
1649
|
+
((_proxyFormState.isValid || _proxySubscribeFormState.isValid) &&
|
|
1650
|
+
isValid !== _formState.isValid)
|
|
1613
1651
|
? {}
|
|
1614
1652
|
: { name }),
|
|
1615
1653
|
...(_options.resolver || !name ? { isValid } : {}),
|
|
@@ -1633,9 +1671,9 @@ function createFormControl(props = {}) {
|
|
|
1633
1671
|
const getFieldState = (name, formState) => ({
|
|
1634
1672
|
invalid: !!get((formState || _formState).errors, name),
|
|
1635
1673
|
isDirty: !!get((formState || _formState).dirtyFields, name),
|
|
1636
|
-
isTouched: !!get((formState || _formState).touchedFields, name),
|
|
1637
|
-
isValidating: !!get((formState || _formState).validatingFields, name),
|
|
1638
1674
|
error: get((formState || _formState).errors, name),
|
|
1675
|
+
isValidating: !!get(_formState.validatingFields, name),
|
|
1676
|
+
isTouched: !!get((formState || _formState).touchedFields, name),
|
|
1639
1677
|
});
|
|
1640
1678
|
const clearErrors = (name) => {
|
|
1641
1679
|
name &&
|
|
@@ -1662,10 +1700,33 @@ function createFormControl(props = {}) {
|
|
|
1662
1700
|
options && options.shouldFocus && ref && ref.focus && ref.focus();
|
|
1663
1701
|
};
|
|
1664
1702
|
const watch = (name, defaultValue) => isFunction(name)
|
|
1665
|
-
? _subjects.
|
|
1703
|
+
? _subjects.state.subscribe({
|
|
1666
1704
|
next: (payload) => name(_getWatch(undefined, defaultValue), payload),
|
|
1667
1705
|
})
|
|
1668
1706
|
: _getWatch(name, defaultValue, true);
|
|
1707
|
+
const _subscribe = (props) => _subjects.state.subscribe({
|
|
1708
|
+
next: (formState) => {
|
|
1709
|
+
if (shouldSubscribeByName(props.name, formState.name, props.exact) &&
|
|
1710
|
+
shouldRenderFormState(formState, props.formState || _proxyFormState, _setFormState, props.reRenderRoot)) {
|
|
1711
|
+
props.callback({
|
|
1712
|
+
values: { ..._formValues },
|
|
1713
|
+
..._formState,
|
|
1714
|
+
...formState,
|
|
1715
|
+
});
|
|
1716
|
+
}
|
|
1717
|
+
},
|
|
1718
|
+
}).unsubscribe;
|
|
1719
|
+
const subscribe = (props) => {
|
|
1720
|
+
_state.mount = true;
|
|
1721
|
+
_proxySubscribeFormState = {
|
|
1722
|
+
..._proxySubscribeFormState,
|
|
1723
|
+
...props.formState,
|
|
1724
|
+
};
|
|
1725
|
+
return _subscribe({
|
|
1726
|
+
...props,
|
|
1727
|
+
formState: _proxySubscribeFormState,
|
|
1728
|
+
});
|
|
1729
|
+
};
|
|
1669
1730
|
const unregister = (name, options = {}) => {
|
|
1670
1731
|
for (const fieldName of name ? convertToArrayPayload(name) : _names.mount) {
|
|
1671
1732
|
_names.mount.delete(fieldName);
|
|
@@ -1683,29 +1744,25 @@ function createFormControl(props = {}) {
|
|
|
1683
1744
|
!options.keepDefaultValue &&
|
|
1684
1745
|
unset(_defaultValues, fieldName);
|
|
1685
1746
|
}
|
|
1686
|
-
_subjects.
|
|
1687
|
-
values:
|
|
1747
|
+
_subjects.state.next({
|
|
1748
|
+
values: cloneObject(_formValues),
|
|
1688
1749
|
});
|
|
1689
1750
|
_subjects.state.next({
|
|
1690
1751
|
..._formState,
|
|
1691
1752
|
...(!options.keepDirty ? {} : { isDirty: _getDirty() }),
|
|
1692
1753
|
});
|
|
1693
|
-
!options.keepIsValid &&
|
|
1694
|
-
};
|
|
1695
|
-
const
|
|
1696
|
-
if ((isBoolean(disabled) && _state.mount) ||
|
|
1697
|
-
|
|
1698
|
-
|
|
1699
|
-
|
|
1700
|
-
? getFieldValue(field ? field._f : get(fields, name)._f)
|
|
1701
|
-
: value;
|
|
1702
|
-
set(_formValues, name, inputValue);
|
|
1703
|
-
updateTouchAndDirty(name, inputValue, false, false, true);
|
|
1754
|
+
!options.keepIsValid && _setValid();
|
|
1755
|
+
};
|
|
1756
|
+
const _setDisabledField = ({ disabled, name, }) => {
|
|
1757
|
+
if ((isBoolean(disabled) && _state.mount) ||
|
|
1758
|
+
!!disabled ||
|
|
1759
|
+
_names.disabled.has(name)) {
|
|
1760
|
+
disabled ? _names.disabled.add(name) : _names.disabled.delete(name);
|
|
1704
1761
|
}
|
|
1705
1762
|
};
|
|
1706
1763
|
const register = (name, options = {}) => {
|
|
1707
1764
|
let field = get(_fields, name);
|
|
1708
|
-
const disabledIsDefined = isBoolean(options.disabled);
|
|
1765
|
+
const disabledIsDefined = isBoolean(options.disabled) || isBoolean(_options.disabled);
|
|
1709
1766
|
set(_fields, name, {
|
|
1710
1767
|
...(field || {}),
|
|
1711
1768
|
_f: {
|
|
@@ -1717,18 +1774,20 @@ function createFormControl(props = {}) {
|
|
|
1717
1774
|
});
|
|
1718
1775
|
_names.mount.add(name);
|
|
1719
1776
|
if (field) {
|
|
1720
|
-
|
|
1721
|
-
|
|
1722
|
-
|
|
1777
|
+
_setDisabledField({
|
|
1778
|
+
disabled: isBoolean(options.disabled)
|
|
1779
|
+
? options.disabled
|
|
1780
|
+
: _options.disabled,
|
|
1723
1781
|
name,
|
|
1724
|
-
value: options.value,
|
|
1725
1782
|
});
|
|
1726
1783
|
}
|
|
1727
1784
|
else {
|
|
1728
1785
|
updateValidAndValue(name, true, options.value);
|
|
1729
1786
|
}
|
|
1730
1787
|
return {
|
|
1731
|
-
...(disabledIsDefined
|
|
1788
|
+
...(disabledIsDefined
|
|
1789
|
+
? { disabled: options.disabled || _options.disabled }
|
|
1790
|
+
: {}),
|
|
1732
1791
|
...(_options.progressive
|
|
1733
1792
|
? {
|
|
1734
1793
|
required: !!options.required,
|
|
@@ -1809,20 +1868,26 @@ function createFormControl(props = {}) {
|
|
|
1809
1868
|
let onValidError = undefined;
|
|
1810
1869
|
if (e) {
|
|
1811
1870
|
e.preventDefault && e.preventDefault();
|
|
1812
|
-
e.persist &&
|
|
1871
|
+
e.persist &&
|
|
1872
|
+
e.persist();
|
|
1813
1873
|
}
|
|
1814
1874
|
let fieldValues = cloneObject(_formValues);
|
|
1815
1875
|
_subjects.state.next({
|
|
1816
1876
|
isSubmitting: true,
|
|
1817
1877
|
});
|
|
1818
1878
|
if (_options.resolver) {
|
|
1819
|
-
const { errors, values } = await
|
|
1879
|
+
const { errors, values } = await _runSchema();
|
|
1820
1880
|
_formState.errors = errors;
|
|
1821
1881
|
fieldValues = values;
|
|
1822
1882
|
}
|
|
1823
1883
|
else {
|
|
1824
1884
|
await executeBuiltInValidation(_fields);
|
|
1825
1885
|
}
|
|
1886
|
+
if (_names.disabled.size) {
|
|
1887
|
+
for (const name of _names.disabled) {
|
|
1888
|
+
set(fieldValues, name, undefined);
|
|
1889
|
+
}
|
|
1890
|
+
}
|
|
1826
1891
|
unset(_formState.errors, 'root');
|
|
1827
1892
|
if (isEmptyObject(_formState.errors)) {
|
|
1828
1893
|
_subjects.state.next({
|
|
@@ -1873,7 +1938,7 @@ function createFormControl(props = {}) {
|
|
|
1873
1938
|
}
|
|
1874
1939
|
if (!options.keepError) {
|
|
1875
1940
|
unset(_formState.errors, name);
|
|
1876
|
-
_proxyFormState.isValid &&
|
|
1941
|
+
_proxyFormState.isValid && _setValid();
|
|
1877
1942
|
}
|
|
1878
1943
|
_subjects.state.next({ ..._formState });
|
|
1879
1944
|
}
|
|
@@ -1888,7 +1953,11 @@ function createFormControl(props = {}) {
|
|
|
1888
1953
|
}
|
|
1889
1954
|
if (!keepStateOptions.keepValues) {
|
|
1890
1955
|
if (keepStateOptions.keepDirtyValues) {
|
|
1891
|
-
|
|
1956
|
+
const fieldsToCheck = new Set([
|
|
1957
|
+
..._names.mount,
|
|
1958
|
+
...Object.keys(getDirtyFields(_defaultValues, _formValues)),
|
|
1959
|
+
]);
|
|
1960
|
+
for (const fieldName of Array.from(fieldsToCheck)) {
|
|
1892
1961
|
get(_formState.dirtyFields, fieldName)
|
|
1893
1962
|
? set(values, fieldName, get(_formValues, fieldName))
|
|
1894
1963
|
: setValue(fieldName, get(values, fieldName));
|
|
@@ -1912,17 +1981,15 @@ function createFormControl(props = {}) {
|
|
|
1912
1981
|
}
|
|
1913
1982
|
}
|
|
1914
1983
|
}
|
|
1915
|
-
|
|
1984
|
+
for (const fieldName of _names.mount) {
|
|
1985
|
+
setValue(fieldName, get(values, fieldName));
|
|
1986
|
+
}
|
|
1916
1987
|
}
|
|
1917
|
-
_formValues =
|
|
1918
|
-
? keepStateOptions.keepDefaultValues
|
|
1919
|
-
? cloneObject(_defaultValues)
|
|
1920
|
-
: {}
|
|
1921
|
-
: cloneObject(values);
|
|
1988
|
+
_formValues = cloneObject(values);
|
|
1922
1989
|
_subjects.array.next({
|
|
1923
1990
|
values: { ...values },
|
|
1924
1991
|
});
|
|
1925
|
-
_subjects.
|
|
1992
|
+
_subjects.state.next({
|
|
1926
1993
|
values: { ...values },
|
|
1927
1994
|
});
|
|
1928
1995
|
}
|
|
@@ -1930,6 +1997,7 @@ function createFormControl(props = {}) {
|
|
|
1930
1997
|
mount: keepStateOptions.keepDirtyValues ? _names.mount : new Set(),
|
|
1931
1998
|
unMount: new Set(),
|
|
1932
1999
|
array: new Set(),
|
|
2000
|
+
disabled: new Set(),
|
|
1933
2001
|
watch: new Set(),
|
|
1934
2002
|
watchAll: false,
|
|
1935
2003
|
focus: '',
|
|
@@ -1938,7 +2006,7 @@ function createFormControl(props = {}) {
|
|
|
1938
2006
|
!_proxyFormState.isValid ||
|
|
1939
2007
|
!!keepStateOptions.keepIsValid ||
|
|
1940
2008
|
!!keepStateOptions.keepDirtyValues;
|
|
1941
|
-
_state.watch = !!
|
|
2009
|
+
_state.watch = !!_options.shouldUnregister;
|
|
1942
2010
|
_subjects.state.next({
|
|
1943
2011
|
submitCount: keepStateOptions.keepSubmitCount
|
|
1944
2012
|
? _formState.submitCount
|
|
@@ -1953,14 +2021,16 @@ function createFormControl(props = {}) {
|
|
|
1953
2021
|
? _formState.isSubmitted
|
|
1954
2022
|
: false,
|
|
1955
2023
|
dirtyFields: isEmptyResetValues
|
|
1956
|
-
?
|
|
2024
|
+
? {}
|
|
1957
2025
|
: keepStateOptions.keepDirtyValues
|
|
1958
2026
|
? keepStateOptions.keepDefaultValues && _formValues
|
|
1959
2027
|
? getDirtyFields(_defaultValues, _formValues)
|
|
1960
2028
|
: _formState.dirtyFields
|
|
1961
2029
|
: keepStateOptions.keepDefaultValues && formValues
|
|
1962
2030
|
? getDirtyFields(_defaultValues, formValues)
|
|
1963
|
-
:
|
|
2031
|
+
: keepStateOptions.keepDirty
|
|
2032
|
+
? _formState.dirtyFields
|
|
2033
|
+
: {},
|
|
1964
2034
|
touchedFields: keepStateOptions.keepTouched
|
|
1965
2035
|
? _formState.touchedFields
|
|
1966
2036
|
: {},
|
|
@@ -1983,11 +2053,13 @@ function createFormControl(props = {}) {
|
|
|
1983
2053
|
: fieldReference.ref;
|
|
1984
2054
|
if (fieldRef.focus) {
|
|
1985
2055
|
fieldRef.focus();
|
|
1986
|
-
options.shouldSelect &&
|
|
2056
|
+
options.shouldSelect &&
|
|
2057
|
+
isFunction(fieldRef.select) &&
|
|
2058
|
+
fieldRef.select();
|
|
1987
2059
|
}
|
|
1988
2060
|
}
|
|
1989
2061
|
};
|
|
1990
|
-
const
|
|
2062
|
+
const _setFormState = (updatedFormState) => {
|
|
1991
2063
|
_formState = {
|
|
1992
2064
|
..._formState,
|
|
1993
2065
|
...updatedFormState,
|
|
@@ -2000,28 +2072,28 @@ function createFormControl(props = {}) {
|
|
|
2000
2072
|
isLoading: false,
|
|
2001
2073
|
});
|
|
2002
2074
|
});
|
|
2003
|
-
|
|
2075
|
+
const methods = {
|
|
2004
2076
|
control: {
|
|
2005
2077
|
register,
|
|
2006
2078
|
unregister,
|
|
2007
2079
|
getFieldState,
|
|
2008
2080
|
handleSubmit,
|
|
2009
2081
|
setError,
|
|
2010
|
-
|
|
2082
|
+
_subscribe,
|
|
2083
|
+
_runSchema,
|
|
2011
2084
|
_getWatch,
|
|
2012
2085
|
_getDirty,
|
|
2013
|
-
|
|
2014
|
-
|
|
2015
|
-
|
|
2016
|
-
|
|
2086
|
+
_setValid,
|
|
2087
|
+
_setFieldArray,
|
|
2088
|
+
_setDisabledField,
|
|
2089
|
+
_setErrors,
|
|
2017
2090
|
_getFieldArray,
|
|
2018
2091
|
_reset,
|
|
2019
2092
|
_resetDefaultValues,
|
|
2020
|
-
|
|
2093
|
+
_removeUnmounted,
|
|
2021
2094
|
_disableForm,
|
|
2022
2095
|
_subjects,
|
|
2023
2096
|
_proxyFormState,
|
|
2024
|
-
_setErrors,
|
|
2025
2097
|
get _fields() {
|
|
2026
2098
|
return _fields;
|
|
2027
2099
|
},
|
|
@@ -2046,9 +2118,6 @@ function createFormControl(props = {}) {
|
|
|
2046
2118
|
get _formState() {
|
|
2047
2119
|
return _formState;
|
|
2048
2120
|
},
|
|
2049
|
-
set _formState(value) {
|
|
2050
|
-
_formState = value;
|
|
2051
|
-
},
|
|
2052
2121
|
get _options() {
|
|
2053
2122
|
return _options;
|
|
2054
2123
|
},
|
|
@@ -2059,6 +2128,7 @@ function createFormControl(props = {}) {
|
|
|
2059
2128
|
};
|
|
2060
2129
|
},
|
|
2061
2130
|
},
|
|
2131
|
+
subscribe,
|
|
2062
2132
|
trigger,
|
|
2063
2133
|
register,
|
|
2064
2134
|
handleSubmit,
|
|
@@ -2073,6 +2143,10 @@ function createFormControl(props = {}) {
|
|
|
2073
2143
|
setFocus,
|
|
2074
2144
|
getFieldState,
|
|
2075
2145
|
};
|
|
2146
|
+
return {
|
|
2147
|
+
...methods,
|
|
2148
|
+
formControl: methods,
|
|
2149
|
+
};
|
|
2076
2150
|
}
|
|
2077
2151
|
|
|
2078
2152
|
/**
|
|
@@ -2105,8 +2179,8 @@ function createFormControl(props = {}) {
|
|
|
2105
2179
|
* ```
|
|
2106
2180
|
*/
|
|
2107
2181
|
function useForm(props = {}) {
|
|
2108
|
-
const _formControl = React$1.useRef();
|
|
2109
|
-
const _values = React$1.useRef();
|
|
2182
|
+
const _formControl = React$1.useRef(undefined);
|
|
2183
|
+
const _values = React$1.useRef(undefined);
|
|
2110
2184
|
const [formState, updateFormState] = React$1.useState({
|
|
2111
2185
|
isDirty: false,
|
|
2112
2186
|
isValidating: false,
|
|
@@ -2127,20 +2201,22 @@ function useForm(props = {}) {
|
|
|
2127
2201
|
});
|
|
2128
2202
|
if (!_formControl.current) {
|
|
2129
2203
|
_formControl.current = {
|
|
2130
|
-
...createFormControl(props),
|
|
2204
|
+
...(props.formControl ? props.formControl : createFormControl(props)),
|
|
2131
2205
|
formState,
|
|
2132
2206
|
};
|
|
2207
|
+
if (props.formControl &&
|
|
2208
|
+
props.defaultValues &&
|
|
2209
|
+
!isFunction(props.defaultValues)) {
|
|
2210
|
+
props.formControl.reset(props.defaultValues, props.resetOptions);
|
|
2211
|
+
}
|
|
2133
2212
|
}
|
|
2134
2213
|
const control = _formControl.current.control;
|
|
2135
2214
|
control._options = props;
|
|
2136
|
-
|
|
2137
|
-
|
|
2138
|
-
|
|
2139
|
-
|
|
2140
|
-
|
|
2141
|
-
}
|
|
2142
|
-
},
|
|
2143
|
-
});
|
|
2215
|
+
React$1.useLayoutEffect(() => control._subscribe({
|
|
2216
|
+
formState: control._proxyFormState,
|
|
2217
|
+
callback: () => updateFormState({ ...control._formState }),
|
|
2218
|
+
reRenderRoot: true,
|
|
2219
|
+
}), [control]);
|
|
2144
2220
|
React$1.useEffect(() => control._disableForm(props.disabled), [control, props.disabled]);
|
|
2145
2221
|
React$1.useEffect(() => {
|
|
2146
2222
|
if (control._proxyFormState.isDirty) {
|
|
@@ -2163,13 +2239,13 @@ function useForm(props = {}) {
|
|
|
2163
2239
|
}
|
|
2164
2240
|
}, [props.values, control]);
|
|
2165
2241
|
React$1.useEffect(() => {
|
|
2166
|
-
if (props.errors) {
|
|
2242
|
+
if (props.errors && !isEmptyObject(props.errors)) {
|
|
2167
2243
|
control._setErrors(props.errors);
|
|
2168
2244
|
}
|
|
2169
2245
|
}, [props.errors, control]);
|
|
2170
2246
|
React$1.useEffect(() => {
|
|
2171
2247
|
if (!control._state.mount) {
|
|
2172
|
-
control.
|
|
2248
|
+
control._setValid();
|
|
2173
2249
|
control._state.mount = true;
|
|
2174
2250
|
}
|
|
2175
2251
|
if (control._state.watch) {
|
|
@@ -2180,7 +2256,7 @@ function useForm(props = {}) {
|
|
|
2180
2256
|
});
|
|
2181
2257
|
React$1.useEffect(() => {
|
|
2182
2258
|
props.shouldUnregister &&
|
|
2183
|
-
control._subjects.
|
|
2259
|
+
control._subjects.state.next({
|
|
2184
2260
|
values: control._getWatch(),
|
|
2185
2261
|
});
|
|
2186
2262
|
}, [props.shouldUnregister, control]);
|
|
@@ -2188,6 +2264,10 @@ function useForm(props = {}) {
|
|
|
2188
2264
|
return _formControl.current;
|
|
2189
2265
|
}
|
|
2190
2266
|
|
|
2267
|
+
var t=function(t,n,e){if(t&&"reportValidity"in t){var i=get(e,n);t.setCustomValidity(i&&i.message||""),t.reportValidity();}},n=function(r,n){var e=function(e){var i=n.fields[e];i&&i.ref&&"reportValidity"in i.ref?t(i.ref,e,r):i.refs&&i.refs.forEach(function(n){return t(n,e,r)});};for(var i in n.fields)e(i);},e=function(r){return r instanceof Date},i=function(r){return null==r},a=function(r){return "object"==typeof r},o$1=function(r){return !i(r)&&!Array.isArray(r)&&a(r)&&!e(r)},f=function(r){return /^\w*$/.test(r)},s$1=function(r,t,n){for(var e=-1,i=f(t)?[t]:function(r){return t=r.replace(/["|']|\]/g,"").split(/\.|\[/),Array.isArray(t)?t.filter(Boolean):[];var t;}(t),a=i.length,s=a-1;++e<a;){var u=i[e],c=n;if(e!==s){var l=r[u];c=o$1(l)||Array.isArray(l)?l:isNaN(+i[e+1])?{}:[];}r[u]=c,r=r[u];}return r},u=function(t,e){e.shouldUseNativeValidation&&n(t,e);var i={};for(var a in t){var o=get(e.fields,a),f=Object.assign(t[a]||{},{ref:o&&o.ref});if(c(e.names||Object.keys(t),a)){var u=Object.assign({},get(i,a));s$1(u,"root",f),s$1(i,a,u);}else s$1(i,a,f);}return i},c=function(r,t){return r.some(function(r){return r.startsWith(t+".")})};
|
|
2268
|
+
|
|
2269
|
+
function o(o,n$1,a){return void 0===n$1&&(n$1={}),void 0===a&&(a={}),function(s,i,c){try{return Promise.resolve(function(t,r){try{var u=(n$1.context&&"development"===process.env.NODE_ENV&&console.warn("You should not used the yup options context. Please, use the 'useForm' context object instead"),Promise.resolve(o["sync"===a.mode?"validateSync":"validate"](s,Object.assign({abortEarly:!1},n$1,{context:i}))).then(function(t){return c.shouldUseNativeValidation&&n({},c),{values:a.raw?s:t,errors:{}}}));}catch(e){return r(e)}return u&&u.then?u.then(void 0,r):u}(0,function(e){if(!e.inner)throw e;return {values:{},errors:u((o=e,n=!c.shouldUseNativeValidation&&"all"===c.criteriaMode,(o.inner||[]).reduce(function(e,t){if(e[t.path]||(e[t.path]={message:t.message,type:t.type}),n){var o=e[t.path].types,a=o&&o[t.type];e[t.path]=appendErrors(t.path,n,e,t.type,a?[].concat(a,t.message):t.message);}return e},{})),c)};var o,n;}))}catch(e){return Promise.reject(e)}}}
|
|
2270
|
+
|
|
2191
2271
|
var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};
|
|
2192
2272
|
|
|
2193
2273
|
function getDefaultExportFromCjs (x) {
|
|
@@ -63193,4 +63273,5 @@ exports.useTheme = useTheme$2;
|
|
|
63193
63273
|
exports.useThemeProps = useThemeProps$1;
|
|
63194
63274
|
exports.withStyles = withStyles;
|
|
63195
63275
|
exports.withTheme = withTheme;
|
|
63276
|
+
exports.yupResolverTP = o;
|
|
63196
63277
|
//# sourceMappingURL=index.js.map
|