react-hook-form 7.17.1 → 7.17.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.
- 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 +45 -35
- 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/controller.d.ts +6 -8
- package/dist/types/fields.d.ts +2 -2
- package/dist/types/form.d.ts +4 -4
- package/dist/types/utils.d.ts +18 -10
- package/dist/useSubscribe.d.ts +3 -2
- package/dist/utils/Subject.d.ts +3 -2
- package/package.json +1 -1
package/dist/index.esm.js
CHANGED
@@ -103,27 +103,33 @@ var shouldRenderFormState = (formStateData, _proxyFormState, isRoot) => {
|
|
103
103
|
|
104
104
|
var convertToArrayPayload = (value) => Array.isArray(value) ? value : [value];
|
105
105
|
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
if (_subscription.current) {
|
111
|
-
_unSubscribe.current && _unSubscribe.current.unsubscribe();
|
112
|
-
_subscription.current = _unSubscribe.current = undefined;
|
113
|
-
}
|
106
|
+
const tearDown = (_unsubscribe) => {
|
107
|
+
if (_unsubscribe.current) {
|
108
|
+
_unsubscribe.current.unsubscribe();
|
109
|
+
_unsubscribe.current = undefined;
|
114
110
|
}
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
if (!_unSubscribe.current && _subscription.current) {
|
120
|
-
_unSubscribe.current = _subscription.current.subscribe({
|
121
|
-
next: callback,
|
122
|
-
});
|
123
|
-
}
|
111
|
+
};
|
112
|
+
const updateSubscriptionProps = ({ _unsubscribe, props }) => () => {
|
113
|
+
if (props.disabled) {
|
114
|
+
tearDown(_unsubscribe);
|
124
115
|
}
|
125
|
-
|
126
|
-
|
116
|
+
else if (!_unsubscribe.current) {
|
117
|
+
_unsubscribe.current = props.subject.subscribe({
|
118
|
+
next: props.callback,
|
119
|
+
});
|
120
|
+
}
|
121
|
+
};
|
122
|
+
function useSubscribe(props) {
|
123
|
+
const _unsubscribe = React.useRef();
|
124
|
+
const _updateSubscription = React.useRef(() => { });
|
125
|
+
_updateSubscription.current = updateSubscriptionProps({
|
126
|
+
_unsubscribe,
|
127
|
+
props,
|
128
|
+
});
|
129
|
+
!props.skipEarlySubscription && _updateSubscription.current();
|
130
|
+
React.useEffect(() => {
|
131
|
+
_updateSubscription.current();
|
132
|
+
return () => tearDown(_unsubscribe);
|
127
133
|
}, []);
|
128
134
|
}
|
129
135
|
|
@@ -166,7 +172,7 @@ function useController(props) {
|
|
166
172
|
useSubscribe({
|
167
173
|
subject: control._subjects.control,
|
168
174
|
callback: (data) => (!data.name || _name.current === data.name) &&
|
169
|
-
setInputStateValue(get(data.values,
|
175
|
+
setInputStateValue(get(data.values, _name.current)),
|
170
176
|
});
|
171
177
|
const registerProps = control.register(name, Object.assign(Object.assign({}, props.rules), { value }));
|
172
178
|
const updateMounted = React.useCallback((name, value) => {
|
@@ -366,15 +372,18 @@ const useFieldArray = (props) => {
|
|
366
372
|
const { control = methods.control, name, keyName = 'id', shouldUnregister, } = props;
|
367
373
|
const [fields, setFields] = React.useState(mapIds(control._getFieldArray(name), keyName));
|
368
374
|
const _fieldIds = React.useRef(fields);
|
375
|
+
const _name = React.useRef(name);
|
376
|
+
_name.current = name;
|
369
377
|
_fieldIds.current = fields;
|
370
378
|
control._names.array.add(name);
|
371
379
|
useSubscribe({
|
372
380
|
callback: ({ values, name: fieldArrayName }) => {
|
373
|
-
if (fieldArrayName ===
|
374
|
-
setFields(mapIds(get(values,
|
381
|
+
if (fieldArrayName === _name.current || !fieldArrayName) {
|
382
|
+
setFields(mapIds(get(values, _name.current), keyName));
|
375
383
|
}
|
376
384
|
},
|
377
385
|
subject: control._subjects.array,
|
386
|
+
skipEarlySubscription: true,
|
378
387
|
});
|
379
388
|
const updateValues = React.useCallback((updatedFieldArrayValuesWithKey) => {
|
380
389
|
const updatedFieldArrayValues = omitKeys(updatedFieldArrayValuesWithKey, keyName);
|
@@ -483,6 +492,8 @@ const useFieldArray = (props) => {
|
|
483
492
|
};
|
484
493
|
};
|
485
494
|
|
495
|
+
var isFunction = (value) => typeof value === 'function';
|
496
|
+
|
486
497
|
function cloneObject(data) {
|
487
498
|
let copy;
|
488
499
|
const isArray = Array.isArray(data);
|
@@ -495,6 +506,10 @@ function cloneObject(data) {
|
|
495
506
|
else if (isArray || isObject(data)) {
|
496
507
|
copy = isArray ? [] : {};
|
497
508
|
for (const key in data) {
|
509
|
+
if (isFunction(data[key])) {
|
510
|
+
copy = data;
|
511
|
+
break;
|
512
|
+
}
|
498
513
|
copy[key] = cloneObject(data[key]);
|
499
514
|
}
|
500
515
|
}
|
@@ -547,8 +562,6 @@ var getValidationModes = (mode) => ({
|
|
547
562
|
|
548
563
|
var isBoolean = (value) => typeof value === 'boolean';
|
549
564
|
|
550
|
-
var isFunction = (value) => typeof value === 'function';
|
551
|
-
|
552
565
|
var isHTMLElement = (value) => value instanceof HTMLElement;
|
553
566
|
|
554
567
|
var isMultipleSelect = (element) => element.type === `select-multiple`;
|
@@ -1324,8 +1337,8 @@ function createFormControl(props = {}) {
|
|
1324
1337
|
!field._f.deps) ||
|
1325
1338
|
skipValidation(isBlurEvent, get(_formState.touchedFields, name), _formState.isSubmitted, validationModeAfterSubmit, validationModeBeforeSubmit);
|
1326
1339
|
const isWatched = isFieldWatched(name, isBlurEvent);
|
1327
|
-
if (isBlurEvent
|
1328
|
-
field._f.onBlur(event);
|
1340
|
+
if (isBlurEvent) {
|
1341
|
+
field._f.onBlur && field._f.onBlur(event);
|
1329
1342
|
}
|
1330
1343
|
else if (field._f.onChange) {
|
1331
1344
|
field._f.onChange(event);
|
@@ -1558,10 +1571,13 @@ function createFormControl(props = {}) {
|
|
1558
1571
|
}
|
1559
1572
|
};
|
1560
1573
|
const reset = (formValues, keepStateOptions = {}) => {
|
1574
|
+
const hasUpdatedFormValues = !isEmptyObject(formValues);
|
1561
1575
|
const updatedValues = formValues || _defaultValues;
|
1562
1576
|
const cloneUpdatedValues = cloneObject(updatedValues);
|
1577
|
+
if (!keepStateOptions.keepDefaultValues) {
|
1578
|
+
_defaultValues = updatedValues;
|
1579
|
+
}
|
1563
1580
|
if (!keepStateOptions.keepValues) {
|
1564
|
-
_formValues = props.shouldUnregister ? {} : cloneUpdatedValues;
|
1565
1581
|
if (isWeb) {
|
1566
1582
|
for (const name of _names.mount) {
|
1567
1583
|
const field = get(_fields, name);
|
@@ -1578,16 +1594,10 @@ function createFormControl(props = {}) {
|
|
1578
1594
|
}
|
1579
1595
|
}
|
1580
1596
|
}
|
1581
|
-
|
1582
|
-
if (!keepStateOptions.keepDefaultValues) {
|
1583
|
-
_defaultValues = Object.assign({}, updatedValues);
|
1584
|
-
}
|
1585
|
-
if (!keepStateOptions.keepValues) {
|
1597
|
+
_formValues = props.shouldUnregister ? {} : cloneUpdatedValues;
|
1586
1598
|
_fields = {};
|
1587
1599
|
_subjects.control.next({
|
1588
|
-
values:
|
1589
|
-
? _defaultValues
|
1590
|
-
: Object.assign({}, updatedValues),
|
1600
|
+
values: hasUpdatedFormValues ? cloneUpdatedValues : _defaultValues,
|
1591
1601
|
});
|
1592
1602
|
_subjects.watch.next({});
|
1593
1603
|
_subjects.array.next({
|