react-hook-form 7.56.1 → 7.56.2

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.
@@ -217,47 +217,7 @@ var getProxyFormState = (formState, control, localProxyFormState, isRoot = true)
217
217
  return result;
218
218
  };
219
219
 
220
- var isPrimitive = (value) => isNullOrUndefined(value) || !isObjectType(value);
221
-
222
- function deepEqual(object1, object2) {
223
- if (isPrimitive(object1) || isPrimitive(object2)) {
224
- return object1 === object2;
225
- }
226
- if (isDateObject(object1) && isDateObject(object2)) {
227
- return object1.getTime() === object2.getTime();
228
- }
229
- const keys1 = Object.keys(object1);
230
- const keys2 = Object.keys(object2);
231
- if (keys1.length !== keys2.length) {
232
- return false;
233
- }
234
- for (const key of keys1) {
235
- const val1 = object1[key];
236
- if (!keys2.includes(key)) {
237
- return false;
238
- }
239
- if (key !== 'ref') {
240
- const val2 = object2[key];
241
- if ((isDateObject(val1) && isDateObject(val2)) ||
242
- (isObject(val1) && isObject(val2)) ||
243
- (Array.isArray(val1) && Array.isArray(val2))
244
- ? !deepEqual(val1, val2)
245
- : val1 !== val2) {
246
- return false;
247
- }
248
- }
249
- }
250
- return true;
251
- }
252
-
253
- const useDeepEqualEffect = (effect, deps) => {
254
- const ref = React.useRef(deps);
255
- if (!deepEqual(deps, ref.current)) {
256
- ref.current = deps;
257
- }
258
- // eslint-disable-next-line react-hooks/exhaustive-deps
259
- React.useEffect(effect, ref.current);
260
- };
220
+ const useIsomorphicLayoutEffect = typeof window !== 'undefined' ? React.useLayoutEffect : React.useEffect;
261
221
 
262
222
  /**
263
223
  * This custom hook allows you to subscribe to each form state, and isolate the re-render at the custom hook level. It has its scope in terms of form state subscription, so it would not affect other useFormState and useForm. Using this hook can reduce the re-render impact on large and complex form application.
@@ -303,7 +263,7 @@ function useFormState(props) {
303
263
  isValid: false,
304
264
  errors: false,
305
265
  });
306
- useDeepEqualEffect(() => control._subscribe({
266
+ useIsomorphicLayoutEffect(() => control._subscribe({
307
267
  name: name,
308
268
  formState: _localProxyFormState.current,
309
269
  exact,
@@ -354,16 +314,17 @@ var generateWatchOutput = (names, _names, formValues, isGlobal, defaultValue) =>
354
314
  function useWatch(props) {
355
315
  const methods = useFormContext();
356
316
  const { control = methods.control, name, defaultValue, disabled, exact, } = props || {};
357
- const [value, updateValue] = React__default.useState(control._getWatch(name, defaultValue));
358
- useDeepEqualEffect(() => control._subscribe({
317
+ const _defaultValue = React__default.useRef(defaultValue);
318
+ const [value, updateValue] = React__default.useState(control._getWatch(name, _defaultValue.current));
319
+ useIsomorphicLayoutEffect(() => control._subscribe({
359
320
  name: name,
360
321
  formState: {
361
322
  values: true,
362
323
  },
363
324
  exact,
364
325
  callback: (formState) => !disabled &&
365
- updateValue(generateWatchOutput(name, control._names, formState.values || control._formValues, false, defaultValue)),
366
- }), [name, defaultValue, disabled, exact]);
326
+ updateValue(generateWatchOutput(name, control._names, formState.values || control._formValues, false, _defaultValue.current)),
327
+ }), [name, control, disabled, exact]);
367
328
  React__default.useEffect(() => control._removeUnmounted());
368
329
  return value;
369
330
  }
@@ -452,12 +413,7 @@ function useController(props) {
452
413
  const ref = React__default.useCallback((elm) => {
453
414
  const field = get(control._fields, name);
454
415
  if (field && elm) {
455
- field._f.ref = {
456
- focus: () => elm.focus(),
457
- select: () => elm.select(),
458
- setCustomValidity: (message) => elm.setCustomValidity(message),
459
- reportValidity: () => elm.reportValidity(),
460
- };
416
+ field._f.ref = elm;
461
417
  }
462
418
  }, [control._fields, name]);
463
419
  const field = React__default.useMemo(() => ({
@@ -713,6 +669,39 @@ var createSubject = () => {
713
669
  };
714
670
  };
715
671
 
672
+ var isPrimitive = (value) => isNullOrUndefined(value) || !isObjectType(value);
673
+
674
+ function deepEqual(object1, object2) {
675
+ if (isPrimitive(object1) || isPrimitive(object2)) {
676
+ return object1 === object2;
677
+ }
678
+ if (isDateObject(object1) && isDateObject(object2)) {
679
+ return object1.getTime() === object2.getTime();
680
+ }
681
+ const keys1 = Object.keys(object1);
682
+ const keys2 = Object.keys(object2);
683
+ if (keys1.length !== keys2.length) {
684
+ return false;
685
+ }
686
+ for (const key of keys1) {
687
+ const val1 = object1[key];
688
+ if (!keys2.includes(key)) {
689
+ return false;
690
+ }
691
+ if (key !== 'ref') {
692
+ const val2 = object2[key];
693
+ if ((isDateObject(val1) && isDateObject(val2)) ||
694
+ (isObject(val1) && isObject(val2)) ||
695
+ (Array.isArray(val1) && Array.isArray(val2))
696
+ ? !deepEqual(val1, val2)
697
+ : val1 !== val2) {
698
+ return false;
699
+ }
700
+ }
701
+ }
702
+ return true;
703
+ }
704
+
716
705
  var isEmptyObject = (value) => isObject(value) && !Object.keys(value).length;
717
706
 
718
707
  var isFileInput = (element) => element.type === 'file';
@@ -1276,7 +1265,7 @@ function createFormControl(props = {}) {
1276
1265
  };
1277
1266
  const _fields = {};
1278
1267
  let _defaultValues = isObject(_options.defaultValues) || isObject(_options.values)
1279
- ? cloneObject(_options.values || _options.defaultValues) || {}
1268
+ ? cloneObject(_options.defaultValues || _options.values) || {}
1280
1269
  : {};
1281
1270
  let _formValues = _options.shouldUnregister
1282
1271
  ? {}
@@ -2587,7 +2576,6 @@ function useFieldArray(props) {
2587
2576
  };
2588
2577
  }
2589
2578
 
2590
- const useIsomorphicLayoutEffect = typeof window !== 'undefined' ? React__default.useLayoutEffect : React__default.useEffect;
2591
2579
  /**
2592
2580
  * Custom hook to manage the entire form.
2593
2581
  *