react-hook-form 7.82.0 → 7.83.0

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.
@@ -2,6 +2,8 @@ import React from 'react';
2
2
 
3
3
  var isCheckBoxInput = (element) => element.type === 'checkbox';
4
4
 
5
+ var isFileInput = (element) => element.type === 'file';
6
+
5
7
  var isDateObject = (value) => value instanceof Date;
6
8
 
7
9
  var isNullOrUndefined = (value) => value == null;
@@ -15,7 +17,9 @@ var isObject = (value) => !isNullOrUndefined(value) &&
15
17
  var getEventValue = (event) => isObject(event) && event.target
16
18
  ? isCheckBoxInput(event.target)
17
19
  ? event.target.checked
18
- : event.target.value
20
+ : isFileInput(event.target)
21
+ ? event.target.files
22
+ : event.target.value
19
23
  : event;
20
24
 
21
25
  var isNameInFieldArray = (names, name) => name
@@ -167,12 +171,12 @@ const useIsomorphicLayoutEffect = isWeb
167
171
  : React.useEffect;
168
172
 
169
173
  /**
170
- * 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.
174
+ * Subscribes to each form state and isolates re-renders at the custom hook level. It has its own scope for form state subscriptions, so it will not affect other useFormState or useForm instances. Using this hook can reduce the re-render impact on large and complex form applications.
171
175
  *
172
176
  * @remarks
173
177
  * [API](https://react-hook-form.com/docs/useformstate) • [Demo](https://codesandbox.io/s/useformstate-75xly)
174
178
  *
175
- * @param props - include options on specify fields to subscribe. {@link UseFormStateReturn}
179
+ * @param props - Include options to specify fields to subscribe to. {@link UseFormStateReturn}
176
180
  *
177
181
  * @example
178
182
  * ```tsx
@@ -304,7 +308,7 @@ function deepEqual(object1, object2, visited = new WeakMap()) {
304
308
  }
305
309
 
306
310
  /**
307
- * Custom hook to subscribe to field change and isolate re-rendering at the component level.
311
+ * Custom hook to subscribe to field changes and isolate re-rendering at the component level.
308
312
  *
309
313
  * @remarks
310
314
  *
@@ -314,7 +318,7 @@ function deepEqual(object1, object2, visited = new WeakMap()) {
314
318
  * ```tsx
315
319
  * const { control } = useForm();
316
320
  * const values = useWatch({
317
- * name: "fieldName"
321
+ * name: "fieldName",
318
322
  * control,
319
323
  * })
320
324
  * ```
@@ -508,7 +512,7 @@ function useController(props) {
508
512
  }), [name, disabled, formState.disabled, onChange, onBlur, ref, value]);
509
513
  React.useEffect(() => {
510
514
  const _shouldUnregisterField = control._options.shouldUnregister || shouldUnregister;
511
- control.register(name, {
515
+ _registerProps.current = control.register(name, {
512
516
  ..._props.current.rules,
513
517
  ...(isBoolean(_props.current.disabled)
514
518
  ? { disabled: _props.current.disabled }
@@ -676,8 +680,6 @@ var updateFieldArrayRootError = (errors, error, name) => {
676
680
 
677
681
  var isEmptyObject = (value) => isObject(value) && !Object.keys(value).length;
678
682
 
679
- var isFileInput = (element) => element.type === 'file';
680
-
681
683
  var isHTMLElement = (value) => {
682
684
  if (!isWeb) {
683
685
  return false;
@@ -1924,6 +1926,14 @@ const defaultOptions = {
1924
1926
  shouldFocusError: true,
1925
1927
  };
1926
1928
  const FORM_ERROR_TYPE = 'form';
1929
+ const updateDirtyFields = (dirtyFields, nextDirtyFields) => {
1930
+ for (const key in dirtyFields) {
1931
+ if (!(key in nextDirtyFields)) {
1932
+ delete dirtyFields[key];
1933
+ }
1934
+ }
1935
+ Object.assign(dirtyFields, nextDirtyFields);
1936
+ };
1927
1937
  const DEFAULT_FORM_STATE = {
1928
1938
  submitCount: 0,
1929
1939
  isDirty: false,
@@ -2166,7 +2176,7 @@ function createFormControl(props = {}) {
2166
2176
  }
2167
2177
  isPreviousDirty = !!get(_formState.dirtyFields, name);
2168
2178
  if (isCurrentFieldPristine !== _formState.isDirty) {
2169
- _formState.dirtyFields = getDirtyFields(_defaultValues, _formValues, undefined, _fields);
2179
+ updateDirtyFields(_formState.dirtyFields, getDirtyFields(_defaultValues, _formValues, undefined, _fields));
2170
2180
  }
2171
2181
  else {
2172
2182
  isCurrentFieldPristine
@@ -2723,8 +2733,9 @@ function createFormControl(props = {}) {
2723
2733
  });
2724
2734
  }
2725
2735
  else {
2736
+ _formState.errors = {};
2726
2737
  _subjects.state.next({
2727
- errors: {},
2738
+ errors: _formState.errors,
2728
2739
  });
2729
2740
  }
2730
2741
  };
@@ -2909,20 +2920,23 @@ function createFormControl(props = {}) {
2909
2920
  : fieldRef === field._f.ref) {
2910
2921
  return;
2911
2922
  }
2923
+ const newField = {
2924
+ ...field._f,
2925
+ };
2926
+ if (radioOrCheckbox) {
2927
+ newField.refs = [
2928
+ ...refs.filter(live),
2929
+ fieldRef,
2930
+ ...(Array.isArray(get(_defaultValues, name)) ? [{}] : []),
2931
+ ];
2932
+ newField.ref = { type: fieldRef.type, name };
2933
+ }
2934
+ else {
2935
+ newField.ref = fieldRef;
2936
+ delete newField.refs;
2937
+ }
2912
2938
  set(_fields, name, {
2913
- _f: {
2914
- ...field._f,
2915
- ...(radioOrCheckbox
2916
- ? {
2917
- refs: [
2918
- ...refs.filter(live),
2919
- fieldRef,
2920
- ...(Array.isArray(get(_defaultValues, name)) ? [{}] : []),
2921
- ],
2922
- ref: { type: fieldRef.type, name },
2923
- }
2924
- : { ref: fieldRef }),
2925
- },
2939
+ _f: newField,
2926
2940
  });
2927
2941
  updateValidAndValue(name, false, undefined, fieldRef);
2928
2942
  }