react-hook-form 7.50.1 → 7.51.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 +3 -0
- package/dist/constants.d.ts +0 -13
- package/dist/constants.d.ts.map +1 -1
- package/dist/index.cjs.js +1 -1
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.esm.mjs +50 -27
- 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 +2 -29
- package/dist/logic/createFormControl.d.ts.map +1 -1
- package/dist/logic/getProxyFormState.d.ts +1 -1
- package/dist/logic/getProxyFormState.d.ts.map +1 -1
- package/dist/react-server.esm.mjs.map +1 -1
- package/dist/types/controller.d.ts +1 -0
- package/dist/types/controller.d.ts.map +1 -1
- package/dist/types/form.d.ts +10 -6
- package/dist/types/form.d.ts.map +1 -1
- package/dist/useController.d.ts.map +1 -1
- package/dist/useForm.d.ts +1 -1
- package/dist/useForm.d.ts.map +1 -1
- package/dist/useFormContext.d.ts +2 -2
- package/dist/useFormContext.d.ts.map +1 -1
- package/dist/useFormState.d.ts.map +1 -1
- package/dist/utils/fillBooleanArray.d.ts +1 -1
- package/dist/utils/isSelectInput.d.ts +1 -1
- package/dist/utils/objectHasTruthyValue.d.ts +3 -0
- package/dist/utils/objectHasTruthyValue.d.ts.map +1 -0
- package/dist/utils/omit.d.ts +5 -2
- package/package.json +23 -23
package/dist/index.esm.mjs
CHANGED
@@ -263,6 +263,7 @@ function useFormState(props) {
|
|
263
263
|
isLoading: false,
|
264
264
|
dirtyFields: false,
|
265
265
|
touchedFields: false,
|
266
|
+
validatingFields: false,
|
266
267
|
isValidating: false,
|
267
268
|
isValid: false,
|
268
269
|
errors: false,
|
@@ -490,6 +491,10 @@ function useController(props) {
|
|
490
491
|
enumerable: true,
|
491
492
|
get: () => !!get(formState.touchedFields, name),
|
492
493
|
},
|
494
|
+
isValidating: {
|
495
|
+
enumerable: true,
|
496
|
+
get: () => !!get(formState.validatingFields, name),
|
497
|
+
},
|
493
498
|
error: {
|
494
499
|
enumerable: true,
|
495
500
|
get: () => get(formState.errors, name),
|
@@ -1349,6 +1354,8 @@ var isRadioOrCheckbox = (ref) => isRadioInput(ref) || isCheckBoxInput(ref);
|
|
1349
1354
|
|
1350
1355
|
var live = (ref) => isHTMLElement(ref) && ref.isConnected;
|
1351
1356
|
|
1357
|
+
var objectHasTruthyValue = (value) => isObject(value) && Object.values(value).some((val) => val);
|
1358
|
+
|
1352
1359
|
var objectHasFunction = (data) => {
|
1353
1360
|
for (const key in data) {
|
1354
1361
|
if (isFunction(data[key])) {
|
@@ -1518,7 +1525,7 @@ const defaultOptions = {
|
|
1518
1525
|
reValidateMode: VALIDATION_MODE.onChange,
|
1519
1526
|
shouldFocusError: true,
|
1520
1527
|
};
|
1521
|
-
function createFormControl(props = {}
|
1528
|
+
function createFormControl(props = {}) {
|
1522
1529
|
let _options = {
|
1523
1530
|
...defaultOptions,
|
1524
1531
|
...props,
|
@@ -1534,12 +1541,13 @@ function createFormControl(props = {}, flushRootRender) {
|
|
1534
1541
|
isValid: false,
|
1535
1542
|
touchedFields: {},
|
1536
1543
|
dirtyFields: {},
|
1544
|
+
validatingFields: {},
|
1537
1545
|
errors: _options.errors || {},
|
1538
1546
|
disabled: _options.disabled || false,
|
1539
1547
|
};
|
1540
1548
|
let _fields = {};
|
1541
|
-
let _defaultValues = isObject(_options.
|
1542
|
-
? cloneObject(_options.
|
1549
|
+
let _defaultValues = isObject(_options.defaultValues) || isObject(_options.values)
|
1550
|
+
? cloneObject(_options.defaultValues || _options.values) || {}
|
1543
1551
|
: {};
|
1544
1552
|
let _formValues = _options.shouldUnregister
|
1545
1553
|
? {}
|
@@ -1560,6 +1568,7 @@ function createFormControl(props = {}, flushRootRender) {
|
|
1560
1568
|
const _proxyFormState = {
|
1561
1569
|
isDirty: false,
|
1562
1570
|
dirtyFields: false,
|
1571
|
+
validatingFields: false,
|
1563
1572
|
touchedFields: false,
|
1564
1573
|
isValidating: false,
|
1565
1574
|
isValid: false,
|
@@ -1589,10 +1598,19 @@ function createFormControl(props = {}, flushRootRender) {
|
|
1589
1598
|
}
|
1590
1599
|
}
|
1591
1600
|
};
|
1592
|
-
const _updateIsValidating = (
|
1601
|
+
const _updateIsValidating = (isValidating, names) => {
|
1602
|
+
if (!(_proxyFormState.isValidating || _proxyFormState.validatingFields)) {
|
1603
|
+
return;
|
1604
|
+
}
|
1605
|
+
names.forEach((name) => {
|
1606
|
+
set(_formState.validatingFields, name, isValidating);
|
1607
|
+
});
|
1608
|
+
_formState.isValidating = objectHasTruthyValue(_formState.validatingFields);
|
1593
1609
|
_subjects.state.next({
|
1594
|
-
|
1610
|
+
validatingFields: _formState.validatingFields,
|
1611
|
+
isValidating: _formState.isValidating,
|
1595
1612
|
});
|
1613
|
+
};
|
1596
1614
|
const _updateFieldArray = (name, values = [], method, args, shouldSetValues = true, shouldUpdateFieldsAndState = true) => {
|
1597
1615
|
if (args && method) {
|
1598
1616
|
_state.action = true;
|
@@ -1721,7 +1739,7 @@ function createFormControl(props = {}, flushRootRender) {
|
|
1721
1739
|
};
|
1722
1740
|
_subjects.state.next(updatedFormState);
|
1723
1741
|
}
|
1724
|
-
_updateIsValidating(false);
|
1742
|
+
_updateIsValidating(false, Object.keys(_formState.validatingFields).filter((key) => key === name));
|
1725
1743
|
};
|
1726
1744
|
const _executeSchema = async (name) => _options.resolver(_formValues, _options.context, getResolverOptions(name || _names.mount, _fields, _options.criteriaMode, _options.shouldUseNativeValidation));
|
1727
1745
|
const executeSchemaAndUpdateState = async (names) => {
|
@@ -1877,10 +1895,9 @@ function createFormControl(props = {}, flushRootRender) {
|
|
1877
1895
|
}
|
1878
1896
|
isWatched(name, _names) && _subjects.state.next({ ..._formState });
|
1879
1897
|
_subjects.values.next({
|
1880
|
-
name,
|
1898
|
+
name: _state.mount ? name : undefined,
|
1881
1899
|
values: { ..._formValues },
|
1882
1900
|
});
|
1883
|
-
!_state.mount && flushRootRender();
|
1884
1901
|
};
|
1885
1902
|
const onChange = async (event) => {
|
1886
1903
|
const target = event.target;
|
@@ -1926,7 +1943,7 @@ function createFormControl(props = {}, flushRootRender) {
|
|
1926
1943
|
_subjects.state.next({ name, ...(watched ? {} : fieldState) }));
|
1927
1944
|
}
|
1928
1945
|
!isBlurEvent && watched && _subjects.state.next({ ..._formState });
|
1929
|
-
_updateIsValidating(true);
|
1946
|
+
_updateIsValidating(true, [name]);
|
1930
1947
|
if (_options.resolver) {
|
1931
1948
|
const { errors } = await _executeSchema([name]);
|
1932
1949
|
_updateIsFieldValueUpdated(fieldValue);
|
@@ -1968,7 +1985,7 @@ function createFormControl(props = {}, flushRootRender) {
|
|
1968
1985
|
let isValid;
|
1969
1986
|
let validationResult;
|
1970
1987
|
const fieldNames = convertToArrayPayload(name);
|
1971
|
-
_updateIsValidating(true);
|
1988
|
+
_updateIsValidating(true, fieldNames);
|
1972
1989
|
if (_options.resolver) {
|
1973
1990
|
const errors = await executeSchemaAndUpdateState(isUndefined(name) ? name : fieldNames);
|
1974
1991
|
isValid = isEmptyObject(errors);
|
@@ -2015,6 +2032,7 @@ function createFormControl(props = {}, flushRootRender) {
|
|
2015
2032
|
invalid: !!get((formState || _formState).errors, name),
|
2016
2033
|
isDirty: !!get((formState || _formState).dirtyFields, name),
|
2017
2034
|
isTouched: !!get((formState || _formState).touchedFields, name),
|
2035
|
+
isValidating: !!get((formState || _formState).validatingFields, name),
|
2018
2036
|
error: get((formState || _formState).errors, name),
|
2019
2037
|
});
|
2020
2038
|
const clearErrors = (name) => {
|
@@ -2053,6 +2071,8 @@ function createFormControl(props = {}, flushRootRender) {
|
|
2053
2071
|
!options.keepError && unset(_formState.errors, fieldName);
|
2054
2072
|
!options.keepDirty && unset(_formState.dirtyFields, fieldName);
|
2055
2073
|
!options.keepTouched && unset(_formState.touchedFields, fieldName);
|
2074
|
+
!options.keepIsValidating &&
|
2075
|
+
unset(_formState.validatingFields, fieldName);
|
2056
2076
|
!_options.shouldUnregister &&
|
2057
2077
|
!options.keepDefaultValue &&
|
2058
2078
|
unset(_defaultValues, fieldName);
|
@@ -2252,9 +2272,8 @@ function createFormControl(props = {}, flushRootRender) {
|
|
2252
2272
|
const _reset = (formValues, keepStateOptions = {}) => {
|
2253
2273
|
const updatedValues = formValues ? cloneObject(formValues) : _defaultValues;
|
2254
2274
|
const cloneUpdatedValues = cloneObject(updatedValues);
|
2255
|
-
const
|
2256
|
-
|
2257
|
-
: _defaultValues;
|
2275
|
+
const isEmptyResetValues = isEmptyObject(formValues);
|
2276
|
+
const values = isEmptyResetValues ? _defaultValues : cloneUpdatedValues;
|
2258
2277
|
if (!keepStateOptions.keepDefaultValues) {
|
2259
2278
|
_defaultValues = updatedValues;
|
2260
2279
|
}
|
@@ -2299,14 +2318,13 @@ function createFormControl(props = {}, flushRootRender) {
|
|
2299
2318
|
});
|
2300
2319
|
}
|
2301
2320
|
_names = {
|
2302
|
-
mount: new Set(),
|
2321
|
+
mount: keepStateOptions.keepDirtyValues ? _names.mount : new Set(),
|
2303
2322
|
unMount: new Set(),
|
2304
2323
|
array: new Set(),
|
2305
2324
|
watch: new Set(),
|
2306
2325
|
watchAll: false,
|
2307
2326
|
focus: '',
|
2308
2327
|
};
|
2309
|
-
!_state.mount && flushRootRender();
|
2310
2328
|
_state.mount =
|
2311
2329
|
!_proxyFormState.isValid ||
|
2312
2330
|
!!keepStateOptions.keepIsValid ||
|
@@ -2316,20 +2334,24 @@ function createFormControl(props = {}, flushRootRender) {
|
|
2316
2334
|
submitCount: keepStateOptions.keepSubmitCount
|
2317
2335
|
? _formState.submitCount
|
2318
2336
|
: 0,
|
2319
|
-
isDirty:
|
2320
|
-
?
|
2321
|
-
:
|
2322
|
-
|
2337
|
+
isDirty: isEmptyResetValues
|
2338
|
+
? false
|
2339
|
+
: keepStateOptions.keepDirty
|
2340
|
+
? _formState.isDirty
|
2341
|
+
: !!(keepStateOptions.keepDefaultValues &&
|
2342
|
+
!deepEqual(formValues, _defaultValues)),
|
2323
2343
|
isSubmitted: keepStateOptions.keepIsSubmitted
|
2324
2344
|
? _formState.isSubmitted
|
2325
2345
|
: false,
|
2326
|
-
dirtyFields:
|
2327
|
-
?
|
2328
|
-
|
2329
|
-
|
2330
|
-
|
2331
|
-
|
2332
|
-
:
|
2346
|
+
dirtyFields: isEmptyResetValues
|
2347
|
+
? []
|
2348
|
+
: keepStateOptions.keepDirtyValues
|
2349
|
+
? keepStateOptions.keepDefaultValues && _formValues
|
2350
|
+
? getDirtyFields(_defaultValues, _formValues)
|
2351
|
+
: _formState.dirtyFields
|
2352
|
+
: keepStateOptions.keepDefaultValues && formValues
|
2353
|
+
? getDirtyFields(_defaultValues, formValues)
|
2354
|
+
: {},
|
2333
2355
|
touchedFields: keepStateOptions.keepTouched
|
2334
2356
|
? _formState.touchedFields
|
2335
2357
|
: {},
|
@@ -2487,6 +2509,7 @@ function useForm(props = {}) {
|
|
2487
2509
|
submitCount: 0,
|
2488
2510
|
dirtyFields: {},
|
2489
2511
|
touchedFields: {},
|
2512
|
+
validatingFields: {},
|
2490
2513
|
errors: props.errors || {},
|
2491
2514
|
disabled: props.disabled || false,
|
2492
2515
|
defaultValues: isFunction(props.defaultValues)
|
@@ -2495,7 +2518,7 @@ function useForm(props = {}) {
|
|
2495
2518
|
});
|
2496
2519
|
if (!_formControl.current) {
|
2497
2520
|
_formControl.current = {
|
2498
|
-
...createFormControl(props
|
2521
|
+
...createFormControl(props),
|
2499
2522
|
formState,
|
2500
2523
|
};
|
2501
2524
|
}
|