react-hook-form 7.22.5 → 7.23.0-next.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.
- package/README.md +6 -0
- package/dist/index.cjs.js +1 -1
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.esm.mjs +58 -50
- package/dist/index.esm.mjs.map +1 -1
- package/dist/index.umd.js +1 -1
- package/dist/index.umd.js.map +1 -1
- package/dist/logic/mapCurrentIds.d.ts +1 -1
- package/dist/logic/mapCurrentIds.d.ts.map +1 -1
- package/dist/types/fieldArray.d.ts +1 -1
- package/dist/types/fieldArray.d.ts.map +1 -1
- package/dist/types/form.d.ts +2 -1
- package/dist/types/form.d.ts.map +1 -1
- package/dist/types/index.d.ts +1 -0
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/path/common.d.ts +316 -0
- package/dist/types/path/common.d.ts.map +1 -0
- package/dist/types/path/eager.d.ts +82 -0
- package/dist/types/path/eager.d.ts.map +1 -0
- package/dist/types/path/index.d.ts +4 -0
- package/dist/types/path/index.d.ts.map +1 -0
- package/dist/types/utils.d.ts +19 -25
- package/dist/types/utils.d.ts.map +1 -1
- package/dist/types/validator.d.ts +1 -1
- package/dist/types/validator.d.ts.map +1 -1
- package/dist/useFieldArray.d.ts.map +1 -1
- package/dist/useForm.d.ts.map +1 -1
- package/dist/useFormState.d.ts.map +1 -1
- package/package.json +5 -2
- package/dist/logic/mapId.d.ts +0 -4
- package/dist/logic/mapId.d.ts.map +0 -1
package/dist/index.esm.mjs
CHANGED
@@ -143,11 +143,12 @@ function useFormState(props) {
|
|
143
143
|
});
|
144
144
|
const _name = React.useRef(name);
|
145
145
|
_name.current = name;
|
146
|
+
const callback = React.useCallback((value) => shouldSubscribeByName(_name.current, value.name, exact) &&
|
147
|
+
shouldRenderFormState(value, _localProxyFormState.current) &&
|
148
|
+
updateFormState(Object.assign(Object.assign({}, control._formState), value)), [control, exact]);
|
146
149
|
useSubscribe({
|
147
150
|
disabled,
|
148
|
-
callback
|
149
|
-
shouldRenderFormState(value, _localProxyFormState.current) &&
|
150
|
-
updateFormState(Object.assign(Object.assign({}, control._formState), value)),
|
151
|
+
callback,
|
151
152
|
subject: control._subjects.state,
|
152
153
|
});
|
153
154
|
return getProxyFormState(formState, control._proxyFormState, _localProxyFormState.current, false);
|
@@ -185,21 +186,22 @@ function useWatch(props) {
|
|
185
186
|
const { control = methods.control, name, defaultValue, disabled, exact, } = props || {};
|
186
187
|
const _name = React.useRef(name);
|
187
188
|
_name.current = name;
|
189
|
+
const callback = React.useCallback((formState) => {
|
190
|
+
if (shouldSubscribeByName(_name.current, formState.name, exact)) {
|
191
|
+
const fieldValues = generateWatchOutput(_name.current, control._names, formState.values || control._formValues);
|
192
|
+
updateValue(isUndefined(_name.current) ||
|
193
|
+
(isObject(fieldValues) && !objectHasFunction(fieldValues))
|
194
|
+
? Object.assign({}, fieldValues) : Array.isArray(fieldValues)
|
195
|
+
? [...fieldValues]
|
196
|
+
: isUndefined(fieldValues)
|
197
|
+
? defaultValue
|
198
|
+
: fieldValues);
|
199
|
+
}
|
200
|
+
}, [control, exact, defaultValue]);
|
188
201
|
useSubscribe({
|
189
202
|
disabled,
|
190
203
|
subject: control._subjects.watch,
|
191
|
-
callback
|
192
|
-
if (shouldSubscribeByName(_name.current, formState.name, exact)) {
|
193
|
-
const fieldValues = generateWatchOutput(_name.current, control._names, formState.values || control._formValues);
|
194
|
-
updateValue(isUndefined(_name.current) ||
|
195
|
-
(isObject(fieldValues) && !objectHasFunction(fieldValues))
|
196
|
-
? Object.assign({}, fieldValues) : Array.isArray(fieldValues)
|
197
|
-
? [...fieldValues]
|
198
|
-
: isUndefined(fieldValues)
|
199
|
-
? defaultValue
|
200
|
-
: fieldValues);
|
201
|
-
}
|
202
|
-
},
|
204
|
+
callback,
|
203
205
|
});
|
204
206
|
const [value, updateValue] = React.useState(isUndefined(defaultValue)
|
205
207
|
? control._getWatch(name)
|
@@ -352,11 +354,6 @@ var isWatched = (name, _names, isBlurEvent) => !isBlurEvent &&
|
|
352
354
|
[..._names.watch].some((watchName) => name.startsWith(watchName) &&
|
353
355
|
/^\.\w+/.test(name.slice(watchName.length))));
|
354
356
|
|
355
|
-
var mapCurrentIds = (values, _fieldIds, keyName) => values.map((value, index) => {
|
356
|
-
const output = _fieldIds.current[index];
|
357
|
-
return Object.assign(Object.assign({}, value), (output ? { [keyName]: output[keyName] } : {}));
|
358
|
-
});
|
359
|
-
|
360
357
|
var generateId = () => {
|
361
358
|
const d = typeof performance === 'undefined' ? Date.now() : performance.now() * 1000;
|
362
359
|
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => {
|
@@ -365,7 +362,9 @@ var generateId = () => {
|
|
365
362
|
});
|
366
363
|
};
|
367
364
|
|
368
|
-
var
|
365
|
+
var mapCurrentIds = (values, keyName, _fieldIds) => values.map((value, index) => (Object.assign(Object.assign({}, value), { [keyName]: _fieldIds && _fieldIds.current[index]
|
366
|
+
? _fieldIds.current[index][keyName]
|
367
|
+
: value[keyName] || generateId() })));
|
369
368
|
|
370
369
|
function append(data, value) {
|
371
370
|
return [...convertToArrayPayload(data), ...convertToArrayPayload(value)];
|
@@ -448,31 +447,36 @@ var updateAt = (fieldValues, index, value) => {
|
|
448
447
|
const useFieldArray = (props) => {
|
449
448
|
const methods = useFormContext();
|
450
449
|
const { control = methods.control, name, keyName = 'id', shouldUnregister, } = props;
|
451
|
-
const
|
450
|
+
const _shouldKeepKeyName = React.useRef(get(control._getFieldArray(name)[0], keyName));
|
451
|
+
const [fields, setFields] = React.useState(mapCurrentIds(control._getFieldArray(name), keyName));
|
452
452
|
const _fieldIds = React.useRef(fields);
|
453
453
|
const _name = React.useRef(name);
|
454
454
|
const _actioned = React.useRef(false);
|
455
455
|
_name.current = name;
|
456
456
|
_fieldIds.current = fields;
|
457
457
|
control._names.array.add(name);
|
458
|
+
const callback = React.useCallback(({ values, name: fieldArrayName }) => {
|
459
|
+
if (fieldArrayName === _name.current || !fieldArrayName) {
|
460
|
+
setFields(mapCurrentIds(get(values, _name.current, []), keyName));
|
461
|
+
}
|
462
|
+
}, [keyName]);
|
458
463
|
useSubscribe({
|
459
|
-
callback
|
460
|
-
if (fieldArrayName === _name.current || !fieldArrayName) {
|
461
|
-
setFields(mapIds(get(values, _name.current), keyName));
|
462
|
-
}
|
463
|
-
},
|
464
|
+
callback,
|
464
465
|
subject: control._subjects.array,
|
465
466
|
});
|
466
|
-
const updateValues = React.useCallback((updatedFieldArrayValuesWithKey) => {
|
467
|
-
const updatedFieldArrayValues =
|
467
|
+
const updateValues = React.useCallback((updatedFieldArrayValuesWithKey, hasKey = _shouldKeepKeyName.current) => {
|
468
|
+
const updatedFieldArrayValues = hasKey
|
469
|
+
? updatedFieldArrayValuesWithKey
|
470
|
+
: omitKeys(updatedFieldArrayValuesWithKey, keyName);
|
471
|
+
hasKey && (_shouldKeepKeyName.current = true);
|
468
472
|
_actioned.current = true;
|
469
473
|
set(control._formValues, name, updatedFieldArrayValues);
|
470
474
|
return updatedFieldArrayValues;
|
471
475
|
}, [control, name, keyName]);
|
472
476
|
const append$1 = (value, options) => {
|
473
477
|
const appendValue = convertToArrayPayload(cloneObject(value));
|
474
|
-
const updatedFieldArrayValuesWithKey = append(mapCurrentIds(control._getFieldArray(name),
|
475
|
-
const fieldArrayValues = updateValues(updatedFieldArrayValuesWithKey);
|
478
|
+
const updatedFieldArrayValuesWithKey = append(mapCurrentIds(control._getFieldArray(name), keyName, _fieldIds), mapCurrentIds(appendValue, keyName));
|
479
|
+
const fieldArrayValues = updateValues(updatedFieldArrayValuesWithKey, appendValue[0][keyName]);
|
476
480
|
control._names.focus = getFocusFieldName(name, fieldArrayValues.length - 1, options);
|
477
481
|
setFields(updatedFieldArrayValuesWithKey);
|
478
482
|
control._updateFieldArray(name, append, {
|
@@ -480,8 +484,9 @@ const useFieldArray = (props) => {
|
|
480
484
|
}, fieldArrayValues);
|
481
485
|
};
|
482
486
|
const prepend$1 = (value, options) => {
|
483
|
-
const
|
484
|
-
const
|
487
|
+
const prependValue = convertToArrayPayload(cloneObject(value));
|
488
|
+
const updatedFieldArrayValuesWithKey = prepend(mapCurrentIds(control._getFieldArray(name), keyName, _fieldIds), mapCurrentIds(prependValue, keyName));
|
489
|
+
const fieldArrayValues = updateValues(updatedFieldArrayValuesWithKey, prependValue[0][keyName]);
|
485
490
|
control._names.focus = getFocusFieldName(name, 0, options);
|
486
491
|
setFields(updatedFieldArrayValuesWithKey);
|
487
492
|
control._updateFieldArray(name, prepend, {
|
@@ -489,7 +494,7 @@ const useFieldArray = (props) => {
|
|
489
494
|
}, fieldArrayValues);
|
490
495
|
};
|
491
496
|
const remove = (index) => {
|
492
|
-
const updatedFieldArrayValuesWithKey = removeArrayAt(mapCurrentIds(control._getFieldArray(name),
|
497
|
+
const updatedFieldArrayValuesWithKey = removeArrayAt(mapCurrentIds(control._getFieldArray(name), keyName, _fieldIds), index);
|
493
498
|
const fieldArrayValues = updateValues(updatedFieldArrayValuesWithKey);
|
494
499
|
setFields(updatedFieldArrayValuesWithKey);
|
495
500
|
control._updateFieldArray(name, removeArrayAt, {
|
@@ -497,8 +502,9 @@ const useFieldArray = (props) => {
|
|
497
502
|
}, fieldArrayValues);
|
498
503
|
};
|
499
504
|
const insert$1 = (index, value, options) => {
|
500
|
-
const
|
501
|
-
const
|
505
|
+
const insertValue = convertToArrayPayload(cloneObject(value));
|
506
|
+
const updatedFieldArrayValuesWithKey = insert(mapCurrentIds(control._getFieldArray(name), keyName, _fieldIds), index, mapCurrentIds(insertValue, keyName));
|
507
|
+
const fieldArrayValues = updateValues(updatedFieldArrayValuesWithKey, insertValue[0][keyName]);
|
502
508
|
control._names.focus = getFocusFieldName(name, index, options);
|
503
509
|
setFields(updatedFieldArrayValuesWithKey);
|
504
510
|
control._updateFieldArray(name, insert, {
|
@@ -507,7 +513,7 @@ const useFieldArray = (props) => {
|
|
507
513
|
}, fieldArrayValues);
|
508
514
|
};
|
509
515
|
const swap = (indexA, indexB) => {
|
510
|
-
const updatedFieldArrayValuesWithKey = mapCurrentIds(control._getFieldArray(name),
|
516
|
+
const updatedFieldArrayValuesWithKey = mapCurrentIds(control._getFieldArray(name), keyName, _fieldIds);
|
511
517
|
swapArrayAt(updatedFieldArrayValuesWithKey, indexA, indexB);
|
512
518
|
const fieldArrayValues = updateValues(updatedFieldArrayValuesWithKey);
|
513
519
|
setFields(updatedFieldArrayValuesWithKey);
|
@@ -517,7 +523,7 @@ const useFieldArray = (props) => {
|
|
517
523
|
}, fieldArrayValues, false);
|
518
524
|
};
|
519
525
|
const move = (from, to) => {
|
520
|
-
const updatedFieldArrayValuesWithKey = mapCurrentIds(control._getFieldArray(name),
|
526
|
+
const updatedFieldArrayValuesWithKey = mapCurrentIds(control._getFieldArray(name), keyName, _fieldIds);
|
521
527
|
moveArrayAt(updatedFieldArrayValuesWithKey, from, to);
|
522
528
|
const fieldArrayValues = updateValues(updatedFieldArrayValuesWithKey);
|
523
529
|
setFields(updatedFieldArrayValuesWithKey);
|
@@ -527,10 +533,10 @@ const useFieldArray = (props) => {
|
|
527
533
|
}, fieldArrayValues, false);
|
528
534
|
};
|
529
535
|
const update = (index, value) => {
|
530
|
-
const updatedFieldArrayValuesWithKey = mapCurrentIds(control._getFieldArray(name),
|
536
|
+
const updatedFieldArrayValuesWithKey = mapCurrentIds(control._getFieldArray(name), keyName, _fieldIds);
|
531
537
|
const updatedFieldArrayValues = updateAt(updatedFieldArrayValuesWithKey, index, value);
|
532
|
-
_fieldIds.current =
|
533
|
-
const fieldArrayValues = updateValues(_fieldIds.current);
|
538
|
+
_fieldIds.current = mapCurrentIds(updatedFieldArrayValues, keyName);
|
539
|
+
const fieldArrayValues = updateValues(_fieldIds.current, get(value, keyName));
|
534
540
|
setFields(_fieldIds.current);
|
535
541
|
control._updateFieldArray(name, updateAt, {
|
536
542
|
argA: index,
|
@@ -538,8 +544,9 @@ const useFieldArray = (props) => {
|
|
538
544
|
}, fieldArrayValues, true, false, false);
|
539
545
|
};
|
540
546
|
const replace = (value) => {
|
541
|
-
const
|
542
|
-
const
|
547
|
+
const updateValue = convertToArrayPayload(value);
|
548
|
+
const updatedFieldArrayValuesWithKey = mapCurrentIds(updateValue, keyName);
|
549
|
+
const fieldArrayValues = updateValues(updatedFieldArrayValuesWithKey, updateValue[0][keyName]);
|
543
550
|
setFields(updatedFieldArrayValuesWithKey);
|
544
551
|
control._updateFieldArray(name, () => updatedFieldArrayValuesWithKey, {}, fieldArrayValues, true, false, false);
|
545
552
|
};
|
@@ -1178,8 +1185,8 @@ function createFormControl(props = {}) {
|
|
1178
1185
|
shouldSkipSetValueAs
|
1179
1186
|
? set(_formValues, name, shouldSkipSetValueAs ? defaultValue : getFieldValue(field._f))
|
1180
1187
|
: setFieldValue(name, defaultValue);
|
1188
|
+
_stateFlags.mount && _updateValid();
|
1181
1189
|
}
|
1182
|
-
_stateFlags.mount && _updateValid();
|
1183
1190
|
};
|
1184
1191
|
const updateTouchAndDirty = (name, fieldValue, isCurrentTouched, shouldRender = true) => {
|
1185
1192
|
let isFieldDirty = false;
|
@@ -1843,14 +1850,15 @@ function useForm(props = {}) {
|
|
1843
1850
|
_formControl.current = Object.assign(Object.assign({}, createFormControl(props)), { formState });
|
1844
1851
|
}
|
1845
1852
|
const control = _formControl.current.control;
|
1853
|
+
const callback = React.useCallback((value) => {
|
1854
|
+
if (shouldRenderFormState(value, control._proxyFormState, true)) {
|
1855
|
+
control._formState = Object.assign(Object.assign({}, control._formState), value);
|
1856
|
+
updateFormState(Object.assign({}, control._formState));
|
1857
|
+
}
|
1858
|
+
}, [control]);
|
1846
1859
|
useSubscribe({
|
1847
1860
|
subject: control._subjects.state,
|
1848
|
-
callback
|
1849
|
-
if (shouldRenderFormState(value, control._proxyFormState, true)) {
|
1850
|
-
control._formState = Object.assign(Object.assign({}, control._formState), value);
|
1851
|
-
updateFormState(Object.assign({}, control._formState));
|
1852
|
-
}
|
1853
|
-
},
|
1861
|
+
callback,
|
1854
1862
|
});
|
1855
1863
|
React.useEffect(() => {
|
1856
1864
|
if (!control._stateFlags.mount) {
|