react-hook-form 7.20.3 → 7.21.1-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.
@@ -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) => {
@@ -614,9 +622,6 @@ function deepEqual(object1, object2) {
614
622
  if (isPrimitive(object1) || isPrimitive(object2)) {
615
623
  return object1 === object2;
616
624
  }
617
- if (!isNaN(object1) || !isNaN(object2)) {
618
- return +object1 === +object2;
619
- }
620
625
  if (isDateObject(object1) && isDateObject(object2)) {
621
626
  return object1.getTime() === object2.getTime();
622
627
  }
@@ -662,7 +667,7 @@ var isMultipleSelect = (element) => element.type === `select-multiple`;
662
667
 
663
668
  var isRadioInput = (element) => element.type === 'radio';
664
669
 
665
- var isRadioOrCheckboxFunction = (ref) => isRadioInput(ref) || isCheckBoxInput(ref);
670
+ var isRadioOrCheckbox = (ref) => isRadioInput(ref) || isCheckBoxInput(ref);
666
671
 
667
672
  var isWeb = typeof window !== 'undefined' &&
668
673
  typeof window.HTMLElement !== 'undefined' &&
@@ -781,7 +786,7 @@ var getFieldValueAs = (value, { valueAsNumber, valueAsDate, setValueAs }) => isU
781
786
  ? value === ''
782
787
  ? NaN
783
788
  : +value
784
- : valueAsDate
789
+ : valueAsDate && isString(value)
785
790
  ? new Date(value)
786
791
  : setValueAs
787
792
  ? setValueAs(value)
@@ -834,6 +839,18 @@ var getResolverOptions = (fieldsNames, _fields, criteriaMode, shouldUseNativeVal
834
839
  };
835
840
  };
836
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
+
837
854
  var hasValidation = (options) => options.mount &&
838
855
  (options.required ||
839
856
  options.min ||
@@ -888,9 +905,9 @@ var skipValidation = (isBlurEvent, isTouched, isSubmitted, reValidateMode, mode)
888
905
  return true;
889
906
  };
890
907
 
891
- var isMessage = (value) => isString(value) || React.isValidElement(value);
908
+ var unsetEmptyArray = (ref, name) => !compact(get(ref, name)).length && unset(ref, name);
892
909
 
893
- var isRegex = (value) => value instanceof RegExp;
910
+ var isMessage = (value) => isString(value) || React.isValidElement(value);
894
911
 
895
912
  function getValidateError(result, ref, type = 'validate') {
896
913
  if (isMessage(result) ||
@@ -958,7 +975,8 @@ var validateField = async (field, inputValue, validateAllFieldCriteria, shouldUs
958
975
  const maxOutput = getValueAndMessage(max);
959
976
  const minOutput = getValueAndMessage(min);
960
977
  if (!isNaN(inputValue)) {
961
- const valueNumber = ref.valueAsNumber || parseFloat(inputValue);
978
+ const valueNumber = ref.valueAsNumber ||
979
+ parseFloat(inputValue);
962
980
  if (!isNullOrUndefined(maxOutput.value)) {
963
981
  exceedMax = valueNumber > maxOutput.value;
964
982
  }
@@ -1053,7 +1071,6 @@ const defaultOptions = {
1053
1071
  reValidateMode: VALIDATION_MODE.onChange,
1054
1072
  shouldFocusError: true,
1055
1073
  };
1056
- const isWindowUndefined = typeof window === 'undefined';
1057
1074
  function createFormControl(props = {}) {
1058
1075
  let _options = Object.assign(Object.assign({}, defaultOptions), props);
1059
1076
  let _formState = {
@@ -1131,11 +1148,13 @@ function createFormControl(props = {}) {
1131
1148
  if (Array.isArray(get(_formState.errors, name))) {
1132
1149
  const errors = method(get(_formState.errors, name), args.argA, args.argB);
1133
1150
  shouldSetValues && set(_formState.errors, name, errors);
1151
+ unsetEmptyArray(_formState.errors, name);
1134
1152
  }
1135
1153
  if (_proxyFormState.touchedFields && get(_formState.touchedFields, name)) {
1136
1154
  const touchedFields = method(get(_formState.touchedFields, name), args.argA, args.argB);
1137
1155
  shouldSetValues &&
1138
1156
  set(_formState.touchedFields, name, touchedFields);
1157
+ unsetEmptyArray(_formState.touchedFields, name);
1139
1158
  }
1140
1159
  if (_proxyFormState.dirtyFields || _proxyFormState.isDirty) {
1141
1160
  _formState.dirtyFields = getDirtyFields(_defaultValues, _formValues);
@@ -1324,6 +1343,11 @@ function createFormControl(props = {}) {
1324
1343
  }
1325
1344
  else if (!isFileInput(fieldReference.ref)) {
1326
1345
  fieldReference.ref.value = fieldValue;
1346
+ if (!fieldReference.ref.type) {
1347
+ _subjects.watch.next({
1348
+ name,
1349
+ });
1350
+ }
1327
1351
  }
1328
1352
  }
1329
1353
  }
@@ -1373,7 +1397,7 @@ function createFormControl(props = {}) {
1373
1397
  name,
1374
1398
  });
1375
1399
  };
1376
- const handleChange = async (event) => {
1400
+ const onChange = async (event) => {
1377
1401
  const target = event.target;
1378
1402
  let name = target.name;
1379
1403
  const field = get(_fields, name);
@@ -1517,7 +1541,7 @@ function createFormControl(props = {}) {
1517
1541
  !options.keepIsValid && _updateValid();
1518
1542
  };
1519
1543
  const register = (name, options = {}) => {
1520
- const field = get(_fields, name);
1544
+ let field = get(_fields, name);
1521
1545
  set(_fields, name, {
1522
1546
  _f: Object.assign(Object.assign(Object.assign({}, (field && field._f ? field._f : { ref: { name } })), { name, mount: true }), options),
1523
1547
  });
@@ -1531,48 +1555,47 @@ function createFormControl(props = {}) {
1531
1555
  ? undefined
1532
1556
  : get(_formValues, name, getFieldValue(field._f)))
1533
1557
  : updateValidAndValue(name, true);
1534
- return isWindowUndefined
1535
- ? { name: name }
1536
- : Object.assign(Object.assign({ name }, (isBoolean(options.disabled)
1537
- ? { disabled: options.disabled }
1538
- : {})), { onChange: handleChange, onBlur: handleChange, ref: (ref) => {
1539
- if (ref) {
1540
- register(name, options);
1541
- let field = get(_fields, name);
1542
- const fieldRef = isUndefined(ref.value)
1543
- ? ref.querySelectorAll
1544
- ? ref.querySelectorAll('input,select,textarea')[0] ||
1545
- ref
1546
- : ref
1547
- : ref;
1548
- const isRadioOrCheckbox = isRadioOrCheckboxFunction(fieldRef);
1549
- if (fieldRef === field._f.ref ||
1550
- (isRadioOrCheckbox &&
1551
- compact(field._f.refs || []).find((option) => option === fieldRef))) {
1552
- return;
1553
- }
1554
- field = {
1555
- _f: isRadioOrCheckbox
1556
- ? Object.assign(Object.assign({}, field._f), { refs: [
1557
- ...compact(field._f.refs || []).filter(live),
1558
- fieldRef,
1559
- ], ref: { type: fieldRef.type, name } }) : Object.assign(Object.assign({}, field._f), { ref: fieldRef }),
1560
- };
1561
- set(_fields, name, field);
1562
- (!options || !options.disabled) &&
1563
- 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;
1564
1582
  }
1565
- else {
1566
- const field = get(_fields, name, {});
1567
- const shouldUnregister = _options.shouldUnregister || options.shouldUnregister;
1568
- if (field._f) {
1569
- field._f.mount = false;
1570
- }
1571
- shouldUnregister &&
1572
- !(isNameInFieldArray(_names.array, name) && _stateFlags.action) &&
1573
- _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;
1574
1593
  }
1575
- } });
1594
+ (_options.shouldUnregister || options.shouldUnregister) &&
1595
+ !(isNameInFieldArray(_names.array, name) && _stateFlags.action) &&
1596
+ _names.unMount.add(name);
1597
+ }
1598
+ } });
1576
1599
  };
1577
1600
  const handleSubmit = (onValid, onInvalid) => async (e) => {
1578
1601
  if (e) {
@@ -1845,4 +1868,4 @@ function useForm(props = {}) {
1845
1868
  }
1846
1869
 
1847
1870
  export { Controller, FormProvider, appendErrors, get, set, useController, useFieldArray, useForm, useFormContext, useFormState, useWatch };
1848
- //# sourceMappingURL=index.esm.js.map
1871
+ //# sourceMappingURL=index.esm.mjs.map