react-hook-form 7.28.0-next.0 → 7.29.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 +0 -3
- package/dist/index.cjs.js +1 -1
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.esm.mjs +176 -87
- 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/createFormControl.d.ts.map +1 -1
- package/dist/logic/focusFieldBy.d.ts.map +1 -1
- package/dist/logic/shouldRenderFormState.d.ts.map +1 -1
- package/dist/types/fieldArray.d.ts +1 -1
- package/dist/types/form.d.ts +1 -1
- package/dist/types/form.d.ts.map +1 -1
- package/dist/useFormContext.d.ts.map +1 -1
- package/dist/utils/compact.d.ts.map +1 -1
- package/dist/utils/unset.d.ts.map +1 -1
- package/package.json +21 -21
- package/dist/utils/omitKeys.d.ts +0 -3
- package/dist/utils/omitKeys.d.ts.map +0 -1
package/dist/index.esm.mjs
CHANGED
@@ -22,7 +22,7 @@ var getNodeParentName = (name) => name.substring(0, name.search(/.\d/)) || name;
|
|
22
22
|
|
23
23
|
var isNameInFieldArray = (names, name) => [...names].some((current) => getNodeParentName(name) === current);
|
24
24
|
|
25
|
-
var compact = (value) => value.filter(Boolean);
|
25
|
+
var compact = (value) => Array.isArray(value) ? value.filter(Boolean) : [];
|
26
26
|
|
27
27
|
var isUndefined = (val) => val === undefined;
|
28
28
|
|
@@ -60,12 +60,6 @@ const INPUT_VALIDATION_RULES = {
|
|
60
60
|
validate: 'validate',
|
61
61
|
};
|
62
62
|
|
63
|
-
var omit = (source, key) => {
|
64
|
-
const copy = Object.assign({}, source);
|
65
|
-
delete copy[key];
|
66
|
-
return copy;
|
67
|
-
};
|
68
|
-
|
69
63
|
const HookFormContext = React.createContext(null);
|
70
64
|
/**
|
71
65
|
* This custom hook allows you to access the form context. useFormContext is intended to be used in deeply nested structures, where it would become inconvenient to pass the context as a prop. To be used with {@link FormProvider}.
|
@@ -128,7 +122,10 @@ const useFormContext = () => React.useContext(HookFormContext);
|
|
128
122
|
* }
|
129
123
|
* ```
|
130
124
|
*/
|
131
|
-
const FormProvider = (props) =>
|
125
|
+
const FormProvider = (props) => {
|
126
|
+
const { children, ...data } = props;
|
127
|
+
return (React.createElement(HookFormContext.Provider, { value: data }, props.children));
|
128
|
+
};
|
132
129
|
|
133
130
|
var getProxyFormState = (formState, _proxyFormState, localProxyFormState, isRoot = true) => {
|
134
131
|
const result = {};
|
@@ -150,7 +147,7 @@ var getProxyFormState = (formState, _proxyFormState, localProxyFormState, isRoot
|
|
150
147
|
var isEmptyObject = (value) => isObject(value) && !Object.keys(value).length;
|
151
148
|
|
152
149
|
var shouldRenderFormState = (formStateData, _proxyFormState, isRoot) => {
|
153
|
-
const formState =
|
150
|
+
const { name, ...formState } = formStateData;
|
154
151
|
return (isEmptyObject(formState) ||
|
155
152
|
Object.keys(formState).length >= Object.keys(_proxyFormState).length ||
|
156
153
|
Object.keys(formState).find((key) => _proxyFormState[key] ===
|
@@ -233,14 +230,20 @@ function useFormState(props) {
|
|
233
230
|
const callback = React.useCallback((value) => _mounted.current &&
|
234
231
|
shouldSubscribeByName(_name.current, value.name, exact) &&
|
235
232
|
shouldRenderFormState(value, _localProxyFormState.current) &&
|
236
|
-
updateFormState(
|
233
|
+
updateFormState({
|
234
|
+
...control._formState,
|
235
|
+
...value,
|
236
|
+
}), [control, exact]);
|
237
237
|
useSubscribe({
|
238
238
|
disabled,
|
239
239
|
callback,
|
240
240
|
subject: control._subjects.state,
|
241
241
|
});
|
242
|
-
React.useEffect(() =>
|
243
|
-
_mounted.current =
|
242
|
+
React.useEffect(() => {
|
243
|
+
_mounted.current = true;
|
244
|
+
return () => {
|
245
|
+
_mounted.current = false;
|
246
|
+
};
|
244
247
|
}, []);
|
245
248
|
return getProxyFormState(formState, control._proxyFormState, _localProxyFormState.current, false);
|
246
249
|
}
|
@@ -298,11 +301,12 @@ function useWatch(props) {
|
|
298
301
|
const fieldValues = generateWatchOutput(_name.current, control._names, formState.values || control._formValues);
|
299
302
|
updateValue(isUndefined(_name.current) ||
|
300
303
|
(isObject(fieldValues) && !objectHasFunction(fieldValues))
|
301
|
-
?
|
302
|
-
|
303
|
-
|
304
|
-
|
305
|
-
|
304
|
+
? { ...fieldValues }
|
305
|
+
: Array.isArray(fieldValues)
|
306
|
+
? [...fieldValues]
|
307
|
+
: isUndefined(fieldValues)
|
308
|
+
? defaultValue
|
309
|
+
: fieldValues);
|
306
310
|
}
|
307
311
|
}, [control, exact, defaultValue]);
|
308
312
|
useSubscribe({
|
@@ -357,7 +361,10 @@ function useController(props) {
|
|
357
361
|
control,
|
358
362
|
name,
|
359
363
|
});
|
360
|
-
const _registerProps = React.useRef(control.register(name,
|
364
|
+
const _registerProps = React.useRef(control.register(name, {
|
365
|
+
...props.rules,
|
366
|
+
value,
|
367
|
+
}));
|
361
368
|
React.useEffect(() => {
|
362
369
|
const updateMounted = (name, value) => {
|
363
370
|
const field = get(control._fields, name);
|
@@ -458,7 +465,14 @@ function useController(props) {
|
|
458
465
|
const Controller = (props) => props.render(useController(props));
|
459
466
|
|
460
467
|
var appendErrors = (name, validateAllFieldCriteria, errors, type, message) => validateAllFieldCriteria
|
461
|
-
?
|
468
|
+
? {
|
469
|
+
...errors[name],
|
470
|
+
types: {
|
471
|
+
...(errors[name] && errors[name].types ? errors[name].types : {}),
|
472
|
+
[type]: message || true,
|
473
|
+
},
|
474
|
+
}
|
475
|
+
: {};
|
462
476
|
|
463
477
|
var isKey = (value) => /^\w*$/.test(value);
|
464
478
|
|
@@ -491,8 +505,7 @@ const focusFieldBy = (fields, callback, fieldsNames) => {
|
|
491
505
|
for (const key of fieldsNames || Object.keys(fields)) {
|
492
506
|
const field = get(fields, key);
|
493
507
|
if (field) {
|
494
|
-
const _f = field
|
495
|
-
const current = omit(field, '_f');
|
508
|
+
const { _f, ...currentField } = field;
|
496
509
|
if (_f && callback(_f.name)) {
|
497
510
|
if (_f.ref.focus && isUndefined(_f.ref.focus())) {
|
498
511
|
break;
|
@@ -502,8 +515,8 @@ const focusFieldBy = (fields, callback, fieldsNames) => {
|
|
502
515
|
break;
|
503
516
|
}
|
504
517
|
}
|
505
|
-
else if (isObject(
|
506
|
-
focusFieldBy(
|
518
|
+
else if (isObject(currentField)) {
|
519
|
+
focusFieldBy(currentField, callback);
|
507
520
|
}
|
508
521
|
}
|
509
522
|
}
|
@@ -789,7 +802,10 @@ function useFieldArray(props) {
|
|
789
802
|
insert: React.useCallback(insert$1, [updateValues, name, control]),
|
790
803
|
update: React.useCallback(update, [updateValues, name, control]),
|
791
804
|
replace: React.useCallback(replace, [updateValues, name, control]),
|
792
|
-
fields: React.useMemo(() => fields.map((field, index) => (
|
805
|
+
fields: React.useMemo(() => fields.map((field, index) => ({
|
806
|
+
...field,
|
807
|
+
[keyName]: ids.current[index] || generateId(),
|
808
|
+
})), [fields, keyName]),
|
793
809
|
};
|
794
810
|
}
|
795
811
|
|
@@ -910,9 +926,7 @@ function unset(object, path) {
|
|
910
926
|
if (currentPathsLength === index &&
|
911
927
|
((isObject(objectRef) && isEmptyObject(objectRef)) ||
|
912
928
|
(Array.isArray(objectRef) &&
|
913
|
-
!objectRef.filter((data) =>
|
914
|
-
isBoolean(data) ||
|
915
|
-
(Array.isArray(data) && data.length)).length))) {
|
929
|
+
!objectRef.filter((data) => !isUndefined(data)).length))) {
|
916
930
|
previousObjRef ? delete previousObjRef[item] : delete object[item];
|
917
931
|
}
|
918
932
|
previousObjRef = objectRef;
|
@@ -947,7 +961,7 @@ function getDirtyFieldsFromDefaultValues(data, formValues, dirtyFieldsFromValues
|
|
947
961
|
isPrimitive(dirtyFieldsFromValues[key])) {
|
948
962
|
dirtyFieldsFromValues[key] = Array.isArray(data[key])
|
949
963
|
? markFieldsDirty(data[key], [])
|
950
|
-
:
|
964
|
+
: { ...markFieldsDirty(data[key]) };
|
951
965
|
}
|
952
966
|
else {
|
953
967
|
getDirtyFieldsFromDefaultValues(data[key], isNullOrUndefined(formValues) ? {} : formValues[key], dirtyFieldsFromValues[key]);
|
@@ -1157,8 +1171,12 @@ var validateField = async (field, inputValue, validateAllFieldCriteria, shouldUs
|
|
1157
1171
|
const appendErrorsCurry = appendErrors.bind(null, name, validateAllFieldCriteria, error);
|
1158
1172
|
const getMinMaxMessage = (exceedMax, maxLengthMessage, minLengthMessage, maxType = INPUT_VALIDATION_RULES.maxLength, minType = INPUT_VALIDATION_RULES.minLength) => {
|
1159
1173
|
const message = exceedMax ? maxLengthMessage : minLengthMessage;
|
1160
|
-
error[name] =
|
1161
|
-
|
1174
|
+
error[name] = {
|
1175
|
+
type: exceedMax ? maxType : minType,
|
1176
|
+
message,
|
1177
|
+
ref,
|
1178
|
+
...appendErrorsCurry(exceedMax ? maxType : minType, message),
|
1179
|
+
};
|
1162
1180
|
};
|
1163
1181
|
if (required &&
|
1164
1182
|
((!isRadioOrCheckbox && (isEmpty || isNullOrUndefined(inputValue))) ||
|
@@ -1169,7 +1187,12 @@ var validateField = async (field, inputValue, validateAllFieldCriteria, shouldUs
|
|
1169
1187
|
? { value: !!required, message: required }
|
1170
1188
|
: getValueAndMessage(required);
|
1171
1189
|
if (value) {
|
1172
|
-
error[name] =
|
1190
|
+
error[name] = {
|
1191
|
+
type: INPUT_VALIDATION_RULES.required,
|
1192
|
+
message,
|
1193
|
+
ref: inputRef,
|
1194
|
+
...appendErrorsCurry(INPUT_VALIDATION_RULES.required, message),
|
1195
|
+
};
|
1173
1196
|
if (!validateAllFieldCriteria) {
|
1174
1197
|
setCustomValidity(message);
|
1175
1198
|
return error;
|
@@ -1225,8 +1248,12 @@ var validateField = async (field, inputValue, validateAllFieldCriteria, shouldUs
|
|
1225
1248
|
if (pattern && !isEmpty && isString(inputValue)) {
|
1226
1249
|
const { value: patternValue, message } = getValueAndMessage(pattern);
|
1227
1250
|
if (isRegex(patternValue) && !inputValue.match(patternValue)) {
|
1228
|
-
error[name] =
|
1229
|
-
|
1251
|
+
error[name] = {
|
1252
|
+
type: INPUT_VALIDATION_RULES.pattern,
|
1253
|
+
message,
|
1254
|
+
ref,
|
1255
|
+
...appendErrorsCurry(INPUT_VALIDATION_RULES.pattern, message),
|
1256
|
+
};
|
1230
1257
|
if (!validateAllFieldCriteria) {
|
1231
1258
|
setCustomValidity(message);
|
1232
1259
|
return error;
|
@@ -1238,7 +1265,10 @@ var validateField = async (field, inputValue, validateAllFieldCriteria, shouldUs
|
|
1238
1265
|
const result = await validate(inputValue);
|
1239
1266
|
const validateError = getValidateError(result, inputRef);
|
1240
1267
|
if (validateError) {
|
1241
|
-
error[name] =
|
1268
|
+
error[name] = {
|
1269
|
+
...validateError,
|
1270
|
+
...appendErrorsCurry(INPUT_VALIDATION_RULES.validate, validateError.message),
|
1271
|
+
};
|
1242
1272
|
if (!validateAllFieldCriteria) {
|
1243
1273
|
setCustomValidity(validateError.message);
|
1244
1274
|
return error;
|
@@ -1253,7 +1283,10 @@ var validateField = async (field, inputValue, validateAllFieldCriteria, shouldUs
|
|
1253
1283
|
}
|
1254
1284
|
const validateError = getValidateError(await validate[key](inputValue), inputRef, key);
|
1255
1285
|
if (validateError) {
|
1256
|
-
validationResult =
|
1286
|
+
validationResult = {
|
1287
|
+
...validateError,
|
1288
|
+
...appendErrorsCurry(key, validateError.message),
|
1289
|
+
};
|
1257
1290
|
setCustomValidity(validateError.message);
|
1258
1291
|
if (validateAllFieldCriteria) {
|
1259
1292
|
error[name] = validationResult;
|
@@ -1261,7 +1294,10 @@ var validateField = async (field, inputValue, validateAllFieldCriteria, shouldUs
|
|
1261
1294
|
}
|
1262
1295
|
}
|
1263
1296
|
if (!isEmptyObject(validationResult)) {
|
1264
|
-
error[name] =
|
1297
|
+
error[name] = {
|
1298
|
+
ref: inputRef,
|
1299
|
+
...validationResult,
|
1300
|
+
};
|
1265
1301
|
if (!validateAllFieldCriteria) {
|
1266
1302
|
return error;
|
1267
1303
|
}
|
@@ -1278,7 +1314,10 @@ const defaultOptions = {
|
|
1278
1314
|
shouldFocusError: true,
|
1279
1315
|
};
|
1280
1316
|
function createFormControl(props = {}) {
|
1281
|
-
let _options =
|
1317
|
+
let _options = {
|
1318
|
+
...defaultOptions,
|
1319
|
+
...props,
|
1320
|
+
};
|
1282
1321
|
let _formState = {
|
1283
1322
|
isDirty: false,
|
1284
1323
|
isValidating: false,
|
@@ -1292,7 +1331,7 @@ function createFormControl(props = {}) {
|
|
1292
1331
|
errors: {},
|
1293
1332
|
};
|
1294
1333
|
let _fields = {};
|
1295
|
-
let _defaultValues = _options.defaultValues || {};
|
1334
|
+
let _defaultValues = cloneObject(_options.defaultValues) || {};
|
1296
1335
|
let _formValues = _options.shouldUnregister
|
1297
1336
|
? {}
|
1298
1337
|
: cloneObject(_defaultValues);
|
@@ -1446,8 +1485,16 @@ function createFormControl(props = {}) {
|
|
1446
1485
|
!isEmptyObject(fieldState) ||
|
1447
1486
|
shouldUpdateValid) &&
|
1448
1487
|
!shouldSkipRender) {
|
1449
|
-
const updatedFormState =
|
1450
|
-
|
1488
|
+
const updatedFormState = {
|
1489
|
+
...fieldState,
|
1490
|
+
...(shouldUpdateValid ? { isValid } : {}),
|
1491
|
+
errors: _formState.errors,
|
1492
|
+
name,
|
1493
|
+
};
|
1494
|
+
_formState = {
|
1495
|
+
..._formState,
|
1496
|
+
...updatedFormState,
|
1497
|
+
};
|
1451
1498
|
_subjects.state.next(updatedFormState);
|
1452
1499
|
}
|
1453
1500
|
validateFields[name]--;
|
@@ -1460,7 +1507,7 @@ function createFormControl(props = {}) {
|
|
1460
1507
|
}
|
1461
1508
|
};
|
1462
1509
|
const _executeSchema = async (name) => _options.resolver
|
1463
|
-
? await _options.resolver(
|
1510
|
+
? await _options.resolver({ ..._formValues }, _options.context, getResolverOptions(name || _names.mount, _fields, _options.criteriaMode, _options.shouldUseNativeValidation))
|
1464
1511
|
: {};
|
1465
1512
|
const executeSchemaAndUpdateState = async (names) => {
|
1466
1513
|
const { errors } = await _executeSchema();
|
@@ -1483,8 +1530,7 @@ function createFormControl(props = {}) {
|
|
1483
1530
|
for (const name in fields) {
|
1484
1531
|
const field = fields[name];
|
1485
1532
|
if (field) {
|
1486
|
-
const fieldReference = field
|
1487
|
-
const fieldValue = omit(field, '_f');
|
1533
|
+
const { _f: fieldReference, ...fieldValue } = field;
|
1488
1534
|
if (fieldReference) {
|
1489
1535
|
const fieldError = await validateField(field, get(_formValues, fieldReference.name), shouldDisplayAllAssociatedErrors, _options.shouldUseNativeValidation);
|
1490
1536
|
if (fieldError[fieldReference.name]) {
|
@@ -1519,13 +1565,15 @@ function createFormControl(props = {}) {
|
|
1519
1565
|
const _getDirty = (name, data) => (name && data && set(_formValues, name, data),
|
1520
1566
|
!deepEqual(getValues(), _defaultValues));
|
1521
1567
|
const _getWatch = (names, defaultValue, isGlobal) => {
|
1522
|
-
const fieldValues =
|
1523
|
-
|
1524
|
-
|
1525
|
-
|
1526
|
-
|
1527
|
-
|
1528
|
-
|
1568
|
+
const fieldValues = {
|
1569
|
+
...(_stateFlags.mount
|
1570
|
+
? _formValues
|
1571
|
+
: isUndefined(defaultValue)
|
1572
|
+
? _defaultValues
|
1573
|
+
: isString(names)
|
1574
|
+
? { [names]: defaultValue }
|
1575
|
+
: defaultValue),
|
1576
|
+
};
|
1529
1577
|
return generateWatchOutput(names, _names, fieldValues, isGlobal);
|
1530
1578
|
};
|
1531
1579
|
const _getFieldArray = (name) => compact(get(_stateFlags.mount ? _formValues : _defaultValues, name, props.shouldUnregister ? get(_defaultValues, name, []) : []));
|
@@ -1547,9 +1595,10 @@ function createFormControl(props = {}) {
|
|
1547
1595
|
else if (fieldReference.refs) {
|
1548
1596
|
if (isCheckBoxInput(fieldReference.ref)) {
|
1549
1597
|
fieldReference.refs.length > 1
|
1550
|
-
? fieldReference.refs.forEach((checkboxRef) =>
|
1551
|
-
|
1552
|
-
|
1598
|
+
? fieldReference.refs.forEach((checkboxRef) => !checkboxRef.disabled &&
|
1599
|
+
(checkboxRef.checked = Array.isArray(fieldValue)
|
1600
|
+
? !!fieldValue.find((data) => data === checkboxRef.value)
|
1601
|
+
: fieldValue === checkboxRef.value))
|
1553
1602
|
: fieldReference.refs[0] &&
|
1554
1603
|
(fieldReference.refs[0].checked = !!fieldValue);
|
1555
1604
|
}
|
@@ -1650,7 +1699,7 @@ function createFormControl(props = {}) {
|
|
1650
1699
|
});
|
1651
1700
|
if (shouldSkipValidation) {
|
1652
1701
|
return (shouldRender &&
|
1653
|
-
_subjects.state.next(
|
1702
|
+
_subjects.state.next({ name, ...(watched ? {} : fieldState) }));
|
1654
1703
|
}
|
1655
1704
|
!isBlurEvent && watched && _subjects.state.next({});
|
1656
1705
|
validateFields[name] = validateFields[name] ? +1 : 1;
|
@@ -1698,17 +1747,25 @@ function createFormControl(props = {}) {
|
|
1698
1747
|
else {
|
1699
1748
|
validationResult = isValid = await executeBuildInValidation(_fields);
|
1700
1749
|
}
|
1701
|
-
_subjects.state.next(
|
1702
|
-
(
|
1703
|
-
|
1704
|
-
|
1750
|
+
_subjects.state.next({
|
1751
|
+
...(!isString(name) ||
|
1752
|
+
(_proxyFormState.isValid && isValid !== _formState.isValid)
|
1753
|
+
? {}
|
1754
|
+
: { name }),
|
1755
|
+
...(_options.resolver ? { isValid } : {}),
|
1756
|
+
errors: _formState.errors,
|
1757
|
+
isValidating: false,
|
1758
|
+
});
|
1705
1759
|
options.shouldFocus &&
|
1706
1760
|
!validationResult &&
|
1707
1761
|
focusFieldBy(_fields, (key) => get(_formState.errors, key), name ? fieldNames : _names.mount);
|
1708
1762
|
return validationResult;
|
1709
1763
|
};
|
1710
1764
|
const getValues = (fieldNames) => {
|
1711
|
-
const values =
|
1765
|
+
const values = {
|
1766
|
+
..._defaultValues,
|
1767
|
+
...(_stateFlags.mount ? _formValues : {}),
|
1768
|
+
};
|
1712
1769
|
return isUndefined(fieldNames)
|
1713
1770
|
? values
|
1714
1771
|
: isString(fieldNames)
|
@@ -1731,7 +1788,10 @@ function createFormControl(props = {}) {
|
|
1731
1788
|
};
|
1732
1789
|
const setError = (name, error, options) => {
|
1733
1790
|
const ref = (get(_fields, name, { _f: {} })._f || {}).ref;
|
1734
|
-
set(_formState.errors, name,
|
1791
|
+
set(_formState.errors, name, {
|
1792
|
+
...error,
|
1793
|
+
ref,
|
1794
|
+
});
|
1735
1795
|
_subjects.state.next({
|
1736
1796
|
name,
|
1737
1797
|
errors: _formState.errors,
|
@@ -1762,14 +1822,22 @@ function createFormControl(props = {}) {
|
|
1762
1822
|
}
|
1763
1823
|
}
|
1764
1824
|
_subjects.watch.next({});
|
1765
|
-
_subjects.state.next(
|
1825
|
+
_subjects.state.next({
|
1826
|
+
..._formState,
|
1827
|
+
...(!options.keepDirty ? {} : { isDirty: _getDirty() }),
|
1828
|
+
});
|
1766
1829
|
!options.keepIsValid && _updateValid();
|
1767
1830
|
};
|
1768
1831
|
const register = (name, options = {}) => {
|
1769
1832
|
let field = get(_fields, name);
|
1770
1833
|
const disabledIsDefined = isBoolean(options.disabled);
|
1771
1834
|
set(_fields, name, {
|
1772
|
-
_f:
|
1835
|
+
_f: {
|
1836
|
+
...(field && field._f ? field._f : { ref: { name } }),
|
1837
|
+
name,
|
1838
|
+
mount: true,
|
1839
|
+
...options,
|
1840
|
+
},
|
1773
1841
|
});
|
1774
1842
|
_names.mount.add(name);
|
1775
1843
|
field
|
@@ -1778,17 +1846,22 @@ function createFormControl(props = {}) {
|
|
1778
1846
|
? undefined
|
1779
1847
|
: get(_formValues, name, getFieldValue(field._f)))
|
1780
1848
|
: updateValidAndValue(name, true, options.value);
|
1781
|
-
return
|
1782
|
-
? {
|
1783
|
-
|
1784
|
-
|
1785
|
-
|
1786
|
-
|
1787
|
-
|
1788
|
-
|
1789
|
-
|
1790
|
-
|
1791
|
-
|
1849
|
+
return {
|
1850
|
+
...(disabledIsDefined ? { disabled: options.disabled } : {}),
|
1851
|
+
...(_options.shouldUseNativeValidation
|
1852
|
+
? {
|
1853
|
+
required: !!options.required,
|
1854
|
+
min: getRuleValue(options.min),
|
1855
|
+
max: getRuleValue(options.max),
|
1856
|
+
minLength: getRuleValue(options.minLength),
|
1857
|
+
maxLength: getRuleValue(options.maxLength),
|
1858
|
+
pattern: getRuleValue(options.pattern),
|
1859
|
+
}
|
1860
|
+
: {}),
|
1861
|
+
name,
|
1862
|
+
onChange,
|
1863
|
+
onBlur: onChange,
|
1864
|
+
ref: (ref) => {
|
1792
1865
|
if (ref) {
|
1793
1866
|
register(name, options);
|
1794
1867
|
field = get(_fields, name);
|
@@ -1805,12 +1878,15 @@ function createFormControl(props = {}) {
|
|
1805
1878
|
return;
|
1806
1879
|
}
|
1807
1880
|
set(_fields, name, {
|
1808
|
-
_f:
|
1809
|
-
|
1810
|
-
|
1811
|
-
|
1812
|
-
|
1813
|
-
|
1881
|
+
_f: {
|
1882
|
+
...field._f,
|
1883
|
+
...(radioOrCheckbox
|
1884
|
+
? {
|
1885
|
+
refs: [...refs.filter(live), fieldRef],
|
1886
|
+
ref: { type: fieldRef.type, name },
|
1887
|
+
}
|
1888
|
+
: { ref: fieldRef }),
|
1889
|
+
},
|
1814
1890
|
});
|
1815
1891
|
updateValidAndValue(name, false, undefined, fieldRef);
|
1816
1892
|
}
|
@@ -1823,7 +1899,8 @@ function createFormControl(props = {}) {
|
|
1823
1899
|
!(isNameInFieldArray(_names.array, name) && _stateFlags.action) &&
|
1824
1900
|
_names.unMount.add(name);
|
1825
1901
|
}
|
1826
|
-
}
|
1902
|
+
},
|
1903
|
+
};
|
1827
1904
|
};
|
1828
1905
|
const handleSubmit = (onValid, onInvalid) => async (e) => {
|
1829
1906
|
if (e) {
|
@@ -1854,7 +1931,7 @@ function createFormControl(props = {}) {
|
|
1854
1931
|
}
|
1855
1932
|
else {
|
1856
1933
|
if (onInvalid) {
|
1857
|
-
await onInvalid(
|
1934
|
+
await onInvalid({ ..._formState.errors }, e);
|
1858
1935
|
}
|
1859
1936
|
_options.shouldFocusError &&
|
1860
1937
|
focusFieldBy(_fields, (key) => get(_formState.errors, key), _names.mount);
|
@@ -1897,7 +1974,7 @@ function createFormControl(props = {}) {
|
|
1897
1974
|
unset(_formState.errors, name);
|
1898
1975
|
_proxyFormState.isValid && _updateValid();
|
1899
1976
|
}
|
1900
|
-
_subjects.state.next(
|
1977
|
+
_subjects.state.next({ ..._formState });
|
1901
1978
|
}
|
1902
1979
|
};
|
1903
1980
|
const reset = (formValues, keepStateOptions = {}) => {
|
@@ -1965,7 +2042,10 @@ function createFormControl(props = {}) {
|
|
1965
2042
|
dirtyFields: keepStateOptions.keepDirty
|
1966
2043
|
? _formState.dirtyFields
|
1967
2044
|
: (keepStateOptions.keepDefaultValues && formValues
|
1968
|
-
? Object.entries(formValues).reduce((previous, [key, value]) => (
|
2045
|
+
? Object.entries(formValues).reduce((previous, [key, value]) => ({
|
2046
|
+
...previous,
|
2047
|
+
[key]: value !== get(_defaultValues, key),
|
2048
|
+
}), {})
|
1969
2049
|
: {}),
|
1970
2050
|
touchedFields: keepStateOptions.keepTouched
|
1971
2051
|
? _formState.touchedFields
|
@@ -2027,7 +2107,10 @@ function createFormControl(props = {}) {
|
|
2027
2107
|
return _options;
|
2028
2108
|
},
|
2029
2109
|
set _options(value) {
|
2030
|
-
_options =
|
2110
|
+
_options = {
|
2111
|
+
..._options,
|
2112
|
+
...value,
|
2113
|
+
};
|
2031
2114
|
},
|
2032
2115
|
},
|
2033
2116
|
trigger,
|
@@ -2093,13 +2176,19 @@ function useForm(props = {}) {
|
|
2093
2176
|
_formControl.current.control._options = props;
|
2094
2177
|
}
|
2095
2178
|
else {
|
2096
|
-
_formControl.current =
|
2179
|
+
_formControl.current = {
|
2180
|
+
...createFormControl(props),
|
2181
|
+
formState,
|
2182
|
+
};
|
2097
2183
|
}
|
2098
2184
|
const control = _formControl.current.control;
|
2099
2185
|
const callback = React.useCallback((value) => {
|
2100
2186
|
if (shouldRenderFormState(value, control._proxyFormState, true)) {
|
2101
|
-
control._formState =
|
2102
|
-
|
2187
|
+
control._formState = {
|
2188
|
+
...control._formState,
|
2189
|
+
...value,
|
2190
|
+
};
|
2191
|
+
updateFormState({ ...control._formState });
|
2103
2192
|
}
|
2104
2193
|
}, [control]);
|
2105
2194
|
useSubscribe({
|