react-hook-form 7.39.3 → 7.39.5

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.
Files changed (47) hide show
  1. package/dist/__typetest__/__fixtures__/pathString.d.ts +2 -2
  2. package/dist/__typetest__/__fixtures__/pathString.d.ts.map +1 -1
  3. package/dist/__typetest__/__fixtures__/traversable.d.ts +3 -3
  4. package/dist/__typetest__/__fixtures__/traversable.d.ts.map +1 -1
  5. package/dist/__typetest__/__fixtures__/tuple.d.ts +2 -2
  6. package/dist/__typetest__/__fixtures__/tuple.d.ts.map +1 -1
  7. package/dist/index.cjs.js +1 -1
  8. package/dist/index.cjs.js.map +1 -1
  9. package/dist/index.esm.mjs +89 -93
  10. package/dist/index.esm.mjs.map +1 -1
  11. package/dist/index.umd.js +1 -1
  12. package/dist/index.umd.js.map +1 -1
  13. package/dist/logic/createFormControl.d.ts.map +1 -1
  14. package/dist/logic/generateWatchOutput.d.ts.map +1 -1
  15. package/dist/logic/getCheckboxValue.d.ts +1 -1
  16. package/dist/logic/getCheckboxValue.d.ts.map +1 -1
  17. package/dist/logic/getRadioValue.d.ts +1 -1
  18. package/dist/logic/getRadioValue.d.ts.map +1 -1
  19. package/dist/types/controller.d.ts +5 -5
  20. package/dist/types/controller.d.ts.map +1 -1
  21. package/dist/types/errors.d.ts +8 -8
  22. package/dist/types/errors.d.ts.map +1 -1
  23. package/dist/types/events.d.ts +1 -1
  24. package/dist/types/events.d.ts.map +1 -1
  25. package/dist/types/fieldArray.d.ts +13 -13
  26. package/dist/types/fieldArray.d.ts.map +1 -1
  27. package/dist/types/fields.d.ts +10 -10
  28. package/dist/types/fields.d.ts.map +1 -1
  29. package/dist/types/form.d.ts +50 -50
  30. package/dist/types/form.d.ts.map +1 -1
  31. package/dist/types/path/common.d.ts +32 -32
  32. package/dist/types/path/common.d.ts.map +1 -1
  33. package/dist/types/path/eager.d.ts +11 -11
  34. package/dist/types/path/eager.d.ts.map +1 -1
  35. package/dist/types/resolvers.d.ts +4 -4
  36. package/dist/types/resolvers.d.ts.map +1 -1
  37. package/dist/types/utils.d.ts +13 -13
  38. package/dist/types/utils.d.ts.map +1 -1
  39. package/dist/types/validator.d.ts +6 -6
  40. package/dist/types/validator.d.ts.map +1 -1
  41. package/dist/useFormState.d.ts.map +1 -1
  42. package/dist/useSubscribe.d.ts +1 -1
  43. package/dist/useSubscribe.d.ts.map +1 -1
  44. package/dist/useWatch.d.ts.map +1 -1
  45. package/dist/utils/createSubject.d.ts +3 -3
  46. package/dist/utils/createSubject.d.ts.map +1 -1
  47. package/package.json +2 -2
@@ -239,39 +239,69 @@ function useFormState(props) {
239
239
  });
240
240
  React.useEffect(() => {
241
241
  _mounted.current = true;
242
+ const isDirty = control._proxyFormState.isDirty && control._getDirty();
243
+ if (isDirty !== control._formState.isDirty) {
244
+ control._subjects.state.next({
245
+ isDirty,
246
+ });
247
+ }
248
+ control._updateValid();
242
249
  return () => {
243
250
  _mounted.current = false;
244
251
  };
245
- }, []);
252
+ }, [control]);
246
253
  return getProxyFormState(formState, control, _localProxyFormState.current, false);
247
254
  }
248
255
 
249
256
  var isString = (value) => typeof value === 'string';
250
257
 
