react-hook-form 7.19.1 → 7.19.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.
package/dist/index.esm.js CHANGED
@@ -116,23 +116,29 @@ const tearDown = (_subscription) => {
116
116
  _subscription.current = undefined;
117
117
  }
118
118
  };
119
- const updateSubscriptionProps = ({ _subscription, props }) => {
120
- if (props.disabled) {
119
+ const updateSubscriptionProps = ({ _subscription, _props }) => {
120
+ if (_props.current.disabled) {
121
121
  tearDown(_subscription);
122
122
  }
123
123
  else if (!_subscription.current) {
124
- _subscription.current = props.subject.subscribe({
125
- next: props.callback,
124
+ _subscription.current = _props.current.subject.subscribe({
125
+ next: _props.current.callback,
126
126
  });
127
127
  }
128
128
  };
129
129
  function useSubscribe(props) {
130
130
  const _subscription = React.useRef();
131
+ const _props = React.useRef(props);
132
+ _props.current = props;
131
133
  updateSubscriptionProps({
132
134
  _subscription,
133
- props,
135
+ _props,
134
136
  });
135
137
  React.useEffect(() => {
138
+ updateSubscriptionProps({
139
+ _subscription,
140
+ _props,
141
+ });
136
142
  return () => tearDown(_subscription);
137
143
  }, []);
138
144
  }
@@ -177,33 +183,6 @@ function generateWatchOutput(names, _names, formValues, isGlobal) {
177
183
  return formValues;
178
184
  }
179
185
 
180
- var isKey = (value) => /^\w*$/.test(value);
181
-
182
- var stringToPath = (input) => compact(input.replace(/["|']|\]/g, '').split(/\.|\[/));
183
-
184
- function set(object, path, value) {
185
- let index = -1;
186
- const tempPath = isKey(path) ? [path] : stringToPath(path);
187
- const length = tempPath.length;
188
- const lastIndex = length - 1;
189
- while (++index < length) {
190
- const key = tempPath[index];
191
- let newValue = value;
192
- if (index !== lastIndex) {
193
- const objValue = object[key];
194
- newValue =
195
- isObject(objValue) || Array.isArray(objValue)
196
- ? objValue
197
- : !isNaN(+tempPath[index + 1])
198
- ? []
199
- : {};
200
- }
201
- object[key] = newValue;
202
- object = object[key];
203
- }
204
- return object;
205
- }
206
-
207
186
  function useWatch(props) {
208
187
  const methods = useFormContext();
209
188
  const { control = methods.control, name, defaultValue, disabled, } = props || {};
@@ -214,10 +193,8 @@ function useWatch(props) {
214
193
  subject: control._subjects.watch,
215
194
  callback: (formState) => {
216
195
  if (shouldSubscribeByName(_name.current, formState.name)) {
217
- const fieldValues = generateWatchOutput(_name.current, control._names, control._formValues);
218
- updateValue(isObject(fieldValues) &&
219
- !(isString(_name.current) &&
220
- get(control._fields, _name.current, {})._f)
196
+ const fieldValues = generateWatchOutput(_name.current, control._names, formState.values || control._formValues);
197
+ updateValue(isUndefined(_name.current)
221
198
  ? Object.assign({}, fieldValues) : Array.isArray(fieldValues)
222
199
  ? [...fieldValues]
223
200
  : fieldValues);
@@ -261,7 +238,7 @@ function useController(props) {
261
238
  if (isNameInFieldArray(control._names.array, name)
262
239
  ? _shouldUnregisterField && !control._stateFlags.action
263
240
  : _shouldUnregisterField) {
264
- control.unregister(name);
241
+ control.unregister(name, { keepDefaultValue: true });
265
242
  }
266
243
  else {
267
244
  updateMounted(name, false);
@@ -316,6 +293,33 @@ const Controller = (props) => props.render(useController(props));
316
293
  var appendErrors = (name, validateAllFieldCriteria, errors, type, message) => validateAllFieldCriteria
317
294
  ? Object.assign(Object.assign({}, errors[name]), { types: Object.assign(Object.assign({}, (errors[name] && errors[name].types ? errors[name].types : {})), { [type]: message || true }) }) : {};
318
295
 
296
+ var isKey = (value) => /^\w*$/.test(value);
297
+
298
+ var stringToPath = (input) => compact(input.replace(/["|']|\]/g, '').split(/\.|\[/));
299
+
300
+ function set(object, path, value) {
301
+ let index = -1;
302
+ const tempPath = isKey(path) ? [path] : stringToPath(path);
303
+ const length = tempPath.length;
304
+ const lastIndex = length - 1;
305
+ while (++index < length) {
306
+ const key = tempPath[index];
307
+ let newValue = value;
308
+ if (index !== lastIndex) {
309
+ const objValue = object[key];
310
+ newValue =
311
+ isObject(objValue) || Array.isArray(objValue)
312
+ ? objValue
313
+ : !isNaN(+tempPath[index + 1])
314
+ ? []
315
+ : {};
316
+ }
317
+ object[key] = newValue;
318
+ object = object[key];
319
+ }
320
+ return object;
321
+ }
322
+
319
323
  const focusFieldBy = (fields, callback, fieldsNames) => {
320
324
  for (const key of fieldsNames || Object.keys(fields)) {
321
325
  const field = get(fields, key);
@@ -433,7 +437,6 @@ const useFieldArray = (props) => {
433
437
  const updatedFieldArrayValues = omitKeys(updatedFieldArrayValuesWithKey, keyName);
434
438
  _actioned.current = true;
435
439
  set(control._formValues, name, updatedFieldArrayValues);
436
- setFields(updatedFieldArrayValuesWithKey);
437
440
  return updatedFieldArrayValues;
438
441
  }, [control, name, keyName]);
439
442
  const append$1 = (value, options) => {
@@ -442,6 +445,7 @@ const useFieldArray = (props) => {
442
445
  control._updateFieldArray(name, append, {
443
446
  argA: fillEmptyArray(value),
444
447
  }, updateValues(updatedFieldArrayValuesWithKey));
448
+ setFields(updatedFieldArrayValuesWithKey);
445
449
  control._names.focus = getFocusFieldName(name, updatedFieldArrayValuesWithKey.length - appendValue.length, options);
446
450
  };
447
451
  const prepend$1 = (value, options) => {
@@ -449,6 +453,7 @@ const useFieldArray = (props) => {
449
453
  control._updateFieldArray(name, prepend, {
450
454
  argA: fillEmptyArray(value),
451
455
  }, updateValues(updatedFieldArrayValuesWithKey));
456
+ setFields(updatedFieldArrayValuesWithKey);
452
457
  control._names.focus = getFocusFieldName(name, 0, options);
453
458
  };
454
459
  const remove = (index) => {
@@ -456,6 +461,7 @@ const useFieldArray = (props) => {
456
461
  control._updateFieldArray(name, removeArrayAt, {
457
462
  argA: index,
458
463
  }, updateValues(updatedFieldArrayValuesWithKey));
464
+ setFields(updatedFieldArrayValuesWithKey);
459
465
  };
460
466
  const insert$1 = (index, value, options) => {
461
467
  const updatedFieldArrayValuesWithKey = insert(mapCurrentIds(control._getFieldArray(name), _fieldIds, keyName), index, mapIds(convertToArrayPayload(value), keyName));
@@ -463,6 +469,7 @@ const useFieldArray = (props) => {
463
469
  argA: index,
464
470
  argB: fillEmptyArray(value),
465
471
  }, updateValues(updatedFieldArrayValuesWithKey));
472
+ setFields(updatedFieldArrayValuesWithKey);
466
473
  control._names.focus = getFocusFieldName(name, index, options);
467
474
  };
468
475
  const swap = (indexA, indexB) => {
@@ -472,6 +479,7 @@ const useFieldArray = (props) => {
472
479
  argA: indexA,
473
480
  argB: indexB,
474
481
  }, updateValues(updatedFieldArrayValuesWithKey), false);
482
+ setFields(updatedFieldArrayValuesWithKey);
475
483
  };
476
484
  const move = (from, to) => {
477
485
  const updatedFieldArrayValuesWithKey = mapCurrentIds(control._getFieldArray(name), _fieldIds, keyName);
@@ -480,6 +488,7 @@ const useFieldArray = (props) => {
480
488
  argA: from,
481
489
  argB: to,
482
490
  }, updateValues(updatedFieldArrayValuesWithKey), false);
491
+ setFields(updatedFieldArrayValuesWithKey);
483
492
  };
484
493
  const update = (index, value) => {
485
494
  const updatedFieldArrayValuesWithKey = mapCurrentIds(control._getFieldArray(name), _fieldIds, keyName);
@@ -489,10 +498,12 @@ const useFieldArray = (props) => {
489
498
  argA: index,
490
499
  argB: value,
491
500
  }, updateValues(_fieldIds.current), true, false);
501
+ setFields(_fieldIds.current);
492
502
  };
493
503
  const replace = (value) => {
494
504
  const updatedFieldArrayValuesWithKey = mapIds(convertToArrayPayload(value), keyName);
495
505
  control._updateFieldArray(name, () => updatedFieldArrayValuesWithKey, {}, updateValues(updatedFieldArrayValuesWithKey), true, false);
506
+ setFields(updatedFieldArrayValuesWithKey);
496
507
  };
497
508
  React.useEffect(() => {
498
509
  control._stateFlags.action = false;
@@ -1585,7 +1596,9 @@ function createFormControl(props = {}) {
1585
1596
  e.persist && e.persist();
1586
1597
  }
1587
1598
  let hasNoPromiseError = true;
1588
- let fieldValues = Object.assign({}, _formValues);
1599
+ let fieldValues = _options.shouldUnregister
1600
+ ? cloneObject(_formValues)
1601
+ : Object.assign({}, _formValues);
1589
1602
  _subjects.state.next({
1590
1603
  isSubmitting: true,
1591
1604
  });
@@ -1843,7 +1856,6 @@ function useForm(props = {}) {
1843
1856
  }
1844
1857
  control._removeUnmounted();
1845
1858
  });
1846
- React.useEffect(() => () => Object.values(control._subjects).forEach((subject) => subject.unsubscribe()), [control]);
1847
1859
  _formControl.current.formState = getProxyFormState(formState, control._proxyFormState);
1848
1860
  return _formControl.current;
1849
1861
  }