react-hook-form 7.5.3-beta.2 → 7.5.3

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
@@ -133,7 +133,7 @@ function useFormState(props) {
133
133
 
134
134
  function useController({ name, rules, defaultValue, control, shouldUnregister, }) {
135
135
  const methods = useFormContext();
136
- const { defaultValuesRef, register, fieldsRef, unregister, fieldArrayNamesRef, controllerSubjectRef, shouldUnmount, inFieldArrayActionRef, } = control || methods.control;
136
+ const { defaultValuesRef, register, fieldsRef, unregister, fieldArrayNamesRef, controllerSubjectRef, shouldUnmountUnregister, inFieldArrayActionRef, } = control || methods.control;
137
137
  const { onChange, onBlur, ref } = register(name, rules);
138
138
  const isFieldArray = isNameInFieldArray(fieldArrayNamesRef.current, name);
139
139
  const [value, setInputStateValue] = React.useState(isUndefined(get(fieldsRef.current, name)._f.value) || isFieldArray
@@ -153,10 +153,10 @@ function useController({ name, rules, defaultValue, control, shouldUnregister, }
153
153
  });
154
154
  return () => {
155
155
  controllerSubscription.unsubscribe();
156
- const shouldUnmountField = shouldUnmount || shouldUnregister;
156
+ const shouldUnmount = shouldUnmountUnregister || shouldUnregister;
157
157
  if (isFieldArray
158
- ? shouldUnmountField && !inFieldArrayActionRef.current
159
- : shouldUnmountField) {
158
+ ? shouldUnmount && !inFieldArrayActionRef.current
159
+ : shouldUnmount) {
160
160
  unregister(name);
161
161
  }
162
162
  else if (get(fieldsRef.current, name)) {
@@ -275,20 +275,21 @@ const getFieldsValues = (fieldsRef, defaultValuesRef, output = {}) => {
275
275
  if (field) {
276
276
  const _f = field._f;
277
277
  const current = omit(field, '_f');
278
- set(output, name, _f && _f.ref
278
+ set(output, name, _f
279
279
  ? _f.ref.disabled || (_f.refs && _f.refs.every((ref) => ref.disabled))
280
280
  ? undefined
281
281
  : _f.value
282
282
  : Array.isArray(field)
283
283
  ? []
284
284
  : {});
285
- current &&
285
+ if (current) {
286
286
  getFieldsValues({
287
287
  current,
288
288
  }, defaultValuesRef, output[name]);
289
+ }
289
290
  }
290
291
  }
291
- return output;
292
+ return Object.assign(Object.assign({}, defaultValuesRef), output);
292
293
  };
293
294
 
294
295
  var generateId = () => {
@@ -388,8 +389,6 @@ function insert(data, index, value) {
388
389
  ];
389
390
  }
390
391
 
391
- var isString = (value) => typeof value === 'string';
392
-
393
392
  var moveArrayAt = (data, from, to) => {
394
393
  if (Array.isArray(data)) {
395
394
  if (isUndefined(data[to])) {
@@ -466,7 +465,7 @@ function unset(object, path) {
466
465
  const useFieldArray = ({ control, name, keyName = 'id', shouldUnregister, }) => {
467
466
  const methods = useFormContext();
468
467
  const focusNameRef = React.useRef('');
469
- const { isWatchAllRef, watchFieldsRef, getIsDirty, watchSubjectRef, fieldArraySubjectRef, fieldArrayNamesRef, fieldsRef, defaultValuesRef, formStateRef, formStateSubjectRef, readFormStateRef, validFieldsRef, fieldsWithValidationRef, fieldArrayDefaultValuesRef, unregister, shouldUnmount, inFieldArrayActionRef, } = control || methods.control;
468
+ const { isWatchAllRef, watchFieldsRef, getIsDirty, watchSubjectRef, fieldArraySubjectRef, fieldArrayNamesRef, fieldsRef, defaultValuesRef, formStateRef, formStateSubjectRef, readFormStateRef, validFieldsRef, fieldsWithValidationRef, fieldArrayDefaultValuesRef, unregister, shouldUnmountUnregister, inFieldArrayActionRef, } = control || methods.control;
470
469
  const [fields, setFields] = React.useState(mapIds(get(fieldsRef.current, name)
471
470
  ? get(getFieldsValues(fieldsRef), name)
472
471
  : get(fieldArrayDefaultValuesRef.current, getNodeParentName(name))
@@ -476,7 +475,7 @@ const useFieldArray = ({ control, name, keyName = 'id', shouldUnregister, }) =>
476
475
  fieldArrayNamesRef.current.add(name);
477
476
  const omitKey = (fields) => fields.map((field) => omit((field || {}), keyName));
478
477
  const getCurrentFieldsValues = () => {
479
- const values = get(getFieldsValues(fieldsRef), name, []);
478
+ const values = get(getFieldsValues(fieldsRef, defaultValuesRef.current), name, []);
480
479
  return mapIds(get(fieldArrayDefaultValuesRef.current, name, []).map((item, index) => (Object.assign(Object.assign({}, item), values[index]))), keyName);
481
480
  };
482
481
  const getFocusDetail = (index, options) => options
@@ -616,10 +615,10 @@ const useFieldArray = ({ control, name, keyName = 'id', shouldUnregister, }) =>
616
615
  }
617
616
  watchSubjectRef.current.next({
618
617
  name,
619
- value: get(getFieldsValues(fieldsRef), name, []),
618
+ value: get(getFieldsValues(fieldsRef, defaultValuesRef.current), name, []),
620
619
  });
621
620
  focusNameRef.current &&
622
- focusFieldBy(fieldsRef.current, (key) => isString(key) && key.startsWith(focusNameRef.current));
621
+ focusFieldBy(fieldsRef.current, (key) => key.startsWith(focusNameRef.current));
623
622
  focusNameRef.current = '';
624
623
  fieldArraySubjectRef.current.next({
625
624
  name,
@@ -641,7 +640,7 @@ const useFieldArray = ({ control, name, keyName = 'id', shouldUnregister, }) =>
641
640
  !get(fieldsRef.current, name) && set(fieldsRef.current, name, []);
642
641
  return () => {
643
642
  fieldArraySubscription.unsubscribe();
644
- (shouldUnmount || shouldUnregister) && unregister(name);
643
+ (shouldUnmountUnregister || shouldUnregister) && unregister(name);
645
644
  };
646
645
  }, []);
647
646
  return {
@@ -776,6 +775,8 @@ var skipValidation = ({ isOnBlur, isOnChange, isOnTouch, isTouched, isReValidate
776
775
 
777
776
  var isFunction = (value) => typeof value === 'function';
778
777
 
778
+ var isString = (value) => typeof value === 'string';
779
+
779
780
  var isMessage = (value) => isString(value) || React.isValidElement(value);
780
781
 
781
782
  var isRegex = (value) => value instanceof RegExp;
@@ -1138,7 +1139,7 @@ function useForm({ mode = VALIDATION_MODE.onSubmit, reValidateMode = VALIDATION_
1138
1139
  return isUndefined(error);
1139
1140
  }, [isValidateAllFieldCriteria]);
1140
1141
  const executeSchemaOrResolverValidation = React.useCallback(async (names, currentNames = []) => {
1141
- const { errors } = await resolverRef.current(getFieldsValues(fieldsRef), contextRef.current, {
1142
+ const { errors } = await resolverRef.current(getFieldsValues(fieldsRef, shouldUnregister ? {} : defaultValuesRef.current), contextRef.current, {
1142
1143
  criteriaMode,
1143
1144
  names: currentNames,
1144
1145
  fields: getFields(fieldsNamesRef.current, fieldsRef.current),
@@ -1311,7 +1312,7 @@ function useForm({ mode = VALIDATION_MODE.onSubmit, reValidateMode = VALIDATION_
1311
1312
  isValidating: true,
1312
1313
  });
1313
1314
  if (resolverRef.current) {
1314
- const { errors } = await resolverRef.current(getFieldsValues(fieldsRef), contextRef.current, {
1315
+ const { errors } = await resolverRef.current(getFieldsValues(fieldsRef, shouldUnregister ? {} : defaultValuesRef.current), contextRef.current, {
1315
1316
  criteriaMode,
1316
1317
  fields: getFields([name], fieldsRef.current),
1317
1318
  names: [name],
@@ -1343,7 +1344,9 @@ function useForm({ mode = VALIDATION_MODE.onSubmit, reValidateMode = VALIDATION_
1343
1344
  }
1344
1345
  }, []);
1345
1346
  const getValues = (fieldNames) => {
1346
- const values = Object.assign(Object.assign({}, defaultValuesRef.current), getFieldsValues(fieldsRef));
1347
+ const values = isMountedRef.current
1348
+ ? getFieldsValues(fieldsRef, shouldUnregister ? {} : defaultValuesRef.current)
1349
+ : defaultValuesRef.current;
1347
1350
  return isUndefined(fieldNames)
1348
1351
  ? values
1349
1352
  : isString(fieldNames)
@@ -1353,7 +1356,7 @@ function useForm({ mode = VALIDATION_MODE.onSubmit, reValidateMode = VALIDATION_
1353
1356
  const updateIsValid = React.useCallback(async (values = {}) => {
1354
1357
  const previousIsValid = formStateRef.current.isValid;
1355
1358
  if (resolver) {
1356
- const { errors } = await resolverRef.current(Object.assign(Object.assign({}, getFieldsValues(fieldsRef)), values), contextRef.current, {
1359
+ const { errors } = await resolverRef.current(Object.assign(Object.assign({}, getFieldsValues(fieldsRef, shouldUnregister ? {} : defaultValuesRef.current)), values), contextRef.current, {
1357
1360
  criteriaMode,
1358
1361
  fields: getFields(fieldsNamesRef.current, fieldsRef.current),
1359
1362
  });
@@ -1388,17 +1391,18 @@ function useForm({ mode = VALIDATION_MODE.onSubmit, reValidateMode = VALIDATION_
1388
1391
  const watchInternal = React.useCallback((fieldNames, defaultValue, isGlobal) => {
1389
1392
  const isArrayNames = Array.isArray(fieldNames);
1390
1393
  const fieldValues = isMountedRef.current
1391
- ? Object.assign(Object.assign({}, defaultValuesRef.current), getFieldsValues(fieldsRef)) : isUndefined(defaultValue)
1392
- ? defaultValuesRef.current
1393
- : isArrayNames
1394
- ? defaultValue || {}
1395
- : { [fieldNames]: defaultValue };
1394
+ ? getFieldsValues(fieldsRef, defaultValuesRef.current)
1395
+ : isUndefined(defaultValue)
1396
+ ? defaultValuesRef.current
1397
+ : isArrayNames
1398
+ ? defaultValue || {}
1399
+ : { [fieldNames]: defaultValue };
1396
1400
  if (isUndefined(fieldNames)) {
1397
1401
  isGlobal && (isWatchAllRef.current = true);
1398
1402
  return fieldValues;
1399
1403
  }
1400
1404
  const result = [];
1401
- for (const fieldName of convertToArrayPayload(fieldNames)) {
1405
+ for (const fieldName of isArrayNames ? fieldNames : [fieldNames]) {
1402
1406
  isGlobal && watchFieldsRef.current.add(fieldName);
1403
1407
  result.push(get(fieldValues, fieldName));
1404
1408
  }
@@ -1504,7 +1508,7 @@ function useForm({ mode = VALIDATION_MODE.onSubmit, reValidateMode = VALIDATION_
1504
1508
  e.persist && e.persist();
1505
1509
  }
1506
1510
  let hasNoPromiseError = true;
1507
- let fieldValues = getFieldsValues(fieldsRef);
1511
+ let fieldValues = getFieldsValues(fieldsRef, shouldUnregister ? {} : defaultValuesRef.current);
1508
1512
  formStateSubjectRef.current.next({
1509
1513
  isSubmitting: true,
1510
1514
  });
@@ -1573,26 +1577,6 @@ function useForm({ mode = VALIDATION_MODE.onSubmit, reValidateMode = VALIDATION_
1573
1577
  isSubmitSuccessful: false,
1574
1578
  });
1575
1579
  }, []);
1576
- const registerAbsentFields = (value, name = '') => {
1577
- !get(fieldsRef.current, name) &&
1578
- (isPrimitive(value) ||
1579
- (isWeb && (value instanceof File || value instanceof Date))) &&
1580
- set(fieldsRef.current, name, {
1581
- _f: {
1582
- ref: { name, value },
1583
- value,
1584
- name,
1585
- },
1586
- });
1587
- if (Array.isArray(value) || isObject(value)) {
1588
- if (name && !get(fieldsRef.current, name)) {
1589
- set(fieldsRef.current, name, Array.isArray(value) ? [] : {});
1590
- }
1591
- for (const key in value) {
1592
- registerAbsentFields(value[key], name + (name ? '.' : '') + key);
1593
- }
1594
- }
1595
- };
1596
1580
  const reset = (values, keepStateOptions = {}) => {
1597
1581
  const updatedValues = values || defaultValuesRef.current;
1598
1582
  if (isWeb && !keepStateOptions.keepValues) {
@@ -1602,11 +1586,13 @@ function useForm({ mode = VALIDATION_MODE.onSubmit, reValidateMode = VALIDATION_
1602
1586
  const inputRef = Array.isArray(field._f.refs)
1603
1587
  ? field._f.refs[0]
1604
1588
  : field._f.ref;
1605
- try {
1606
- isHTMLElement(inputRef) && inputRef.closest('form').reset();
1607
- break;
1589
+ if (isHTMLElement(inputRef)) {
1590
+ try {
1591
+ inputRef.closest('form').reset();
1592
+ break;
1593
+ }
1594
+ catch (_a) { }
1608
1595
  }
1609
- catch (_a) { }
1610
1596
  }
1611
1597
  }
1612
1598
  }
@@ -1625,13 +1611,11 @@ function useForm({ mode = VALIDATION_MODE.onSubmit, reValidateMode = VALIDATION_
1625
1611
  isReset: true,
1626
1612
  });
1627
1613
  }
1628
- !keepStateOptions.keepDefaultValues &&
1629
- registerAbsentFields(Object.assign({}, updatedValues));
1630
1614
  resetFromState(keepStateOptions, values);
1615
+ isMountedRef.current = false;
1631
1616
  };
1632
1617
  const setFocus = (name) => get(fieldsRef.current, name)._f.ref.focus();
1633
1618
  React.useEffect(() => {
1634
- registerAbsentFields(defaultValuesRef.current);
1635
1619
  const formStateSubscription = formStateSubjectRef.current.subscribe({
1636
1620
  next(formState) {
1637
1621
  if (shouldRenderFormState(formState, readFormStateRef.current, true)) {
@@ -1659,14 +1643,14 @@ function useForm({ mode = VALIDATION_MODE.onSubmit, reValidateMode = VALIDATION_
1659
1643
  React.useEffect(() => {
1660
1644
  const isLiveInDom = (ref) => !isHTMLElement(ref) || !document.contains(ref);
1661
1645
  isMountedRef.current = true;
1662
- for (const name of unregisterFieldsNamesRef.current) {
1646
+ unregisterFieldsNamesRef.current.forEach((name) => {
1663
1647
  const field = get(fieldsRef.current, name);
1664
1648
  field &&
1665
1649
  (field._f.refs
1666
1650
  ? field._f.refs.every(isLiveInDom)
1667
1651
  : isLiveInDom(field._f.ref)) &&
1668
1652
  unregister(name);
1669
- }
1653
+ });
1670
1654
  unregisterFieldsNamesRef.current = new Set();
1671
1655
  });
1672
1656
  return {
@@ -1690,7 +1674,7 @@ function useForm({ mode = VALIDATION_MODE.onSubmit, reValidateMode = VALIDATION_
1690
1674
  defaultValuesRef,
1691
1675
  fieldArrayDefaultValuesRef,
1692
1676
  unregister,
1693
- shouldUnmount: shouldUnregister,
1677
+ shouldUnmountUnregister: shouldUnregister,
1694
1678
  }), []),
1695
1679
  formState: getProxyFormState(isProxyEnabled, formState, readFormStateRef),
1696
1680
  trigger,