react-hook-form 7.20.5 → 7.21.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.esm.js CHANGED
@@ -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
 
@@ -448,68 +448,76 @@ const useFieldArray = (props) => {
448
448
  const append$1 = (value, options) => {
449
449
  const appendValue = convertToArrayPayload(value);
450
450
  const updatedFieldArrayValuesWithKey = append(mapCurrentIds(control._getFieldArray(name), _fieldIds, keyName), mapIds(appendValue, keyName));
451
+ const fieldArrayValues = updateValues(updatedFieldArrayValuesWithKey);
452
+ control._names.focus = getFocusFieldName(name, fieldArrayValues.length - 1, options);
451
453
  setFields(updatedFieldArrayValuesWithKey);
452
454
  control._updateFieldArray(name, append, {
453
455
  argA: fillEmptyArray(value),
454
- }, updateValues(updatedFieldArrayValuesWithKey));
455
- control._names.focus = getFocusFieldName(name, updatedFieldArrayValuesWithKey.length - appendValue.length, options);
456
+ }, fieldArrayValues);
456
457
  };
457
458
  const prepend$1 = (value, options) => {
458
459
  const updatedFieldArrayValuesWithKey = prepend(mapCurrentIds(control._getFieldArray(name), _fieldIds, keyName), mapIds(convertToArrayPayload(value), keyName));
460
+ const fieldArrayValues = updateValues(updatedFieldArrayValuesWithKey);
461
+ control._names.focus = getFocusFieldName(name, 0, options);
459
462
  setFields(updatedFieldArrayValuesWithKey);
460
463
  control._updateFieldArray(name, prepend, {
461
464
  argA: fillEmptyArray(value),
462
- }, updateValues(updatedFieldArrayValuesWithKey));
463
- control._names.focus = getFocusFieldName(name, 0, options);
465
+ }, fieldArrayValues);
464
466
  };
465
467
  const remove = (index) => {
466
468
  const updatedFieldArrayValuesWithKey = removeArrayAt(mapCurrentIds(control._getFieldArray(name), _fieldIds, keyName), index);
469
+ const fieldArrayValues = updateValues(updatedFieldArrayValuesWithKey);
467
470
  setFields(updatedFieldArrayValuesWithKey);
468
471
  control._updateFieldArray(name, removeArrayAt, {
469
472
  argA: index,
470
- }, updateValues(updatedFieldArrayValuesWithKey));
473
+ }, fieldArrayValues);
471
474
  };
472
475
  const insert$1 = (index, value, options) => {
473
476
  const updatedFieldArrayValuesWithKey = insert(mapCurrentIds(control._getFieldArray(name), _fieldIds, keyName), index, mapIds(convertToArrayPayload(value), keyName));
477
+ const fieldArrayValues = updateValues(updatedFieldArrayValuesWithKey);
478
+ control._names.focus = getFocusFieldName(name, index, options);
474
479
  setFields(updatedFieldArrayValuesWithKey);
475
480
  control._updateFieldArray(name, insert, {
476
481
  argA: index,
477
482
  argB: fillEmptyArray(value),
478
- }, updateValues(updatedFieldArrayValuesWithKey));
479
- control._names.focus = getFocusFieldName(name, index, options);
483
+ }, fieldArrayValues);
480
484
  };
481
485
  const swap = (indexA, indexB) => {
482
486
  const updatedFieldArrayValuesWithKey = mapCurrentIds(control._getFieldArray(name), _fieldIds, keyName);
483
487
  swapArrayAt(updatedFieldArrayValuesWithKey, indexA, indexB);
488
+ const fieldArrayValues = updateValues(updatedFieldArrayValuesWithKey);
484
489
  setFields(updatedFieldArrayValuesWithKey);
485
490
  control._updateFieldArray(name, swapArrayAt, {
486
491
  argA: indexA,
487
492
  argB: indexB,
488
- }, updateValues(updatedFieldArrayValuesWithKey), false);
493
+ }, fieldArrayValues, false);
489
494
  };
490
495
  const move = (from, to) => {
491
496
  const updatedFieldArrayValuesWithKey = mapCurrentIds(control._getFieldArray(name), _fieldIds, keyName);
492
497
  moveArrayAt(updatedFieldArrayValuesWithKey, from, to);
498
+ const fieldArrayValues = updateValues(updatedFieldArrayValuesWithKey);
499
+ setFields(updatedFieldArrayValuesWithKey);
493
500
  control._updateFieldArray(name, moveArrayAt, {
494
501
  argA: from,
495
502
  argB: to,
496
- }, updateValues(updatedFieldArrayValuesWithKey), false);
497
- setFields(updatedFieldArrayValuesWithKey);
503
+ }, fieldArrayValues, false);
498
504
  };
