react-hook-form 7.51.3 → 7.51.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.
@@ -78,6 +78,36 @@ var get = (object, path, defaultValue) => {
78
78
 
79
79
  var isBoolean = (value) => typeof value === 'boolean';
80
80
 
81
+ var isKey = (value) => /^\w*$/.test(value);
82
+
83
+ var stringToPath = (input) => compact(input.replace(/["|']|\]/g, '').split(/\.|\[/));
84
+
85
+ var set = (object, path, value) => {
86
+ let index = -1;
87
+ const tempPath = isKey(path) ? [path] : stringToPath(path);
88
+ const length = tempPath.length;
89
+ const lastIndex = length - 1;
90
+ while (++index < length) {
91
+ const key = tempPath[index];
92
+ let newValue = value;
93
+ if (index !== lastIndex) {
94
+ const objValue = object[key];
95
+ newValue =
96
+ isObject(objValue) || Array.isArray(objValue)
97
+ ? objValue
98
+ : !isNaN(+tempPath[index + 1])
99
+ ? []
100
+ : {};
101
+ }
102
+ if (key === '__proto__') {
103
+ return;
104
+ }
105
+ object[key] = newValue;
106
+ object = object[key];
107
+ }
108
+ return object;
109
+ };
110
+
81
111
  const EVENTS = {
82
112
  BLUR: 'blur',
83
113
  FOCUS_OUT: 'focusout',
@@ -340,33 +370,6 @@ function useWatch(props) {
340
370
  return value;
341
371
  }
342
372
 
343
- var isKey = (value) => /^\w*$/.test(value);
344
-
345
- var stringToPath = (input) => compact(input.replace(/["|']|\]/g, '').split(/\.|\[/));
346
-
347
- var set = (object, path, value) => {
348
- let index = -1;
349
- const tempPath = isKey(path) ? [path] : stringToPath(path);
350
- const length = tempPath.length;
351
- const lastIndex = length - 1;
352
- while (++index < length) {
353
- const key = tempPath[index];
354
- let newValue = value;
355
- if (index !== lastIndex) {
356
- const objValue = object[key];
357
- newValue =
358
- isObject(objValue) || Array.isArray(objValue)
359
- ? objValue
360
- : !isNaN(+tempPath[index + 1])
361
- ? []
362
- : {};
363
- }
364
- object[key] = newValue;
365
- object = object[key];
366
- }
367
- return object;
368
- };
369
-
370
373
  /**
371
374
  * Custom hook to work with controlled component, this function provide you with both form and field level state. Re-render is isolated at the hook level.
372
375
  *
@@ -2025,8 +2028,7 @@ function createFormControl(props = {}) {
2025
2028
  };
2026
2029
  const getValues = (fieldNames) => {
2027
2030
  const values = {
2028
- ..._defaultValues,
2029
- ...(_state.mount ? _formValues : {}),
2031
+ ...(_state.mount ? _formValues : _defaultValues),
2030
2032
  };
2031
2033
  return isUndefined(fieldNames)
2032
2034
  ? values
@@ -2050,7 +2052,11 @@ function createFormControl(props = {}) {
2050
2052
  };
2051
2053
  const setError = (name, error, options) => {
2052
2054
  const ref = (get(_fields, name, { _f: {} })._f || {}).ref;
2055
+ const currentError = get(_formState.errors, name) || {};
2056
+ // Don't override existing error messages elsewhere in the object tree.
2057
+ const { ref: currentRef, message, type, ...restOfErrorTree } = currentError;
2053
2058
  set(_formState.errors, name, {
2059
+ ...restOfErrorTree,
2054
2060
  ...error,
2055
2061
  ref,
2056
2062
  });
@@ -2093,7 +2099,7 @@ function createFormControl(props = {}) {
2093
2099
  !options.keepIsValid && _updateValid();
2094
2100
  };
2095
2101
  const _updateDisabledField = ({ disabled, name, field, fields, value, }) => {
2096
- if (isBoolean(disabled)) {
2102
+ if ((isBoolean(disabled) && _state.mount) || !!disabled) {
2097
2103
  const inputValue = disabled
2098
2104
  ? undefined
2099
2105
  : isUndefined(value)
@@ -2193,12 +2199,15 @@ function createFormControl(props = {}) {
2193
2199
  if (isBoolean(disabled)) {
2194
2200
  _subjects.state.next({ disabled });
2195
2201
  iterateFieldsByAction(_fields, (ref, name) => {
2196
- let requiredDisabledState = disabled;
2197
2202
  const currentField = get(_fields, name);
2198
- if (currentField && isBoolean(currentField._f.disabled)) {
2199
- requiredDisabledState || (requiredDisabledState = currentField._f.disabled);
2203
+ if (currentField) {
2204
+ ref.disabled = currentField._f.disabled || disabled;
2205
+ if (Array.isArray(currentField._f.refs)) {
2206
+ currentField._f.refs.forEach((inputRef) => {
2207
+ inputRef.disabled = currentField._f.disabled || disabled;
2208
+ });
2209
+ }
2200
2210
  }
2201
- ref.disabled = requiredDisabledState;
2202
2211
  }, 0, false);
2203
2212
  }
2204
2213
  };