react-hook-form 7.20.4 → 7.21.1

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.
@@ -22,7 +22,7 @@ var getNodeParentName = (name) => name.substring(0, name.search(/.\d/)) || name;
22
22
 
23
23
  var isNameInFieldArray = (names, name) => [...names].some((current) => getNodeParentName(name) === current);
24
24
 
25
- var compact = (value) => value.filter(Boolean);
25
+ var compact = (value) => (value || []).filter(Boolean);
26
26
 
27
27
  var isUndefined = (val) => val === undefined;
28
28
 
@@ -372,6 +372,31 @@ function append(data, value) {
372
372
  return [...convertToArrayPayload(data), ...convertToArrayPayload(value)];
373
373
  }
374
374
 
375
+ function cloneObject(data) {
376
+ let copy;
377
+ const isArray = Array.isArray(data);
378
+ if (data instanceof Date) {
379
+ copy = new Date(data);
380
+ }
381
+ else if (data instanceof Set) {
382
+ copy = new Set(data);
383
+ }
384
+ else if (isArray || isObject(data)) {
385
+ copy = isArray ? [] : {};
386
+ for (const key in data) {
387
+ if (isFunction(data[key])) {
388
+ copy = data;
389
+ break;
390
+ }
391
+ copy[key] = cloneObject(data[key]);
392
+ }
393
+ }
394
+ else {
395
+ return data;
396
+ }
397
+ return copy;
398
+ }
399
+
375
400
  var fillEmptyArray = (value) => Array.isArray(value) ? value.map(() => undefined) : undefined;
376
401
 
377
402
  function insert(data, index, value) {
@@ -446,70 +471,78 @@ const useFieldArray = (props) => {
446
471
  return updatedFieldArrayValues;
447
472
  }, [control, name, keyName]);
448
473
  const append$1 = (value, options) => {
449
- const appendValue = convertToArrayPayload(value);
474
+ const appendValue = convertToArrayPayload(cloneObject(value));
450
475
  const updatedFieldArrayValuesWithKey = append(mapCurrentIds(control._getFieldArray(name), _fieldIds, keyName), mapIds(appendValue, keyName));
476
+ const fieldArrayValues = updateValues(updatedFieldArrayValuesWithKey);
477
+ control._names.focus = getFocusFieldName(name, fieldArrayValues.length - 1, options);
451
478
  setFields(updatedFieldArrayValuesWithKey);
452
479
  control._updateFieldArray(name, append, {
453
480
  argA: fillEmptyArray(value),
454
- }, updateValues(updatedFieldArrayValuesWithKey));
455
- control._names.focus = getFocusFieldName(name, updatedFieldArrayValuesWithKey.length - appendValue.length, options);
481
+ }, fieldArrayValues);
456
482
  };
457
483
  const prepend$1 = (value, options) => {
458
- const updatedFieldArrayValuesWithKey = prepend(mapCurrentIds(control._getFieldArray(name), _fieldIds, keyName), mapIds(convertToArrayPayload(value), keyName));
484
+ const updatedFieldArrayValuesWithKey = prepend(mapCurrentIds(control._getFieldArray(name), _fieldIds, keyName), mapIds(convertToArrayPayload(cloneObject(value)), keyName));
485
+ const fieldArrayValues = updateValues(updatedFieldArrayValuesWithKey);
486
+ control._names.focus = getFocusFieldName(name, 0, options);
459
487
  setFields(updatedFieldArrayValuesWithKey);
460
488
  control._updateFieldArray(name, prepend, {
461
489
  argA: fillEmptyArray(value),
462
- }, updateValues(updatedFieldArrayValuesWithKey));
463
- control._names.focus = getFocusFieldName(name, 0, options);
490
+ }, fieldArrayValues);
464
491
  };