499
505
  const update = (index, value) => {
500
506
  const updatedFieldArrayValuesWithKey = mapCurrentIds(control._getFieldArray(name), _fieldIds, keyName);
501
507
  const updatedFieldArrayValues = updateAt(updatedFieldArrayValuesWithKey, index, value);
502
508
  _fieldIds.current = mapIds(updatedFieldArrayValues, keyName);
509
+ const fieldArrayValues = updateValues(_fieldIds.current);
503
510
  setFields(_fieldIds.current);
504
511
  control._updateFieldArray(name, updateAt, {
505
512
  argA: index,
506
513
  argB: value,
507
- }, updateValues(_fieldIds.current), true, false);
514
+ }, fieldArrayValues, true, false);
508
515
  };
509
516
  const replace = (value) => {
510
517
  const updatedFieldArrayValuesWithKey = mapIds(convertToArrayPayload(value), keyName);
518
+ const fieldArrayValues = updateValues(updatedFieldArrayValuesWithKey);
511
519
  setFields(updatedFieldArrayValuesWithKey);
512
- control._updateFieldArray(name, () => updatedFieldArrayValuesWithKey, {}, updateValues(updatedFieldArrayValuesWithKey), true, false);
520
+ control._updateFieldArray(name, () => updatedFieldArrayValuesWithKey, {}, fieldArrayValues, true, false);
513
521
  };
