react-hook-form 7.5.2 → 7.5.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/README.md +12 -0
- package/dist/index.cjs.js +1 -1
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.esm.js +16 -6
- package/dist/index.esm.js.map +1 -1
- package/dist/index.umd.js +1 -1
- package/dist/index.umd.js.map +1 -1
- package/dist/types/form.d.ts +1 -0
- package/package.json +1 -1
package/dist/index.esm.js
CHANGED
@@ -133,10 +133,10 @@ function useFormState(props) {
|
|
133
133
|
|
134
134
|
function useController({ name, rules, defaultValue, control, shouldUnregister, }) {
|
135
135
|
const methods = useFormContext();
|
136
|
-
const { defaultValuesRef, register, fieldsRef, unregister, fieldArrayNamesRef, controllerSubjectRef, shouldUnmountUnregister, } = control || methods.control;
|
136
|
+
const { defaultValuesRef, register, fieldsRef, unregister, fieldArrayNamesRef, controllerSubjectRef, shouldUnmountUnregister, inFieldArrayActionRef, } = control || methods.control;
|
137
137
|
const { onChange, onBlur, ref } = register(name, rules);
|
138
|
-
const
|
139
|
-
|
138
|
+
const isFieldArray = isNameInFieldArray(fieldArrayNamesRef.current, name);
|
139
|
+
const [value, setInputStateValue] = React.useState(isUndefined(get(fieldsRef.current, name)._f.value) || isFieldArray
|
140
140
|
? isUndefined(defaultValue)
|
141
141
|
? get(defaultValuesRef.current, name)
|
142
142
|
: defaultValue
|
@@ -153,7 +153,10 @@ function useController({ name, rules, defaultValue, control, shouldUnregister, }
|
|
153
153
|
});
|
154
154
|
return () => {
|
155
155
|
controllerSubscription.unsubscribe();
|
156
|
-
|
156
|
+
const shouldUnmount = shouldUnmountUnregister || shouldUnregister;
|
157
|
+
if (isFieldArray
|
158
|
+
? shouldUnmount && !inFieldArrayActionRef.current
|
159
|
+
: shouldUnmount) {
|
157
160
|
unregister(name);
|
158
161
|
}
|
159
162
|
else if (get(fieldsRef.current, name)) {
|
@@ -462,7 +465,7 @@ function unset(object, path) {
|
|
462
465
|
const useFieldArray = ({ control, name, keyName = 'id', shouldUnregister, }) => {
|
463
466
|
const methods = useFormContext();
|
464
467
|
const focusNameRef = React.useRef('');
|
465
|
-
const { isWatchAllRef, watchFieldsRef, getIsDirty, watchSubjectRef, fieldArraySubjectRef, fieldArrayNamesRef, fieldsRef, defaultValuesRef, formStateRef, formStateSubjectRef, readFormStateRef, validFieldsRef, fieldsWithValidationRef, fieldArrayDefaultValuesRef, unregister, shouldUnmountUnregister, } = control || methods.control;
|
468
|
+
const { isWatchAllRef, watchFieldsRef, getIsDirty, watchSubjectRef, fieldArraySubjectRef, fieldArrayNamesRef, fieldsRef, defaultValuesRef, formStateRef, formStateSubjectRef, readFormStateRef, validFieldsRef, fieldsWithValidationRef, fieldArrayDefaultValuesRef, unregister, shouldUnmountUnregister, inFieldArrayActionRef, } = control || methods.control;
|
466
469
|
const [fields, setFields] = React.useState(mapIds(get(fieldsRef.current, name)
|
467
470
|
? get(getFieldsValues(fieldsRef), name)
|
468
471
|
: get(fieldArrayDefaultValuesRef.current, getNodeParentName(name))
|
@@ -490,6 +493,7 @@ const useFieldArray = ({ control, name, keyName = 'id', shouldUnregister, }) =>
|
|
490
493
|
const updateDirtyFieldsWithDefaultValues = (updatedFieldArrayValues) => updatedFieldArrayValues &&
|
491
494
|
set(formStateRef.current.dirtyFields, name, setFieldArrayDirtyFields(omitKey(updatedFieldArrayValues), get(defaultValuesRef.current, name, []), get(formStateRef.current.dirtyFields, name, [])));
|
492
495
|
const batchStateUpdate = (method, args, updatedFieldArrayValues = [], shouldSet = true) => {
|
496
|
+
inFieldArrayActionRef.current = true;
|
493
497
|
if (get(fieldsRef.current, name)) {
|
494
498
|
const output = method(get(fieldsRef.current, name), args.argA, args.argB);
|
495
499
|
shouldSet && set(fieldsRef.current, name, output);
|
@@ -597,6 +601,7 @@ const useFieldArray = ({ control, name, keyName = 'id', shouldUnregister, }) =>
|
|
597
601
|
}, fieldValues, false);
|
598
602
|
};
|
599
603
|
React.useEffect(() => {
|
604
|
+
inFieldArrayActionRef.current = false;
|
600
605
|
if (isWatchAllRef.current) {
|
601
606
|
formStateSubjectRef.current.next({});
|
602
607
|
}
|
@@ -988,6 +993,7 @@ function useForm({ mode = VALIDATION_MODE.onSubmit, reValidateMode = VALIDATION_
|
|
988
993
|
const controllerSubjectRef = React.useRef(new Subject());
|
989
994
|
const fieldArraySubjectRef = React.useRef(new Subject());
|
990
995
|
const fieldArrayDefaultValuesRef = React.useRef({});
|
996
|
+
const inFieldArrayActionRef = React.useRef(false);
|
991
997
|
const watchFieldsRef = React.useRef(new Set());
|
992
998
|
const isMountedRef = React.useRef(false);
|
993
999
|
const fieldsWithValidationRef = React.useRef({});
|
@@ -1484,9 +1490,12 @@ function useForm({ mode = VALIDATION_MODE.onSubmit, reValidateMode = VALIDATION_
|
|
1484
1490
|
}
|
1485
1491
|
else {
|
1486
1492
|
const field = get(fieldsRef.current, name);
|
1493
|
+
const shouldUnmount = shouldUnregister || (options && options.shouldUnregister);
|
1487
1494
|
field && (field._f.mount = false);
|
1488
1495
|
if (isWeb &&
|
1489
|
-
(
|
1496
|
+
(isNameInFieldArray(fieldArrayNamesRef.current, name)
|
1497
|
+
? shouldUnmount && !inFieldArrayActionRef.current
|
1498
|
+
: shouldUnmount)) {
|
1490
1499
|
unregisterFieldsNamesRef.current.add(name);
|
1491
1500
|
}
|
1492
1501
|
}
|
@@ -1648,6 +1657,7 @@ function useForm({ mode = VALIDATION_MODE.onSubmit, reValidateMode = VALIDATION_
|
|
1648
1657
|
control: React.useMemo(() => ({
|
1649
1658
|
register,
|
1650
1659
|
isWatchAllRef,
|
1660
|
+
inFieldArrayActionRef,
|
1651
1661
|
watchFieldsRef,
|
1652
1662
|
getIsDirty,
|
1653
1663
|
formStateSubjectRef,
|