465
492
  const remove = (index) => {
466
493
  const updatedFieldArrayValuesWithKey = removeArrayAt(mapCurrentIds(control._getFieldArray(name), _fieldIds, keyName), index);
494
+ const fieldArrayValues = updateValues(updatedFieldArrayValuesWithKey);
467
495
  setFields(updatedFieldArrayValuesWithKey);
468
496
  control._updateFieldArray(name, removeArrayAt, {
469
497
  argA: index,
470
- }, updateValues(updatedFieldArrayValuesWithKey));
498
+ }, fieldArrayValues);
471
499
  };
472
500
  const insert$1 = (index, value, options) => {
473
- const updatedFieldArrayValuesWithKey = insert(mapCurrentIds(control._getFieldArray(name), _fieldIds, keyName), index, mapIds(convertToArrayPayload(value), keyName));
501
+ const updatedFieldArrayValuesWithKey = insert(mapCurrentIds(control._getFieldArray(name), _fieldIds, keyName), index, mapIds(convertToArrayPayload(cloneObject(value)), keyName));
502
+ const fieldArrayValues = updateValues(updatedFieldArrayValuesWithKey);
503
+ control._names.focus = getFocusFieldName(name, index, options);
474
504
  setFields(updatedFieldArrayValuesWithKey);
475
505
  control._updateFieldArray(name, insert, {
476
506
  argA: index,
477
507
  argB: fillEmptyArray(value),
478
- }, updateValues(updatedFieldArrayValuesWithKey));
479
- control._names.focus = getFocusFieldName(name, index, options);
508
+ }, fieldArrayValues);
480
509
  };
481
510
  const swap = (indexA, indexB) => {
482
511
  const updatedFieldArrayValuesWithKey = mapCurrentIds(control._getFieldArray(name), _fieldIds, keyName);
483
512
  swapArrayAt(updatedFieldArrayValuesWithKey, indexA, indexB);
513
+ const fieldArrayValues = updateValues(updatedFieldArrayValuesWithKey);
484
514
  setFields(updatedFieldArrayValuesWithKey);
485
515
  control._updateFieldArray(name, swapArrayAt, {
486
516
  argA: indexA,
487
517
  argB: indexB,
488
- }, updateValues(updatedFieldArrayValuesWithKey), false);
518
+ }, fieldArrayValues, false);
489
519
  };
490
520
  const move = (from, to) => {
491
521
  const updatedFieldArrayValuesWithKey = mapCurrentIds(control._getFieldArray(name), _fieldIds, keyName);
492
522
  moveArrayAt(updatedFieldArrayValuesWithKey, from, to);
523
+ const fieldArrayValues = updateValues(updatedFieldArrayValuesWithKey);
524
+ setFields(updatedFieldArrayValuesWithKey);
493
525
  control._updateFieldArray(name, moveArrayAt, {
494
526
  argA: from,
495
527
  argB: to,
496
- }, updateValues(updatedFieldArrayValuesWithKey), false);
497
- setFields(updatedFieldArrayValuesWithKey);
528
+ }, fieldArrayValues, false);
498
529
  };
499
530
  const update = (index, value) => {
500
531
  const updatedFieldArrayValuesWithKey = mapCurrentIds(control._getFieldArray(name), _fieldIds, keyName);
501
532
  const updatedFieldArrayValues = updateAt(updatedFieldArrayValuesWithKey, index, value);
502
533
  _fieldIds.current = mapIds(updatedFieldArrayValues, keyName);
534
+ const fieldArrayValues = updateValues(_fieldIds.current);
503
535
  setFields(_fieldIds.current);
504
536
  control._updateFieldArray(name, updateAt, {
505
537
  argA: index,
506
538
  argB: value,
507
- }, updateValues(_fieldIds.current), true, false);
539
+ }, fieldArrayValues, true, false);
508
540
  };
509
541
  const replace = (value) => {
510
542
  const updatedFieldArrayValuesWithKey = mapIds(convertToArrayPayload(value), keyName);
543
+ const fieldArrayValues = updateValues(updatedFieldArrayValuesWithKey);
511
544
  setFields(updatedFieldArrayValuesWithKey);
512
- control._updateFieldArray(name, () => updatedFieldArrayValuesWithKey, {}, updateValues(updatedFieldArrayValuesWithKey), true, false);
545
+ control._updateFieldArray(name, () => updatedFieldArrayValuesWithKey, {}, fieldArrayValues, true, false);
513
546
  };
