react-hook-form 7.43.4 → 7.43.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.
@@ -22,6 +22,42 @@ var getNodeParentName = (name) => name.substring(0, name.search(/\.\d+(\.|$)/))
22
22
 
23
23
  var isNameInFieldArray = (names, name) => names.has(getNodeParentName(name));
24
24
 
25
+ var isPlainObject = (tempObject) => {
26
+ const prototypeCopy = tempObject.constructor && tempObject.constructor.prototype;
27
+ return (isObject(prototypeCopy) && prototypeCopy.hasOwnProperty('isPrototypeOf'));
28
+ };
29
+
30
+ var isWeb = typeof window !== 'undefined' &&
31
+ typeof window.HTMLElement !== 'undefined' &&
32
+ typeof document !== 'undefined';
33
+
34
+ function cloneObject(data) {
35
+ let copy;
36
+ const isArray = Array.isArray(data);
37
+ if (data instanceof Date) {
38
+ copy = new Date(data);
39
+ }
40
+ else if (data instanceof Set) {
41
+ copy = new Set(data);
42
+ }
43
+ else if (!(isWeb && (data instanceof Blob || data instanceof FileList)) &&
44
+ (isArray || isObject(data))) {
45
+ copy = isArray ? [] : {};
46
+ if (!Array.isArray(data) && !isPlainObject(data)) {
47
+ copy = data;
48
+ }
49
+ else {
50
+ for (const key in data) {
51
+ copy[key] = cloneObject(data[key]);
52
+ }
53
+ }
54
+ }
55
+ else {
56
+ return data;
57
+ }
58
+ return copy;
59
+ }
60
+
25
61
  var compact = (value) => Array.isArray(value) ? value.filter(Boolean) : [];
26
62
 
27
63
  var isUndefined = (val) => val === undefined;
@@ -173,6 +209,7 @@ function useSubscribe(props) {
173
209
  _props.current = props;
174
210
  React.useEffect(() => {
175
211
  const subscription = !props.disabled &&
212
+ _props.current.subject &&
176
213
  _props.current.subject.subscribe({
177
214
  next: _props.current.next,
178
215
  });
@@ -269,42 +306,6 @@ var generateWatchOutput = (names, _names, formValues, isGlobal, defaultValue) =>
269
306
  return formValues;
270
307
  };
271
308
 
272
- var isPlainObject = (tempObject) => {
273
- const prototypeCopy = tempObject.constructor && tempObject.constructor.prototype;
274
- return (isObject(prototypeCopy) && prototypeCopy.hasOwnProperty('isPrototypeOf'));
275
- };
276
-
277
- var isWeb = typeof window !== 'undefined' &&
278
- typeof window.HTMLElement !== 'undefined' &&
279
- typeof document !== 'undefined';
280
-
281
- function cloneObject(data) {
282
- let copy;
283
- const isArray = Array.isArray(data);
284
- if (data instanceof Date) {
285
- copy = new Date(data);
286
- }
287
- else if (data instanceof Set) {
288
- copy = new Set(data);
289
- }
290
- else if (!(isWeb && (data instanceof Blob || data instanceof FileList)) &&
291
- (isArray || isObject(data))) {
292
- copy = isArray ? [] : {};
293
- if (!Array.isArray(data) && !isPlainObject(data)) {
294
- copy = data;
295
- }
296
- else {
297
- for (const key in data) {
298
- copy[key] = cloneObject(data[key]);
299
- }
300
- }
301
- }
302
- else {
303
- return data;
304
- }
305
- return copy;
306
- }
307
-
308
309
  /**
309
310
  * Custom hook to subscribe to field change and isolate re-rendering at the component level.
310
311
  *
@@ -340,6 +341,33 @@ function useWatch(props) {
340
341
  return value;
341
342
  }
342
343
 
344
+ var isKey = (value) => /^\w*$/.test(value);
345
+
346
+ var stringToPath = (input) => compact(input.replace(/["|']|\]/g, '').split(/\.|\[/));
347
+
348
+ function set(object, path, value) {
349
+ let index = -1;
350
+ const tempPath = isKey(path) ? [path] : stringToPath(path);
351
+ const length = tempPath.length;
352
+ const lastIndex = length - 1;
353
+ while (++index < length) {
354
+ const key = tempPath[index];
355
+ let newValue = value;
356
+ if (index !== lastIndex) {
357
+ const objValue = object[key];
358
+ newValue =
359
+ isObject(objValue) || Array.isArray(objValue)
360
+ ? objValue
361
+ : !isNaN(+tempPath[index + 1])
362
+ ? []
363
+ : {};
364
+ }
365
+ object[key] = newValue;
366
+ object = object[key];
367
+ }
368
+ return object;
369
+ }
370
+
343
371
  /**
344
372
  * 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.
345
373
  *
@@ -383,6 +411,7 @@ function useController(props) {
383
411
  value,
384
412
  }));
385
413
  React.useEffect(() => {
414
+ const _shouldUnregisterField = control._options.shouldUnregister || shouldUnregister;
386
415
  const updateMounted = (name, value) => {
387
416
  const field = get(control._fields, name);
388
417
  if (field) {
@@ -390,8 +419,10 @@ function useController(props) {
390
419
  }
391
420
  };
392
421
  updateMounted(name, true);
422
+ if (_shouldUnregisterField) {
423
+ set(control._defaultValues, name, cloneObject(get(control._options.defaultValues, name)));
424
+ }
393
425
  return () => {
394
- const _shouldUnregisterField = control._options.shouldUnregister || shouldUnregister;
395
426
  (isArrayField
396
427
  ? _shouldUnregisterField && !control._state.action
397
428
  : _shouldUnregisterField)
@@ -505,33 +536,6 @@ var appendErrors = (name, validateAllFieldCriteria, errors, type, message) => va
505
536
  }
506
537
  : {};
507
538
 
508
- var isKey = (value) => /^\w*$/.test(value);
509
-
510
- var stringToPath = (input) => compact(input.replace(/["|']|\]/g, '').split(/\.|\[/));
511
-
512
- function set(object, path, value) {
513
- let index = -1;
514
- const tempPath = isKey(path) ? [path] : stringToPath(path);
515
- const length = tempPath.length;
516
- const lastIndex = length - 1;
517
- while (++index < length) {
518
- const key = tempPath[index];
519
- let newValue = value;
520
- if (index !== lastIndex) {
521
- const objValue = object[key];
522
- newValue =
523
- isObject(objValue) || Array.isArray(objValue)
524
- ? objValue
525
- : !isNaN(+tempPath[index + 1])
526
- ? []
527
- : {};
528
- }
529
- object[key] = newValue;
530
- object = object[key];
531
- }
532
- return object;
533
- }
534
-
535
539
  const focusFieldBy = (fields, callback, fieldsNames) => {
536
540
  for (const key of fieldsNames || Object.keys(fields)) {
537
541
  const field = get(fields, key);