514
522
  React.useEffect(() => {
515
523
  control._stateFlags.action = false;
@@ -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,12 +905,10 @@ var skipValidation = (isBlurEvent, isTouched, isSubmitted, reValidateMode, mode)
885
905
  return true;
886
906
  };
887
907
 
888
- var unsetEmptyArray = (ref, name) => !compact(get(ref, name, [])).length && unset(ref, name);
908
+ var unsetEmptyArray = (ref, name) => !compact(get(ref, name)).length && unset(ref, name);
889
909
 
890
910
  var isMessage = (value) => isString(value) || React.isValidElement(value);
891
911
 
892
- var isRegex = (value) => value instanceof RegExp;
893
-
894
912
  function getValidateError(result, ref, type = 'validate') {
895
913
  if (isMessage(result) ||
896
914
  (Array.isArray(result) && result.every(isMessage)) ||
@@ -957,7 +975,8 @@ var validateField = async (field, inputValue, validateAllFieldCriteria, shouldUs
957
975
  const maxOutput = getValueAndMessage(max);
958
976
  const minOutput = getValueAndMessage(min);
959
977
  if (!isNaN(inputValue)) {
960
- const valueNumber = ref.valueAsNumber || parseFloat(inputValue);
978
+ const valueNumber = ref.valueAsNumber ||
979
+ parseFloat(inputValue);
961
980
  if (!isNullOrUndefined(maxOutput.value)) {
962
981
  exceedMax = valueNumber > maxOutput.value;
963
982
  }
@@ -1052,7 +1071,6 @@ const defaultOptions = {
1052
1071
  reValidateMode: VALIDATION_MODE.onChange,
1053
1072
  shouldFocusError: true,
1054
1073
  };
1055
- const isWindowUndefined = typeof window === 'undefined';
1056
1074
  function createFormControl(props = {}) {
1057
1075
  let _options = Object.assign(Object.assign({}, defaultOptions), props);
1058
1076
  let _formState = {
@@ -1374,7 +1392,7 @@ function createFormControl(props = {}) {
1374
1392
  name,
1375
1393
  });
1376
1394
  };
1377
- const handleChange = async (event) => {
1395
+ const onChange = async (event) => {
1378
1396
  const target = event.target;
1379
1397
  let name = target.name;
1380
1398
  const field = get(_fields, name);
@@ -1518,7 +1536,7 @@ function createFormControl(props = {}) {
1518
1536
  !options.keepIsValid && _updateValid();
1519
1537
  };
1520
1538
  const register = (name, options = {}) => {
1521
- const field = get(_fields, name);
1539
+ let field = get(_fields, name);
1522
1540
  set(_fields, name, {
1523
1541
  _f: Object.assign(Object.assign(Object.assign({}, (field && field._f ? field._f : { ref: { name } })), { name, mount: true }), options),
1524
1542
  });
@@ -1532,48 +1550,47 @@ function createFormControl(props = {}) {
1532
1550
  ? undefined
1533
1551
  : get(_formValues, name, getFieldValue(field._f)))
1534
1552
  : updateValidAndValue(name, true);
1535
- return isWindowUndefined
1536
- ? { name: name }
1537
- : Object.assign(Object.assign({ name }, (isBoolean(options.disabled)
1538
- ? { disabled: options.disabled }
1539
- : {})), { onChange: handleChange, onBlur: handleChange, ref: (ref) => {
1540
- if (ref) {
1541
- register(name, options);
1542
- let field = get(_fields, name);
1543
- const fieldRef = isUndefined(ref.value)
1544
- ? ref.querySelectorAll
1545
- ? ref.querySelectorAll('input,select,textarea')[0] ||
1546
- ref
1547
- : ref
1548
- : ref;
1549
- const isRadioOrCheckbox = isRadioOrCheckboxFunction(fieldRef);
1550
- if (fieldRef === field._f.ref ||
1551
- (isRadioOrCheckbox &&
1552
- compact(field._f.refs || []).find((option) => option === fieldRef))) {
1553
- return;
1554
- }
1555
- field = {
1556
- _f: isRadioOrCheckbox
1557
- ? Object.assign(Object.assign({}, field._f), { refs: [
1558
- ...compact(field._f.refs || []).filter(live),
1559
- fieldRef,
1560
- ], ref: { type: fieldRef.type, name } }) : Object.assign(Object.assign({}, field._f), { ref: fieldRef }),
1561
- };
1562
- set(_fields, name, field);
1563
- (!options || !options.disabled) &&
1564
- updateValidAndValue(name, false, fieldRef);
1553
+ return Object.assign(Object.assign(Object.assign({}, (isBoolean(options.disabled) ? { disabled: options.disabled } : {})), (_options.shouldUseNativeValidation
1554
+ ? {
1555
+ required: !!options.required,
1556
+ min: getRuleValue(options.min),
1557
+ max: getRuleValue(options.max),
1558
+ minLength: getRuleValue(options.minLength),
1559
+ maxLength: getRuleValue(options.maxLength),
1560
+ pattern: getRuleValue(options.pattern),
1561
+ }
1562
+ : {})), { name,
1563
+ onChange, onBlur: onChange, ref: (ref) => {
1564
+ if (ref) {
1565
+ register(name, options);
1566
+ field = get(_fields, name);
1567
+ const fieldRef = isUndefined(ref.value)
1568
+ ? ref.querySelectorAll
1569
+ ? ref.querySelectorAll('input,select,textarea')[0] || ref
1570
+ : ref
1571
+ : ref;
1572
+ const radioOrCheckbox = isRadioOrCheckbox(fieldRef);
1573
+ if (fieldRef === field._f.ref ||
1574
+ (radioOrCheckbox &&
1575
+ compact(field._f.refs).find((option) => option === fieldRef))) {
1576
+ return;
1565
1577
  }
1566
- else {
1567
- const field = get(_fields, name, {});
1568
- const shouldUnregister = _options.shouldUnregister || options.shouldUnregister;
1569
- if (field._f) {
1570
- field._f.mount = false;
1571
- }
1572
- shouldUnregister &&
1573
- !(isNameInFieldArray(_names.array, name) && _stateFlags.action) &&
1574
- _names.unMount.add(name);
1578
+ set(_fields, name, {
1579
+ _f: radioOrCheckbox
1580
+ ? 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 }),
1581
+ });
1582
+ !options.disabled && updateValidAndValue(name, false, fieldRef);
1583
+ }
1584
+ else {
1585
+ field = get(_fields, name, {});
1586
+ if (field._f) {
1587
+ field._f.mount = false;
1575
1588
  }
1576
- } });
1589
+ (_options.shouldUnregister || options.shouldUnregister) &&
1590
+ !(isNameInFieldArray(_names.array, name) && _stateFlags.action) &&
1591
+ _names.unMount.add(name);
1592
+ }
1593
+ } });
1577
1594
  };
1578
1595
  const handleSubmit = (onValid, onInvalid) => async (e) => {
1579
1596
  if (e) {