react-hook-form 7.39.4 → 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 (46) 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 +44 -45
  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/utils/createSubject.d.ts +3 -3
  45. package/dist/utils/createSubject.d.ts.map +1 -1
  46. package/package.json +2 -2
@@ -239,26 +239,31 @@ 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
 
@@ -322,16 +327,12 @@ function useWatch(props) {
322
327
  useSubscribe({
323
328
  disabled,
324
329
  subject: control._subjects.watch,
325
- callback: React.useCallback((formState) => {
330
+ callback: (formState) => {
326
331
  if (shouldSubscribeByName(_name.current, formState.name, exact)) {
327
332
  const fieldValues = generateWatchOutput(_name.current, control._names, formState.values || control._formValues);
328
- updateValue(isUndefined(_name.current) || !isUndefined(fieldValues)
329
- ? cloneObject(fieldValues)
330
- : isUndefined(fieldValues)
331
- ? defaultValue
332
- : fieldValues);
333
+ updateValue(isUndefined(fieldValues) ? defaultValue : cloneObject(fieldValues));
333
334
  }
334
- }, [control, exact, defaultValue]),
335
+ },
335
336
  });
336
337
  const [value, updateValue] = React.useState(isUndefined(defaultValue)
337
338
  ? control._getWatch(name)
@@ -1315,7 +1316,7 @@ var getResolverOptions = (fieldsNames, _fields, criteriaMode, shouldUseNativeVal
1315
1316
  };
1316
1317
 
1317
1318
  var getRuleValue = (rule) => isUndefined(rule)
1318
- ? undefined
1319
+ ? rule
1319
1320
  : isRegex(rule)
1320
1321
  ? rule.source
1321
1322
  : isObject(rule)
@@ -1463,8 +1464,7 @@ function createFormControl(props = {}) {
1463
1464
  const fieldValues = method(get(_fields, name), args.argA, args.argB);
1464
1465
  shouldSetValues && set(_fields, name, fieldValues);
1465
1466
  }
1466
- if (_proxyFormState.errors &&
1467
- shouldUpdateFieldsAndState &&
1467
+ if (shouldUpdateFieldsAndState &&
1468
1468
  Array.isArray(get(_formState.errors, name))) {
1469
1469
  const errors = method(get(_formState.errors, name), args.argA, args.argB);
1470
1470
  shouldSetValues && set(_formState.errors, name, errors);
@@ -1509,37 +1509,39 @@ function createFormControl(props = {}) {
1509
1509
  }
1510
1510
  };
1511
1511
  const updateTouchAndDirty = (name, fieldValue, isBlurEvent, shouldDirty, shouldRender) => {
1512
- let isFieldDirty = false;
1512
+ let shouldUpdateField = false;
1513
+ let isPreviousDirty = false;
1513
1514
  const output = {
1514
1515
  name,
1515
1516
  };
1516
- const isPreviousFieldTouched = get(_formState.touchedFields, name);
1517
1517
  if (_proxyFormState.isDirty) {
1518
- const isPreviousFormDirty = _formState.isDirty;
1518
+ isPreviousDirty = _formState.isDirty;
1519
1519
  _formState.isDirty = output.isDirty = _getDirty();
1520
- isFieldDirty = isPreviousFormDirty !== output.isDirty;
1520
+ shouldUpdateField = isPreviousDirty !== output.isDirty;
1521
1521
  }
1522
1522
  if (_proxyFormState.dirtyFields && (!isBlurEvent || shouldDirty)) {
1523
- const isPreviousFieldDirty = get(_formState.dirtyFields, name);
1523
+ isPreviousDirty = get(_formState.dirtyFields, name);
1524
1524
  const isCurrentFieldPristine = deepEqual(get(_defaultValues, name), fieldValue);
1525
1525
  isCurrentFieldPristine
1526
1526
  ? unset(_formState.dirtyFields, name)
1527
1527
  : set(_formState.dirtyFields, name, true);
1528
1528
  output.dirtyFields = _formState.dirtyFields;
1529
- isFieldDirty =
1530
- isFieldDirty ||
1531
- isPreviousFieldDirty !== get(_formState.dirtyFields, name);
1529
+ shouldUpdateField =
1530
+ shouldUpdateField || isPreviousDirty !== !isCurrentFieldPristine;
1532
1531
  }
1533
- if (isBlurEvent && !isPreviousFieldTouched) {
1534
- set(_formState.touchedFields, name, isBlurEvent);
1535
- output.touchedFields = _formState.touchedFields;
1536
- isFieldDirty =
1537
- isFieldDirty ||
1538
- (_proxyFormState.touchedFields &&
1539
- 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
+ }
1540
1542
  }
1541
- isFieldDirty && shouldRender && _subjects.state.next(output);
1542
- return isFieldDirty ? output : {};
1543
+ shouldUpdateField && shouldRender && _subjects.state.next(output);
1544
+ return shouldUpdateField ? output : {};
1543
1545
  };
1544
1546
  const shouldRenderByError = (name, isValid, error, fieldState) => {
1545
1547
  const previousFieldError = get(_formState.errors, name);
@@ -1641,18 +1643,15 @@ function createFormControl(props = {}) {
1641
1643
  };
1642
1644
  const _getDirty = (name, data) => (name && data && set(_formValues, name, data),
1643
1645
  !deepEqual(getValues(), _defaultValues));
1644
- const _getWatch = (names, defaultValue, isGlobal) => {
1645
- const fieldValues = {
1646
- ...(_stateFlags.mount
1647
- ? _formValues
1648
- : isUndefined(defaultValue)
1649
- ? _defaultValues
1650
- : isString(names)
1651
- ? { [names]: defaultValue }
1652
- : defaultValue),
1653
- };
1654
- return generateWatchOutput(names, _names, fieldValues, isGlobal);
1655
- };
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);
1656
1655
  const _getFieldArray = (name) => compact(get(_stateFlags.mount ? _formValues : _defaultValues, name, props.shouldUnregister ? get(_defaultValues, name, []) : []));
1657
1656
  const setFieldValue = (name, value, options = {}) => {
1658
1657
  const field = get(_fields, name);