react-hook-form 7.40.0-next.0 → 7.40.0-next.1

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.
@@ -122,10 +122,7 @@ const useFormContext = () => React.useContext(HookFormContext);
122
122
  * }
123
123
  * ```
124
124
  */
125
- const FormProvider = (props) => {
126
- const { children, ...data } = props;
127
- return (React.createElement(HookFormContext.Provider, { value: data }, children));
128
- };
125
+ const FormProvider = ({ children, ...data }) => (React.createElement(HookFormContext.Provider, { value: React.useState(data)[0] }, children));
129
126
 
130
127
  var getProxyFormState = (formState, control, localProxyFormState, isRoot = true) => {
131
128
  const result = {
@@ -256,16 +253,14 @@ function useFormState(props) {
256
253
  var isString = (value) => typeof value === 'string';
257
254
 
258
255
  var generateWatchOutput = (names, _names, formValues, isGlobal) => {
259
- const isArray = Array.isArray(names);
260
256
  if (isString(names)) {
261
257
  isGlobal && _names.watch.add(names);
262
258
  return get(formValues, names);
263
259
  }
264
- if (isArray) {
265
- return names.map((fieldName) => (isGlobal && _names.watch.add(fieldName),
266
- get(formValues, fieldName)));
260
+ if (Array.isArray(names)) {
261
+ return names.map((fieldName) => (isGlobal && _names.watch.add(fieldName), get(formValues, fieldName)));
267
262
  }
268
- isGlobal && (_names.watchAll = true);
263
+ _names.watchAll = !!isGlobal;
269
264
  return formValues;
270
265
  };
271
266
 
@@ -1321,7 +1316,7 @@ var getResolverOptions = (fieldsNames, _fields, criteriaMode, shouldUseNativeVal
1321
1316
  };
1322
1317
 
1323
1318
  var getRuleValue = (rule) => isUndefined(rule)
1324
- ? undefined
1319
+ ? rule
1325
1320
  : isRegex(rule)
1326
1321
  ? rule.source
1327
1322
  : isObject(rule)
@@ -1396,6 +1391,7 @@ function createFormControl(props = {}) {
1396
1391
  ...defaultOptions,
1397
1392
  ...props,
1398
1393
  };
1394
+ const shouldCaptureDirtyFields = props.resetOptions && props.resetOptions.keepDirtyValues;
1399
1395
  let _formState = {
1400
1396
  submitCount: 0,
1401
1397
  isDirty: false,
@@ -1450,11 +1446,10 @@ function createFormControl(props = {}) {
1450
1446
  timer = window.setTimeout(callback, wait);
1451
1447
  };
1452
1448
  const _updateValid = async () => {
1453
- let isValid = false;
1454
1449
  if (_proxyFormState.isValid) {
1455
- isValid = _options.resolver
1450
+ const isValid = _options.resolver
1456
1451
  ? isEmptyObject((await _executeSchema()).errors)
1457
- : await executeBuiltInValidation(_fields, true);
1452
+ : (await executeBuiltInValidation(_fields, true)).valid;
1458
1453
  if (isValid !== _formState.isValid) {
1459
1454
  _formState.isValid = isValid;
1460
1455
  _subjects.state.next({
@@ -1462,7 +1457,6 @@ function createFormControl(props = {}) {
1462
1457
  });
1463
1458
  }
1464
1459
  }
1465
- return isValid;
1466
1460
  };
1467
1461
  const _updateFieldArray = (name, values = [], method, args, shouldSetValues = true, shouldUpdateFieldsAndState = true) => {
1468
1462
  if (args && method) {
@@ -1471,8 +1465,7 @@ function createFormControl(props = {}) {
1471
1465
  const fieldValues = method(get(_fields, name), args.argA, args.argB);
1472
1466
  shouldSetValues && set(_fields, name, fieldValues);
1473
1467
  }
1474
- if (_proxyFormState.errors &&
1475
- shouldUpdateFieldsAndState &&
1468
+ if (shouldUpdateFieldsAndState &&
1476
1469
  Array.isArray(get(_formState.errors, name))) {
1477
1470
  const errors = method(get(_formState.errors, name), args.argA, args.argB);
1478
1471
  shouldSetValues && set(_formState.errors, name, errors);
@@ -1527,15 +1520,17 @@ function createFormControl(props = {}) {
1527
1520
  _formState.isDirty = output.isDirty = _getDirty();
1528
1521
  shouldUpdateField = isPreviousDirty !== output.isDirty;
1529
1522
  }
1530
- if (_proxyFormState.dirtyFields && (!isBlurEvent || shouldDirty)) {
1531
- isPreviousDirty = get(_formState.dirtyFields, name);
1523
+ if (!isBlurEvent || shouldDirty) {
1532
1524
  const isCurrentFieldPristine = deepEqual(get(_defaultValues, name), fieldValue);
1525
+ isPreviousDirty = get(_formState.dirtyFields, name);
1533
1526
  isCurrentFieldPristine
1534
1527
  ? unset(_formState.dirtyFields, name)
1535
1528
  : set(_formState.dirtyFields, name, true);
1536
1529
  output.dirtyFields = _formState.dirtyFields;
1537
1530
  shouldUpdateField =
1538
- shouldUpdateField || isPreviousDirty !== !isCurrentFieldPristine;
1531
+ shouldUpdateField ||
1532
+ (_proxyFormState.dirtyFields &&
1533
+ isPreviousDirty !== !isCurrentFieldPristine);
1539
1534
  }
1540
1535
  if (isBlurEvent) {
1541
1536
  const isPreviousFieldTouched = get(_formState.touchedFields, name);
@@ -1620,6 +1615,9 @@ function createFormControl(props = {}) {
1620
1615
  const isFieldArrayRoot = _names.array.has(_f.name);
1621
1616
  const fieldError = await validateField(field, get(_formValues, _f.name), shouldDisplayAllAssociatedErrors, _options.shouldUseNativeValidation, isFieldArrayRoot);
1622
1617
  if (fieldError[_f.name]) {
1618
+ if (_f.name === context.name) {
1619
+ context.error = fieldError[context.name];
1620
+ }
1623
1621
  context.valid = false;
1624
1622
  if (shouldOnlyCheckValid) {
1625
1623
  break;
@@ -1636,7 +1634,7 @@ function createFormControl(props = {}) {
1636
1634
  (await executeBuiltInValidation(fieldValue, shouldOnlyCheckValid, context));
1637
1635
  }
1638
1636
  }
1639
- return context.valid;
1637
+ return context;
1640
1638
  };
1641
1639
  const _removeUnmounted = () => {
1642
1640
  for (const name of _names.unMount) {
@@ -1651,18 +1649,15 @@ function createFormControl(props = {}) {
1651
1649
  };
1652
1650
  const _getDirty = (name, data) => (name && data && set(_formValues, name, data),
1653
1651
  !deepEqual(getValues(), _defaultValues));
1654
- const _getWatch = (names, defaultValue, isGlobal) => {
1655
- const fieldValues = {
1656
- ...(_stateFlags.mount
1657
- ? _formValues
1658
- : isUndefined(defaultValue)
1659
- ? _defaultValues
1660
- : isString(names)
1661
- ? { [names]: defaultValue }
1662
- : defaultValue),
1663
- };
1664
- return generateWatchOutput(names, _names, fieldValues, isGlobal);
1665
- };
1652
+ const _getWatch = (names, defaultValue, isGlobal) => generateWatchOutput(names, _names, {
1653
+ ...(_stateFlags.mount
1654
+ ? _formValues
1655
+ : isUndefined(defaultValue)
1656
+ ? _defaultValues
1657
+ : isString(names)
1658
+ ? { [names]: defaultValue }
1659
+ : defaultValue),
1660
+ }, isGlobal);
1666
1661
  const _getFieldArray = (name) => compact(get(_stateFlags.mount ? _formValues : _defaultValues, name, props.shouldUnregister ? get(_defaultValues, name, []) : []));
1667
1662
  const setFieldValue = (name, value, options = {}) => {
1668
1663
  const field = get(_fields, name);
@@ -1708,7 +1703,8 @@ function createFormControl(props = {}) {
1708
1703
  }
1709
1704
  (options.shouldDirty || options.shouldTouch) &&
1710
1705
  updateTouchAndDirty(name, fieldValue, options.shouldTouch, options.shouldDirty, true);
1711
- options.shouldValidate && trigger(name);
1706
+ options.shouldValidate &&
1707
+ trigger(name);
1712
1708
  };
1713
1709
  const setValues = (name, value, options) => {
1714
1710
  for (const fieldKey in value) {
@@ -1806,8 +1802,17 @@ function createFormControl(props = {}) {
1806
1802
  isValid = isEmptyObject(errors);
1807
1803
  }
1808
1804
  else {
1809
- error = (await validateField(field, get(_formValues, name), shouldDisplayAllAssociatedErrors, _options.shouldUseNativeValidation))[name];
1810
- _updateValid();
1805
+ if (_proxyFormState.isValid) {
1806
+ const buildInValidationResult = await executeBuiltInValidation(_fields, true, {
1807
+ name,
1808
+ valid: true,
1809
+ });
1810
+ error = buildInValidationResult.error || {};
1811
+ isValid = buildInValidationResult.valid;
1812
+ }
1813
+ if (!error || isEmptyObject(error)) {
1814
+ error = (await validateField(field, get(_formValues, name), shouldDisplayAllAssociatedErrors, _options.shouldUseNativeValidation))[name];
1815
+ }
1811
1816
  }
1812
1817
  field._f.deps &&
1813
1818
  trigger(field._f.deps);
@@ -1831,12 +1836,13 @@ function createFormControl(props = {}) {
1831
1836
  else if (name) {
1832
1837
  validationResult = (await Promise.all(fieldNames.map(async (fieldName) => {
1833
1838
  const field = get(_fields, fieldName);
1834
- return await executeBuiltInValidation(field && field._f ? { [fieldName]: field } : field);
1839
+ return (await executeBuiltInValidation(field && field._f ? { [fieldName]: field } : field)).valid;
1835
1840
  }))).every(Boolean);
1836
1841
  !(!validationResult && !_formState.isValid) && _updateValid();
1837
1842
  }
1838
1843
  else {
1839
- validationResult = isValid = await executeBuiltInValidation(_fields);
1844
+ validationResult = isValid = (await executeBuiltInValidation(_fields))
1845
+ .valid;
1840
1846
  }
1841
1847
  _subjects.state.next({
1842
1848
  ...(!isString(name) ||
@@ -2085,7 +2091,7 @@ function createFormControl(props = {}) {
2085
2091
  _defaultValues = updatedValues;
2086
2092
  }
2087
2093
  if (!keepStateOptions.keepValues) {
2088
- if (keepStateOptions.keepDirtyValues) {
2094
+ if (keepStateOptions.keepDirtyValues || shouldCaptureDirtyFields) {
2089
2095
  for (const fieldName of _names.mount) {
2090
2096
  get(_formState.dirtyFields, fieldName)
2091
2097
  ? set(values, fieldName, get(_formValues, fieldName))