react-hook-form 7.15.0 → 7.15.3

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.
package/dist/index.esm.js CHANGED
@@ -132,38 +132,10 @@ function useFormState(props) {
132
132
  return getProxyFormState(formState, control._proxyFormState, _localProxyFormState.current, false);
133
133
  }
134
134
 
135
- var isKey = (value) => /^\w*$/.test(value);
136
-
137
- var stringToPath = (input) => compact(input.replace(/["|']|\]/g, '').split(/\.|\[/));
138
-
139
- function set(object, path, value) {
140
- let index = -1;
141
- const tempPath = isKey(path) ? [path] : stringToPath(path);
142
- const length = tempPath.length;
143
- const lastIndex = length - 1;
144
- while (++index < length) {
145
- const key = tempPath[index];
146
- let newValue = value;
147
- if (index !== lastIndex) {
148
- const objValue = object[key];
149
- newValue =
150
- isObject(objValue) || Array.isArray(objValue)
151
- ? objValue
152
- : !isNaN(+tempPath[index + 1])
153
- ? []
154
- : {};
155
- }
156
- object[key] = newValue;
157
- object = object[key];
158
- }
159
- return object;
160
- }
161
-
162
135
  function useController(props) {
163
136
  const methods = useFormContext();
164
137
  const { name, control = methods.control, shouldUnregister } = props;
165
138
  const [value, setInputStateValue] = React.useState(get(control._formValues, name, get(control._defaultValues, name, props.defaultValue)));
166
- set(control._formValues, name, value);
167
139
  const formState = useFormState({
168
140
  control: control || methods.control,
169
141
  name,
@@ -210,6 +182,7 @@ function useController(props) {
210
182
  onBlur: () => {
211
183
  registerProps.onBlur({
212
184
  target: {
185
+ value,
213
186
  name: name,
214
187
  },
215
188
  type: EVENTS.BLUR,
@@ -243,6 +216,33 @@ const Controller = (props) => props.render(useController(props));
243
216
  var appendErrors = (name, validateAllFieldCriteria, errors, type, message) => validateAllFieldCriteria
244
217
  ? Object.assign(Object.assign({}, errors[name]), { types: Object.assign(Object.assign({}, (errors[name] && errors[name].types ? errors[name].types : {})), { [type]: message || true }) }) : {};
245
218
 
219
+ var isKey = (value) => /^\w*$/.test(value);
220
+
221
+ var stringToPath = (input) => compact(input.replace(/["|']|\]/g, '').split(/\.|\[/));
222
+
223
+ function set(object, path, value) {
224
+ let index = -1;
225
+ const tempPath = isKey(path) ? [path] : stringToPath(path);
226
+ const length = tempPath.length;
227
+ const lastIndex = length - 1;
228
+ while (++index < length) {
229
+ const key = tempPath[index];
230
+ let newValue = value;
231
+ if (index !== lastIndex) {
232
+ const objValue = object[key];
233
+ newValue =
234
+ isObject(objValue) || Array.isArray(objValue)
235
+ ? objValue
236
+ : !isNaN(+tempPath[index + 1])
237
+ ? []
238
+ : {};
239
+ }
240
+ object[key] = newValue;
241
+ object = object[key];
242
+ }
243
+ return object;
244
+ }
245
+
246
246
  const focusFieldBy = (fields, callback, fieldsNames) => {
247
247
  for (const key of fieldsNames || Object.keys(fields)) {
248
248
  const field = get(fields, key);
@@ -1174,9 +1174,7 @@ function createFormControl(props = {}) {
1174
1174
  !field._f.deps) ||
1175
1175
  skipValidation(isBlurEvent, get(_formState.touchedFields, name), _formState.isSubmitted, reValidateMode, validationMode);
1176
1176
  const isWatched = !isBlurEvent && isFieldWatched(name);
1177
- if (!isUndefined(inputValue)) {
1178
- set(_formValues, name, inputValue);
1179
- }
1177
+ set(_formValues, name, inputValue);
1180
1178
  const fieldState = updateTouchAndDirtyState(name, inputValue, isBlurEvent, false);
1181
1179
  const shouldRender = !isEmptyObject(fieldState) || isWatched;
1182
1180
  !isBlurEvent &&
@@ -1199,9 +1197,11 @@ function createFormControl(props = {}) {
1199
1197
  error = get(errors, name);
1200
1198
  if (isCheckBoxInput(target) && !error) {
1201
1199
  const parentNodeName = getNodeParentName(name);
1202
- const valError = get(errors, parentNodeName, {});
1203
- valError.type && valError.message && (error = valError);
1204
- if (valError || get(_formState.errors, parentNodeName)) {
1200
+ const parentField = get(_fields, parentNodeName);
1201
+ if (Array.isArray(parentField) &&
1202
+ parentField.every((field) => field._f && isCheckBoxInput(field._f.ref))) {
1203
+ const parentError = get(errors, parentNodeName, {});
1204
+ parentError.type && (error = parentError);
1205
1205
  name = parentNodeName;
1206
1206
  }
1207
1207
  }
@@ -1264,8 +1264,8 @@ function createFormControl(props = {}) {
1264
1264
  ? setValues(fieldName, fieldValue, options)
1265
1265
  : setFieldValue(fieldName, fieldValue, options, true);
1266
1266
  });
1267
- const _getWatch = (fieldNames, defaultValue, isGlobal) => {
1268
- const fieldValues = Object.assign({}, (_isMounted
1267
+ const _getWatch = (fieldNames, defaultValue, isMounted, isGlobal) => {
1268
+ const fieldValues = Object.assign({}, (isMounted || _isMounted
1269
1269
  ? _formValues
1270
1270
  : isUndefined(defaultValue)
1271
1271
  ? _defaultValues
@@ -1393,7 +1393,7 @@ function createFormControl(props = {}) {
1393
1393
  return isValid;
1394
1394
  };
1395
1395
  const getValues = (fieldNames) => {
1396
- const values = Object.assign(Object.assign({}, _defaultValues), _formValues);
1396
+ const values = Object.assign(Object.assign({}, _defaultValues), (_isMounted ? _formValues : {}));
1397
1397
  return isUndefined(fieldNames)
1398
1398
  ? values
1399
1399
  : isString(fieldNames)
@@ -1422,7 +1422,7 @@ function createFormControl(props = {}) {
1422
1422
  ? _subjects.watch.subscribe({
1423
1423
  next: (info) => fieldName(_getWatch(undefined, defaultValue), info),
1424
1424
  })
1425
- : _getWatch(fieldName, defaultValue, true);
1425
+ : _getWatch(fieldName, defaultValue, false, true);
1426
1426
  const unregister = (name, options = {}) => {
1427
1427
  for (const inputName of name ? convertToArrayPayload(name) : _names.mount) {
1428
1428
  _names.mount.delete(inputName);
@@ -1467,14 +1467,14 @@ function createFormControl(props = {}) {
1467
1467
  ], ref: { type: ref.type, name } }) : Object.assign(Object.assign({}, field._f), { ref }),
1468
1468
  };
1469
1469
  set(_fields, name, field);
1470
- _updateValidAndInputValue(name, ref);
1470
+ (!options || !options.disabled) && _updateValidAndInputValue(name, ref);
1471
1471
  };
1472
1472
  const register = (name, options = {}) => {
1473
1473
  const field = get(_fields, name);
1474
1474
  set(_fields, name, {
1475
1475
  _f: Object.assign(Object.assign(Object.assign({}, (field && field._f ? field._f : { ref: { name } })), { name, mount: true }), options),
1476
1476
  });
1477
- if (options.value) {
1477
+ if (!isUndefined(options.value)) {
1478
1478
  set(_formValues, name, options.value);
1479
1479
  }
1480
1480
  if (isBoolean(options.disabled) && field) {
@@ -1555,7 +1555,9 @@ function createFormControl(props = {}) {
1555
1555
  const reset = (formValues, keepStateOptions = {}) => {
1556
1556
  const updatedValues = formValues || _defaultValues;
1557
1557
  const values = cloneObject(updatedValues);
1558
- _formValues = values;
1558
+ if (!keepStateOptions.keepValues) {
1559
+ _formValues = props.shouldUnregister ? {} : values;
1560
+ }
1559
1561
  if (isWeb && !keepStateOptions.keepValues) {
1560
1562
  for (const name of _names.mount) {
1561
1563
  const field = get(_fields, name);
@@ -1618,7 +1620,7 @@ function createFormControl(props = {}) {
1618
1620
  isSubmitting: false,
1619
1621
  isSubmitSuccessful: false,
1620
1622
  });
1621
- _isMounted = !!keepStateOptions.keepIsValid;
1623
+ _isMounted = !_proxyFormState.isValid || !!keepStateOptions.keepIsValid;
1622
1624
  };
1623
1625
  const setFocus = (name) => get(_fields, name)._f.ref.focus();
1624
1626
  const _removeFields = () => {
@@ -1767,7 +1769,7 @@ function useWatch(props) {
1767
1769
  fieldName &&
1768
1770
  (fieldName.startsWith(name) ||
1769
1771
  name.startsWith(fieldName)))) {
1770
- const result = control._getWatch(_name.current, defaultValue);
1772
+ const result = control._getWatch(_name.current, defaultValue, true);
1771
1773
  updateValue(isObject(result)
1772
1774
  ? Object.assign({}, result) : Array.isArray(result)
1773
1775
  ? [...result]