react-hook-form 7.72.1 → 7.73.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.
@@ -64,7 +64,10 @@ var get = (object, path, defaultValue) => {
64
64
  if (!path || !isObject(object)) {
65
65
  return defaultValue;
66
66
  }
67
- const result = (isKey(path) ? [path] : stringToPath(path)).reduce((result, key) => isNullOrUndefined(result) ? result : result[key], object);
67
+ const paths = isKey(path) ? [path] : stringToPath(path);
68
+ const result = paths.reduce((result, key) => {
69
+ return isNullOrUndefined(result) ? undefined : result[key];
70
+ }, object);
68
71
  return isUndefined(result) || result === object
69
72
  ? isUndefined(object[path])
70
73
  ? defaultValue
@@ -239,7 +242,10 @@ var generateWatchOutput = (names, _names, formValues, isGlobal, defaultValue) =>
239
242
 
240
243
  var isPrimitive = (value) => isNullOrUndefined(value) || !isObjectType(value);
241
244
 
242
- function deepEqual(object1, object2, _internal_visited = new WeakSet()) {
245
+ function deepEqual(object1, object2, visited = new WeakSet()) {
246
+ if (object1 === object2) {
247
+ return true;
248
+ }
243
249
  if (isPrimitive(object1) || isPrimitive(object2)) {
244
250
  return Object.is(object1, object2);
245
251
  }
@@ -251,14 +257,14 @@ function deepEqual(object1, object2, _internal_visited = new WeakSet()) {
251
257
  if (keys1.length !== keys2.length) {
252
258
  return false;
253
259
  }
254
- if (_internal_visited.has(object1) || _internal_visited.has(object2)) {
260
+ if (visited.has(object1) || visited.has(object2)) {
255
261
  return true;
256
262
  }
257
- _internal_visited.add(object1);
258
- _internal_visited.add(object2);
263
+ visited.add(object1);
264
+ visited.add(object2);
259
265
  for (const key of keys1) {
260
266
  const val1 = object1[key];
261
- if (!keys2.includes(key)) {
267
+ if (!(key in object2)) {
262
268
  return false;
263
269
  }
264
270
  if (key !== 'ref') {
@@ -266,7 +272,7 @@ function deepEqual(object1, object2, _internal_visited = new WeakSet()) {
266
272
  if ((isDateObject(val1) && isDateObject(val2)) ||
267
273
  ((isObject(val1) || Array.isArray(val1)) &&
268
274
  (isObject(val2) || Array.isArray(val2)))
269
- ? !deepEqual(val1, val2, _internal_visited)
275
+ ? !deepEqual(val1, val2, visited)
270
276
  : !Object.is(val1, val2)) {
271
277
  return false;
272
278
  }
@@ -706,7 +712,7 @@ function Form(props) {
706
712
  const methods = useFormContext();
707
713
  const [mounted, setMounted] = React.useState(false);
708
714
  const { control = methods.control, onSubmit, children, action, method = POST_REQUEST, headers, encType, onError, render, onSuccess, validateStatus, ...rest } = props;
709
- const submit = async (event) => {
715
+ const submit = React.useCallback(async (event) => {
710
716
  let hasError = false;
711
717
  let type = '';
712
718
  await control.handleSubmit(async (data) => {
@@ -716,7 +722,7 @@ function Form(props) {
716
722
  formDataJson = JSON.stringify(data);
717
723
  }
718
724
  catch (_a) { }
719
- const flattenFormValues = flatten(control._formValues);
725
+ const flattenFormValues = flatten(data);
720
726
  for (const key in flattenFormValues) {
721
727
  formData.append(key, flattenFormValues[key]);
722
728
  }
@@ -763,15 +769,25 @@ function Form(props) {
763
769
  }
764
770
  }
765
771
  })(event);
766
- if (hasError && props.control) {
767
- props.control._subjects.state.next({
772
+ if (hasError && control) {
773
+ control._subjects.state.next({
768
774
  isSubmitSuccessful: false,
769
775
  });
770
- props.control.setError('root.server', {
776
+ control.setError('root.server', {
771
777
  type,
772
778
  });
773
779
  }
774
- };
780
+ }, [
781
+ control,
782
+ onSubmit,
783
+ method,
784
+ action,
785
+ headers,
786
+ encType,
787
+ validateStatus,
788
+ onError,
789
+ onSuccess,
790
+ ]);
775
791
  React.useEffect(() => {
776
792
  setMounted(true);
777
793
  }, []);
@@ -880,6 +896,10 @@ function isEmptyArray(obj) {
880
896
  return true;
881
897
  }
882
898
  function unset(object, path) {
899
+ if (isString(path) && Object.prototype.hasOwnProperty.call(object, path)) {
900
+ delete object[path];
901
+ return object;
902
+ }
883
903
  const paths = Array.isArray(path)
884
904
  ? path
885
905
  : isKey(path)
@@ -1841,17 +1861,15 @@ function createFormControl(props = {}) {
1841
1861
  const field = get(_fields, name);
1842
1862
  const isFieldArray = _names.array.has(name);
1843
1863
  const cloneValue = cloneObject(value);
1864
+ const previousValue = get(_formValues, name);
1865
+ const isValueUnchanged = deepEqual(previousValue, cloneValue);
1844
1866
  set(_formValues, name, cloneValue);
1845
1867
  if (isFieldArray) {
1846
1868
  _subjects.array.next({
1847
1869
  name,
1848
1870
  values: cloneObject(_formValues),
1849
1871
  });
1850
- if ((_proxyFormState.isDirty ||
1851
- _proxyFormState.dirtyFields ||
1852
- _proxySubscribeFormState.isDirty ||
1853
- _proxySubscribeFormState.dirtyFields) &&
1854
- options.shouldDirty) {
1872
+ if (options.shouldDirty) {
1855
1873
  _updateDirtyFields(name);
1856
1874
  _subjects.state.next({
1857
1875
  name,
@@ -1861,22 +1879,29 @@ function createFormControl(props = {}) {
1861
1879
  }
1862
1880
  }
1863
1881
  else {
1864
- field && !field._f && !isNullOrUndefined(cloneValue)
1865
- ? setValues(name, cloneValue, options)
1866
- : setFieldValue(name, cloneValue, options);
1867
- }
1868
- if (isWatched(name, _names)) {
1869
- _subjects.state.next({
1870
- ..._formState,
1871
- name,
1872
- values: cloneObject(_formValues),
1873
- });
1882
+ const isEmpty = (Array.isArray(cloneValue) && !cloneValue.length) ||
1883
+ isEmptyObject(cloneValue);
1884
+ if (!field || field._f || isNullOrUndefined(cloneValue) || isEmpty) {
1885
+ setFieldValue(name, cloneValue, options);
1886
+ }
1887
+ else {
1888
+ setValues(name, cloneValue, options);
1889
+ }
1874
1890
  }
1875
- else {
1876
- _subjects.state.next({
1877
- name: _state.mount ? name : undefined,
1878
- values: cloneObject(_formValues),
1879
- });
1891
+ if (!isValueUnchanged) {
1892
+ if (isWatched(name, _names)) {
1893
+ _subjects.state.next({
1894
+ ..._formState,
1895
+ name,
1896
+ values: cloneObject(_formValues),
1897
+ });
1898
+ }
1899
+ else {
1900
+ _subjects.state.next({
1901
+ name: _state.mount ? name : undefined,
1902
+ values: cloneObject(_formValues),
1903
+ });
1904
+ }
1880
1905
  }
1881
1906
  };
1882
1907
  const onChange = async (event) => {
@@ -2095,15 +2120,16 @@ function createFormControl(props = {}) {
2095
2120
  const watch = (name, defaultValue) => isFunction(name)
2096
2121
  ? _subjects.state.subscribe({
2097
2122
  next: (payload) => 'values' in payload &&
2098
- name(_getWatch(undefined, defaultValue), payload),
2123
+ name(payload.values || _getWatch(undefined, defaultValue), payload),
2099
2124
  })
2100
2125
  : _getWatch(name, defaultValue, true);
2101
2126
  const _subscribe = (props) => _subjects.state.subscribe({
2102
2127
  next: (formState) => {
2103
2128
  if (shouldSubscribeByName(props.name, formState.name, props.exact) &&
2104
2129
  shouldRenderFormState(formState, props.formState || _proxyFormState, _setFormState, props.reRenderRoot)) {
2130
+ const snapshot = { ..._formValues };
2105
2131
  props.callback({
2106
- values: { ..._formValues },
2132
+ values: snapshot,
2107
2133
  ..._formState,
2108
2134
  ...formState,
2109
2135
  defaultValues: _defaultValues,
@@ -2165,7 +2191,7 @@ function createFormControl(props = {}) {
2165
2191
  const register = (name, options = {}) => {
2166
2192
  let field = get(_fields, name);
2167
2193
  const disabledIsDefined = isBoolean(options.disabled) || isBoolean(_options.disabled);
2168
- const shouldRevalidateRemount = !_names.registerName.has(name) && field && !field._f.mount;
2194
+ const shouldRevalidateRemount = !_names.registerName.has(name) && field && field._f && !field._f.mount;
2169
2195
  set(_fields, name, {
2170
2196
  ...(field || {}),
2171
2197
  _f: {
@@ -2812,10 +2838,11 @@ function useFieldArray(props) {
2812
2838
  control._subjects.state.next({
2813
2839
  ...control._formState,
2814
2840
  });
2841
+ const validationModes = getValidationModes(control._options.mode);
2815
2842
  if (_actioned.current &&
2816
- (!getValidationModes(control._options.mode).isOnSubmit ||
2817
- control._formState.isSubmitted) &&
2818
- !getValidationModes(control._options.reValidateMode).isOnSubmit) {
2843
+ (!validationModes.isOnSubmit || control._formState.isSubmitted) &&
2844
+ !getValidationModes(control._options.reValidateMode).isOnSubmit &&
2845
+ !validationModes.isOnBlur) {
2819
2846
  if (control._options.resolver) {
2820
2847
  control._runSchema([name]).then((result) => {
2821
2848
  control._updateIsValidating([name]);