251
258
  var generateWatchOutput = (names, _names, formValues, isGlobal) => {
252
- const isArray = Array.isArray(names);
253
259
  if (isString(names)) {
254
260
  isGlobal && _names.watch.add(names);
255
261
  return get(formValues, names);
256
262
  }
257
- if (isArray) {
258
- return names.map((fieldName) => (isGlobal && _names.watch.add(fieldName),
259
- get(formValues, fieldName)));
263
+ if (Array.isArray(names)) {
264
+ return names.map((fieldName) => (isGlobal && _names.watch.add(fieldName), get(formValues, fieldName)));
260
265
  }
261
- isGlobal && (_names.watchAll = true);
266
+ _names.watchAll = !!isGlobal;
262
267
  return formValues;
263
268
  };
264
269
 
265
- var isFunction = (value) => typeof value === 'function';
270
+ var isPlainObject = (tempObject) => {
271
+ const prototypeCopy = tempObject.constructor && tempObject.constructor.prototype;
272
+ return (isObject(prototypeCopy) && prototypeCopy.hasOwnProperty('isPrototypeOf'));
273
+ };
266
274
 
267
- var objectHasFunction = (data) => {
268
- for (const key in data) {
269
- if (isFunction(data[key])) {
270
- return true;
275
+ var isWeb = typeof window !== 'undefined' &&
276
+ typeof window.HTMLElement !== 'undefined' &&
277
+ typeof document !== 'undefined';
278
+
279
+ function cloneObject(data) {
280
+ let copy;
281
+ const isArray = Array.isArray(data);
282
+ if (data instanceof Date) {
283
+ copy = new Date(data);
284
+ }
285
+ else if (data instanceof Set) {
286
+ copy = new Set(data);
287
+ }
288
+ else if (!(isWeb && (data instanceof Blob || data instanceof FileList)) &&
289
+ (isArray || isObject(data))) {
290
+ copy = isArray ? [] : {};
291
+ if (!Array.isArray(data) && !isPlainObject(data)) {
292
+ copy = data;
293
+ }
294
+ else {
295
+ for (const key in data) {
296
+ copy[key] = cloneObject(data[key]);
297
+ }
271
298
  }
272
299
  }
273
- return false;
274
- };
300
+ else {
301
+ return data;
302
+ }
303
+ return copy;
304
+ }
275
305
 
276
306
  /**
277
307
  * Custom hook to subscribe to field change and isolate re-rendering at the component level.
@@ -297,19 +327,12 @@ function useWatch(props) {
297
327
  useSubscribe({
298
328
  disabled,
299
329
  subject: control._subjects.watch,
300
- callback: React.useCallback((formState) => {
330
+ callback: (formState) => {
301
331
  if (shouldSubscribeByName(_name.current, formState.name, exact)) {
302
332
  const fieldValues = generateWatchOutput(_name.current, control._names, formState.values || control._formValues);
303
- updateValue(isUndefined(_name.current) ||
304
- (isObject(fieldValues) && !objectHasFunction(fieldValues))
305
- ? { ...fieldValues }
306
- : Array.isArray(fieldValues)
307
- ? [...fieldValues]
308
- : isUndefined(fieldValues)
309
- ? defaultValue
310
- : fieldValues);
333
+ updateValue(isUndefined(fieldValues) ? defaultValue : cloneObject(fieldValues));
311
334
  }
312
- }, [control, exact, defaultValue]),
335
+ },
313
336
  });
314
337
  const [value, updateValue] = React.useState(isUndefined(defaultValue)
315
338
  ? control._getWatch(name)
@@ -562,6 +585,8 @@ var isBoolean = (value) => typeof value === 'boolean';
562
585
 
563
586
  var isFileInput = (element) => element.type === 'file';
564
587
 
588
+ var isFunction = (value) => typeof value === 'function';
589
+
565
590
  var isMessage = (value) => isString(value) || React.isValidElement(value);
566
591
 
567
592
  var isRadioInput = (element) => element.type === 'radio';
@@ -632,8 +657,8 @@ var validateField = async (field, inputValue, validateAllFieldCriteria, shouldUs
632
657
  }
633
658
  const inputRef = refs ? refs[0] : ref;
634
659
  const setCustomValidity = (message) => {
635
- if (shouldUseNativeValidation && isString(message)) {
636
- inputRef.setCustomValidity(message);
660
+ if (shouldUseNativeValidation && inputRef.reportValidity) {
661
+ inputRef.setCustomValidity(isBoolean(message) ? '' : message || '');
637
662
  inputRef.reportValidity();
638
663
  }
639
664
  };
@@ -804,42 +829,6 @@ function append(data, value) {
804
829
  return [...data, ...convertToArrayPayload(value)];
805
830
  }
806
831
 
807
- var isPlainObject = (tempObject) => {
808
- const prototypeCopy = tempObject.constructor && tempObject.constructor.prototype;
809
- return (isObject(prototypeCopy) && prototypeCopy.hasOwnProperty('isPrototypeOf'));
810
- };
811
-
812
- var isWeb = typeof window !== 'undefined' &&
813
- typeof window.HTMLElement !== 'undefined' &&
814
- typeof document !== 'undefined';
815
-
816
- function cloneObject(data) {
817
- let copy;
818
- const isArray = Array.isArray(data);
819
- if (data instanceof Date) {
820
- copy = new Date(data);
821
- }
822
- else if (data instanceof Set) {
823
- copy = new Set(data);
824
- }
825
- else if (!(isWeb && (data instanceof Blob || data instanceof FileList)) &&
826
- (isArray || isObject(data))) {
827
- copy = isArray ? [] : {};
828
- if (!Array.isArray(data) && !isPlainObject(data)) {
829
- copy = data;
830
- }
831
- else {
832
- for (const key in data) {
833
- copy[key] = cloneObject(data[key]);
834
- }
835
- }
836
- }
837
- else {
838
- return data;
839
- }
840
- return copy;
841
- }
842
-
843
832
  var fillEmptyArray = (value) => Array.isArray(value) ? value.map(() => undefined) : undefined;
844
833
 
845
834
  var getValidationModes = (mode) => ({
@@ -1226,6 +1215,15 @@ var isRadioOrCheckbox = (ref) => isRadioInput(ref) || isCheckBoxInput(ref);
1226
1215
 
1227
1216
  var live = (ref) => isHTMLElement(ref) && ref.isConnected;
1228
1217
 
1218
+ var objectHasFunction = (data) => {
1219
+ for (const key in data) {
1220
+ if (isFunction(data[key])) {
1221
+ return true;
1222
+ }
1223
+ }
1224
+ return false;
1225
+ };
1226
+
1229
1227
  function markFieldsDirty(data, fields = {}) {
1230
1228
  const isParentNodeArray = Array.isArray(data);
1231
1229
  if (isObject(data) || isParentNodeArray) {
@@ -1318,7 +1316,7 @@ var getResolverOptions = (fieldsNames, _fields, criteriaMode, shouldUseNativeVal
1318
1316
  };
1319
1317
 
1320
1318
  var getRuleValue = (rule) => isUndefined(rule)
1321
- ? undefined
1319
+ ? rule
1322
1320
  : isRegex(rule)
1323
1321
  ? rule.source
1324
1322
  : isObject(rule)
@@ -1466,8 +1464,7 @@ function createFormControl(props = {}) {
1466
1464
  const fieldValues = method(get(_fields, name), args.argA, args.argB);
1467
1465
  shouldSetValues && set(_fields, name, fieldValues);
1468
1466
  }
1469
- if (_proxyFormState.errors &&
1470
- shouldUpdateFieldsAndState &&
1467
+ if (shouldUpdateFieldsAndState &&
1471
1468
  Array.isArray(get(_formState.errors, name))) {
1472
1469
  const errors = method(get(_formState.errors, name), args.argA, args.argB);
1473
1470
  shouldSetValues && set(_formState.errors, name, errors);
@@ -1512,37 +1509,39 @@ function createFormControl(props = {}) {
1512
1509
  }
1513
1510
  };
1514
1511
  const updateTouchAndDirty = (name, fieldValue, isBlurEvent, shouldDirty, shouldRender) => {
1515
- let isFieldDirty = false;
1512
+ let shouldUpdateField = false;
1513
+ let isPreviousDirty = false;
1516
1514
  const output = {
1517
1515
  name,
1518
1516
  };
1519
- const isPreviousFieldTouched = get(_formState.touchedFields, name);
1520
1517
  if (_proxyFormState.isDirty) {
1521
- const isPreviousFormDirty = _formState.isDirty;
1518
+ isPreviousDirty = _formState.isDirty;
1522
1519
  _formState.isDirty = output.isDirty = _getDirty();
1523
- isFieldDirty = isPreviousFormDirty !== output.isDirty;
1520
+ shouldUpdateField = isPreviousDirty !== output.isDirty;
1524
1521
  }
1525
1522
  if (_proxyFormState.dirtyFields && (!isBlurEvent || shouldDirty)) {
1526
- const isPreviousFieldDirty = get(_formState.dirtyFields, name);
1523
+ isPreviousDirty = get(_formState.dirtyFields, name);
1527
1524
  const isCurrentFieldPristine = deepEqual(get(_defaultValues, name), fieldValue);
1528
1525
  isCurrentFieldPristine
1529
1526
  ? unset(_formState.dirtyFields, name)
1530
1527
  : set(_formState.dirtyFields, name, true);
1531
1528
  output.dirtyFields = _formState.dirtyFields;
1532
- isFieldDirty =
1533
- isFieldDirty ||
1534
- isPreviousFieldDirty !== get(_formState.dirtyFields, name);
1529
+ shouldUpdateField =
1530
+ shouldUpdateField || isPreviousDirty !== !isCurrentFieldPristine;
1535
1531
  }
1536
- if (isBlurEvent && !isPreviousFieldTouched) {
1537
- set(_formState.touchedFields, name, isBlurEvent);
1538
- output.touchedFields = _formState.touchedFields;
1539
- isFieldDirty =
1540
- isFieldDirty ||
1541
- (_proxyFormState.touchedFields &&
1542
- isPreviousFieldTouched !== isBlurEvent);
1532
+ if (isBlurEvent) {
1533
+ const isPreviousFieldTouched = get(_formState.touchedFields, name);
1534
+ if (!isPreviousFieldTouched) {
1535
+ set(_formState.touchedFields, name, isBlurEvent);
1536
+ output.touchedFields = _formState.touchedFields;
1537
+ shouldUpdateField =
1538
+ shouldUpdateField ||
1539
+ (_proxyFormState.touchedFields &&
1540
+ isPreviousFieldTouched !== isBlurEvent);
1541
+ }
1543
1542
  }
1544
- isFieldDirty && shouldRender && _subjects.state.next(output);
1545
- return isFieldDirty ? output : {};
1543
+ shouldUpdateField && shouldRender && _subjects.state.next(output);
1544
+ return shouldUpdateField ? output : {};
1546
1545
  };
1547
1546
  const shouldRenderByError = (name, isValid, error, fieldState) => {
1548
1547
  const previousFieldError = get(_formState.errors, name);
@@ -1644,18 +1643,15 @@ function createFormControl(props = {}) {
1644
1643
  };
1645
1644
  const _getDirty = (name, data) => (name && data && set(_formValues, name, data),
1646
1645
  !deepEqual(getValues(), _defaultValues));
1647
- const _getWatch = (names, defaultValue, isGlobal) => {
1648
- const fieldValues = {
1649
- ...(_stateFlags.mount
1650
- ? _formValues
1651
- : isUndefined(defaultValue)
1652
- ? _defaultValues
1653
- : isString(names)
1654
- ? { [names]: defaultValue }
1655
- : defaultValue),
1656
- };
1657
- return generateWatchOutput(names, _names, fieldValues, isGlobal);
1658
- };
1646
+ const _getWatch = (names, defaultValue, isGlobal) => generateWatchOutput(names, _names, {
1647
+ ...(_stateFlags.mount
1648
+ ? _formValues
1649
+ : isUndefined(defaultValue)
1650
+ ? _defaultValues
1651
+ : isString(names)
1652
+ ? { [names]: defaultValue }
1653
+ : defaultValue),
1654
+ }, isGlobal);
1659
1655
  const _getFieldArray = (name) => compact(get(_stateFlags.mount ? _formValues : _defaultValues, name, props.shouldUnregister ? get(_defaultValues, name, []) : []));
1660
1656
  const setFieldValue = (name, value, options = {}) => {
1661
1657
  const field = get(_fields, name);