514
547
  React.useEffect(() => {
515
548
  control._stateFlags.action = false;
@@ -555,31 +588,6 @@ const useFieldArray = (props) => {
555
588
  };
556
589
  };
557
590
 
558
- function cloneObject(data) {
559
- let copy;
560
- const isArray = Array.isArray(data);
561
- if (data instanceof Date) {
562
- copy = new Date(data);
563
- }
564
- else if (data instanceof Set) {
565
- copy = new Set(data);
566
- }
567
- else if (isArray || isObject(data)) {
568
- copy = isArray ? [] : {};
569
- for (const key in data) {
570
- if (isFunction(data[key])) {
571
- copy = data;
572
- break;
573
- }
574
- copy[key] = cloneObject(data[key]);
575
- }
576
- }
577
- else {
578
- return data;
579
- }
580
- return copy;
581
- }
582
-
583
591
  function createSubject() {
584
592
  let _observers = [];
585
593
  const next = (value) => {
@@ -659,7 +667,7 @@ var isMultipleSelect = (element) => element.type === `select-multiple`;
659
667
 
660
668
  var isRadioInput = (element) => element.type === 'radio';
661
669
 
662
- var isRadioOrCheckboxFunction = (ref) => isRadioInput(ref) || isCheckBoxInput(ref);
670
+ var isRadioOrCheckbox = (ref) => isRadioInput(ref) || isCheckBoxInput(ref);
663
671
 
664
672
  var isWeb = typeof window !== 'undefined' &&
665
673
  typeof window.HTMLElement !== 'undefined' &&
@@ -778,7 +786,7 @@ var getFieldValueAs = (value, { valueAsNumber, valueAsDate, setValueAs }) => isU
778
786
  ? value === ''
779
787
  ? NaN
780
788
  : +value
781
- : valueAsDate
789
+ : valueAsDate && isString(value)
782
790
  ? new Date(value)
783
791
  : setValueAs
784
792
  ? setValueAs(value)
@@ -831,6 +839,18 @@ var getResolverOptions = (fieldsNames, _fields, criteriaMode, shouldUseNativeVal
831
839
  };
832
840
  };
833
841
 
842
+ var isRegex = (value) => value instanceof RegExp;
843
+
844
+ var getRuleValue = (rule) => isUndefined(rule)
845
+ ? undefined
846
+ : isRegex(rule)
847
+ ? rule.source
848
+ : isObject(rule)
849
+ ? isRegex(rule.value)
850
+ ? rule.value.source
851
+ : rule.value
852
+ : rule;
853
+
834
854
  var hasValidation = (options) => options.mount &&
835
855
  (options.required ||
836
856
  options.min ||
@@ -885,9 +905,9 @@ var skipValidation = (isBlurEvent, isTouched, isSubmitted, reValidateMode, mode)
885
905
  return true;
886
906
  };
887
907
 
888
- var isMessage = (value) => isString(value) || React.isValidElement(value);
908
+ var unsetEmptyArray = (ref, name) => !compact(get(ref, name)).length && unset(ref, name);
889
909
 
890
- var isRegex = (value) => value instanceof RegExp;
910
+ var isMessage = (value) => isString(value) || React.isValidElement(value);
891
911
 
892
912
  function getValidateError(result, ref, type = 'validate') {
893
913
  if (isMessage(result) ||
@@ -955,7 +975,8 @@ var validateField = async (field, inputValue, validateAllFieldCriteria, shouldUs
955
975
  const maxOutput = getValueAndMessage(max);
956
976
  const minOutput = getValueAndMessage(min);
957
977
  if (!isNaN(inputValue)) {
958
- const valueNumber = ref.valueAsNumber || parseFloat(inputValue);
978
+ const valueNumber = ref.valueAsNumber ||
979
+ parseFloat(inputValue);
959
980
  if (!isNullOrUndefined(maxOutput.value)) {
960
981
  exceedMax = valueNumber > maxOutput.value;
961
982
  }
@@ -1050,7 +1071,6 @@ const defaultOptions = {
1050
1071
  reValidateMode: VALIDATION_MODE.onChange,
1051
1072
  shouldFocusError: true,
1052
1073
  };
1053
- const isWindowUndefined = typeof window === 'undefined';
1054
1074
  function createFormControl(props = {}) {
1055
1075
  let _options = Object.assign(Object.assign({}, defaultOptions), props);
1056
1076
  let _formState = {
@@ -1128,11 +1148,13 @@ function createFormControl(props = {}) {
1128
1148
  if (Array.isArray(get(_formState.errors, name))) {
1129
1149
  const errors = method(get(_formState.errors, name), args.argA, args.argB);
1130
1150
  shouldSetValues && set(_formState.errors, name, errors);
1151
+ unsetEmptyArray(_formState.errors, name);
1131
1152
  }
1132
1153
  if (_proxyFormState.touchedFields && get(_formState.touchedFields, name)) {
1133
1154
  const touchedFields = method(get(_formState.touchedFields, name), args.argA, args.argB);
1134
1155
  shouldSetValues &&
1135
1156
  set(_formState.touchedFields, name, touchedFields);
1157
+ unsetEmptyArray(_formState.touchedFields, name);
1136
1158
  }
1137
1159
  if (_proxyFormState.dirtyFields || _proxyFormState.isDirty) {
1138
1160
  _formState.dirtyFields = getDirtyFields(_defaultValues, _formValues);
@@ -1321,6 +1343,11 @@ function createFormControl(props = {}) {
1321
1343
  }
1322
1344
  else if (!isFileInput(fieldReference.ref)) {
1323
1345
  fieldReference.ref.value = fieldValue;
1346
+ if (!fieldReference.ref.type) {
1347
+ _subjects.watch.next({
1348
+ name,
1349
+ });
1350
+ }
1324
1351
  }
1325
1352
  }
1326
1353
  }
@@ -1370,7 +1397,7 @@ function createFormControl(props = {}) {
1370
1397
  name,
1371
1398
  });
1372
1399
  };
1373
- const handleChange = async (event) => {
1400
+ const onChange = async (event) => {
1374
1401
  const target = event.target;
1375
1402
  let name = target.name;
1376
1403
  const field = get(_fields, name);
@@ -1514,7 +1541,7 @@ function createFormControl(props = {}) {
1514
1541
  !options.keepIsValid && _updateValid();
1515
1542
  };
1516
1543
  const register = (name, options = {}) => {
1517
- const field = get(_fields, name);
1544
+ let field = get(_fields, name);
1518
1545
  set(_fields, name, {
1519
1546
  _f: Object.assign(Object.assign(Object.assign({}, (field && field._f ? field._f : { ref: { name } })), { name, mount: true }), options),
1520
1547
  });
@@ -1528,48 +1555,47 @@ function createFormControl(props = {}) {
1528
1555
  ? undefined
1529
1556
  : get(_formValues, name, getFieldValue(field._f)))
1530
1557
  : updateValidAndValue(name, true);
1531
- return isWindowUndefined
1532
- ? { name: name }
1533
- : Object.assign(Object.assign({ name }, (isBoolean(options.disabled)
1534
- ? { disabled: options.disabled }
1535
- : {})), { onChange: handleChange, onBlur: handleChange, ref: (ref) => {
1536
- if (ref) {
1537
- register(name, options);
1538
- let field = get(_fields, name);
1539
- const fieldRef = isUndefined(ref.value)
1540
- ? ref.querySelectorAll
1541
- ? ref.querySelectorAll('input,select,textarea')[0] ||
1542
- ref
1543
- : ref
1544
- : ref;
1545
- const isRadioOrCheckbox = isRadioOrCheckboxFunction(fieldRef);
1546
- if (fieldRef === field._f.ref ||
1547
- (isRadioOrCheckbox &&
1548
- compact(field._f.refs || []).find((option) => option === fieldRef))) {
1549
- return;
1550
- }
1551
- field = {
1552
- _f: isRadioOrCheckbox
1553
- ? Object.assign(Object.assign({}, field._f), { refs: [
1554
- ...compact(field._f.refs || []).filter(live),
1555
- fieldRef,
1556
- ], ref: { type: fieldRef.type, name } }) : Object.assign(Object.assign({}, field._f), { ref: fieldRef }),
1557
- };
1558
- set(_fields, name, field);
1559
- (!options || !options.disabled) &&
1560
- updateValidAndValue(name, false, fieldRef);
1558
+ return Object.assign(Object.assign(Object.assign({}, (isBoolean(options.disabled) ? { disabled: options.disabled } : {})), (_options.shouldUseNativeValidation
1559
+ ? {
1560
+ required: !!options.required,
1561
+ min: getRuleValue(options.min),
1562
+ max: getRuleValue(options.max),
1563
+ minLength: getRuleValue(options.minLength),
1564
+ maxLength: getRuleValue(options.maxLength),
1565
+ pattern: getRuleValue(options.pattern),
1566
+ }
1567
+ : {})), { name,
1568
+ onChange, onBlur: onChange, ref: (ref) => {
1569
+ if (ref) {
1570
+ register(name, options);
1571
+ field = get(_fields, name);
1572
+ const fieldRef = isUndefined(ref.value)
1573
+ ? ref.querySelectorAll
1574
+ ? ref.querySelectorAll('input,select,textarea')[0] || ref
1575
+ : ref
1576
+ : ref;
1577
+ const radioOrCheckbox = isRadioOrCheckbox(fieldRef);
1578
+ if (fieldRef === field._f.ref ||
1579
+ (radioOrCheckbox &&
1580
+ compact(field._f.refs).find((option) => option === fieldRef))) {
1581
+ return;
1561
1582
  }
1562
- else {
1563
- const field = get(_fields, name, {});
1564
- const shouldUnregister = _options.shouldUnregister || options.shouldUnregister;
1565
- if (field._f) {
1566
- field._f.mount = false;
1567
- }
1568
- shouldUnregister &&
1569
- !(isNameInFieldArray(_names.array, name) && _stateFlags.action) &&
1570
- _names.unMount.add(name);
1583
+ set(_fields, name, {
1584
+ _f: radioOrCheckbox
1585
+ ? Object.assign(Object.assign({}, field._f), { refs: [...compact(field._f.refs).filter(live), fieldRef], ref: { type: fieldRef.type, name } }) : Object.assign(Object.assign({}, field._f), { ref: fieldRef }),
1586
+ });
1587
+ !options.disabled && updateValidAndValue(name, false, fieldRef);
1588
+ }
1589
+ else {
1590
+ field = get(_fields, name, {});
1591
+ if (field._f) {
1592
+ field._f.mount = false;
1571
1593
  }
1572
- } });
1594
+ (_options.shouldUnregister || options.shouldUnregister) &&
1595
+ !(isNameInFieldArray(_names.array, name) && _stateFlags.action) &&
1596
+ _names.unMount.add(name);
1597
+ }
1598
+ } });
1573
1599
  };
1574
1600
  const handleSubmit = (onValid, onInvalid) => async (e) => {
1575
1601
  if (e) {
@@ -1842,4 +1868,4 @@ function useForm(props = {}) {
1842
1868
  }
1843
1869
 
1844
1870
  export { Controller, FormProvider, appendErrors, get, set, useController, useFieldArray, useForm, useFormContext, useFormState, useWatch };
1845
- //# sourceMappingURL=index.esm.js.map
1871
+ //# sourceMappingURL=index.esm.mjs.map