@bombillazo/rhf-plus 7.60.0-plus.0 → 7.60.1-plus.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/dist/form.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 +128 -108
- 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/react-server.esm.mjs +4 -2
- package/dist/react-server.esm.mjs.map +1 -1
- package/dist/types/form.d.ts +1 -0
- package/dist/types/form.d.ts.map +1 -1
- package/dist/useController.d.ts.map +1 -1
- package/dist/useFieldArray.d.ts.map +1 -1
- package/dist/useIsomorphicLayoutEffect.d.ts +1 -1
- package/dist/useIsomorphicLayoutEffect.d.ts.map +1 -1
- package/dist/useWatch.d.ts +99 -8
- package/dist/useWatch.d.ts.map +1 -1
- package/package.json +4 -5
- package/dist/__typetest__/__fixtures__/index.d.ts +0 -5
- package/dist/__typetest__/__fixtures__/index.d.ts.map +0 -1
- package/dist/__typetest__/__fixtures__/pathString.d.ts +0 -4
- package/dist/__typetest__/__fixtures__/pathString.d.ts.map +0 -1
- package/dist/__typetest__/__fixtures__/traversable.d.ts +0 -14
- package/dist/__typetest__/__fixtures__/traversable.d.ts.map +0 -1
- package/dist/__typetest__/__fixtures__/tuple.d.ts +0 -15
- package/dist/__typetest__/__fixtures__/tuple.d.ts.map +0 -1
- package/dist/__typetest__/__fixtures__/type.d.ts +0 -12
- package/dist/__typetest__/__fixtures__/type.d.ts.map +0 -1
- package/dist/__typetest__/errors.test-d.d.ts +0 -2
- package/dist/__typetest__/errors.test-d.d.ts.map +0 -1
- package/dist/__typetest__/form.test-d.d.ts +0 -11
- package/dist/__typetest__/form.test-d.d.ts.map +0 -1
- package/dist/__typetest__/path/common.test-d.d.ts +0 -2
- package/dist/__typetest__/path/common.test-d.d.ts.map +0 -1
- package/dist/__typetest__/path/eager.test-d.d.ts +0 -2
- package/dist/__typetest__/path/eager.test-d.d.ts.map +0 -1
- package/dist/__typetest__/use-form-context.test-d.d.ts +0 -2
- package/dist/__typetest__/use-form-context.test-d.d.ts.map +0 -1
- package/dist/__typetest__/util.test-d.d.ts +0 -2
- package/dist/__typetest__/util.test-d.d.ts.map +0 -1
- package/dist/utils/omit.d.ts +0 -3
- package/dist/utils/omit.d.ts.map +0 -1
package/dist/index.esm.mjs
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import
|
|
2
|
-
import React__default from 'react';
|
|
1
|
+
import React from 'react';
|
|
3
2
|
import crypto$1 from 'crypto';
|
|
4
3
|
|
|
5
4
|
var isCheckBoxInput = (element) => element.type === 'checkbox';
|
|
@@ -129,7 +128,7 @@ const INPUT_VALIDATION_RULES = {
|
|
|
129
128
|
validate: 'validate',
|
|
130
129
|
};
|
|
131
130
|
|
|
132
|
-
const HookFormContext =
|
|
131
|
+
const HookFormContext = React.createContext(null);
|
|
133
132
|
HookFormContext.displayName = 'HookFormContext';
|
|
134
133
|
/**
|
|
135
134
|
* 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}.
|
|
@@ -161,7 +160,7 @@ HookFormContext.displayName = 'HookFormContext';
|
|
|
161
160
|
* }
|
|
162
161
|
* ```
|
|
163
162
|
*/
|
|
164
|
-
const useFormContext = () =>
|
|
163
|
+
const useFormContext = () => React.useContext(HookFormContext);
|
|
165
164
|
/**
|
|
166
165
|
* A provider component that propagates the `useForm` methods to all children components via [React Context](https://reactjs.org/docs/context.html) API. To be used with {@link useFormContext}.
|
|
167
166
|
*
|
|
@@ -194,7 +193,7 @@ const useFormContext = () => React__default.useContext(HookFormContext);
|
|
|
194
193
|
*/
|
|
195
194
|
const FormProvider = (props) => {
|
|
196
195
|
const { children, ...data } = props;
|
|
197
|
-
return (
|
|
196
|
+
return (React.createElement(HookFormContext.Provider, { value: data }, children));
|
|
198
197
|
};
|
|
199
198
|
|
|
200
199
|
var getProxyFormState = (formState, control, localProxyFormState, isRoot = true) => {
|
|
@@ -251,8 +250,8 @@ const useIsomorphicLayoutEffect = typeof window !== 'undefined' ? React.useLayou
|
|
|
251
250
|
function useFormState(props) {
|
|
252
251
|
const methods = useFormContext();
|
|
253
252
|
const { control = methods.control, disabled, name, exact } = props || {};
|
|
254
|
-
const [formState, updateFormState] =
|
|
255
|
-
const _localProxyFormState =
|
|
253
|
+
const [formState, updateFormState] = React.useState(control._formState);
|
|
254
|
+
const _localProxyFormState = React.useRef({
|
|
256
255
|
isDirty: false,
|
|
257
256
|
isLoading: false,
|
|
258
257
|
dirtyFields: false,
|
|
@@ -274,10 +273,10 @@ function useFormState(props) {
|
|
|
274
273
|
});
|
|
275
274
|
},
|
|
276
275
|
}), [name, disabled, exact]);
|
|
277
|
-
|
|
276
|
+
React.useEffect(() => {
|
|
278
277
|
_localProxyFormState.current.isValid && control._setValid(true);
|
|
279
278
|
}, [control]);
|
|
280
|
-
return
|
|
279
|
+
return React.useMemo(() => getProxyFormState(formState, control, _localProxyFormState.current, false), [formState, control]);
|
|
281
280
|
}
|
|
282
281
|
|
|
283
282
|
var isString = (value) => typeof value === 'string';
|
|
@@ -295,6 +294,44 @@ var generateWatchOutput = (names, _names, formValues, isGlobal, defaultValue) =>
|
|
|
295
294
|
return formValues;
|
|
296
295
|
};
|
|
297
296
|
|
|
297
|
+
var isPrimitive = (value) => isNullOrUndefined(value) || !isObjectType(value);
|
|
298
|
+
|
|
299
|
+
function deepEqual(object1, object2, _internal_visited = new WeakSet()) {
|
|
300
|
+
if (isPrimitive(object1) || isPrimitive(object2)) {
|
|
301
|
+
return object1 === object2;
|
|
302
|
+
}
|
|
303
|
+
if (isDateObject(object1) && isDateObject(object2)) {
|
|
304
|
+
return object1.getTime() === object2.getTime();
|
|
305
|
+
}
|
|
306
|
+
const keys1 = Object.keys(object1);
|
|
307
|
+
const keys2 = Object.keys(object2);
|
|
308
|
+
if (keys1.length !== keys2.length) {
|
|
309
|
+
return false;
|
|
310
|
+
}
|
|
311
|
+
if (_internal_visited.has(object1) || _internal_visited.has(object2)) {
|
|
312
|
+
return true;
|
|
313
|
+
}
|
|
314
|
+
_internal_visited.add(object1);
|
|
315
|
+
_internal_visited.add(object2);
|
|
316
|
+
for (const key of keys1) {
|
|
317
|
+
const val1 = object1[key];
|
|
318
|
+
if (!keys2.includes(key)) {
|
|
319
|
+
return false;
|
|
320
|
+
}
|
|
321
|
+
if (key !== 'ref') {
|
|
322
|
+
const val2 = object2[key];
|
|
323
|
+
if ((isDateObject(val1) && isDateObject(val2)) ||
|
|
324
|
+
(isObject(val1) && isObject(val2)) ||
|
|
325
|
+
(Array.isArray(val1) && Array.isArray(val2))
|
|
326
|
+
? !deepEqual(val1, val2, _internal_visited)
|
|
327
|
+
: val1 !== val2) {
|
|
328
|
+
return false;
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
return true;
|
|
333
|
+
}
|
|
334
|
+
|
|
298
335
|
/**
|
|
299
336
|
* Custom hook to subscribe to field change and isolate re-rendering at the component level.
|
|
300
337
|
*
|
|
@@ -313,19 +350,36 @@ var generateWatchOutput = (names, _names, formValues, isGlobal, defaultValue) =>
|
|
|
313
350
|
*/
|
|
314
351
|
function useWatch(props) {
|
|
315
352
|
const methods = useFormContext();
|
|
316
|
-
const { control = methods.control, name, defaultValue, disabled, exact, } = props || {};
|
|
317
|
-
const _defaultValue =
|
|
318
|
-
const
|
|
353
|
+
const { control = methods.control, name, defaultValue, disabled, exact, compute, } = props || {};
|
|
354
|
+
const _defaultValue = React.useRef(defaultValue);
|
|
355
|
+
const _compute = React.useRef(compute);
|
|
356
|
+
const _computeFormValues = React.useRef(undefined);
|
|
357
|
+
_compute.current = compute;
|
|
358
|
+
const defaultValueMemo = React.useMemo(() => control._getWatch(name, _defaultValue.current), [control, name]);
|
|
359
|
+
const [value, updateValue] = React.useState(_compute.current ? _compute.current(defaultValueMemo) : defaultValueMemo);
|
|
319
360
|
useIsomorphicLayoutEffect(() => control._subscribe({
|
|
320
361
|
name,
|
|
321
362
|
formState: {
|
|
322
363
|
values: true,
|
|
323
364
|
},
|
|
324
365
|
exact,
|
|
325
|
-
callback: (formState) =>
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
366
|
+
callback: (formState) => {
|
|
367
|
+
if (!disabled) {
|
|
368
|
+
const formValues = generateWatchOutput(name, control._names, formState.values || control._formValues, false, _defaultValue.current);
|
|
369
|
+
if (_compute.current) {
|
|
370
|
+
const computedFormValues = _compute.current(formValues);
|
|
371
|
+
if (!deepEqual(computedFormValues, _computeFormValues.current)) {
|
|
372
|
+
updateValue(computedFormValues);
|
|
373
|
+
_computeFormValues.current = computedFormValues;
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
else {
|
|
377
|
+
updateValue(formValues);
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
},
|
|
381
|
+
}), [control, disabled, name, exact]);
|
|
382
|
+
React.useEffect(() => control._removeUnmounted());
|
|
329
383
|
return value;
|
|
330
384
|
}
|
|
331
385
|
|
|
@@ -355,15 +409,16 @@ function useWatch(props) {
|
|
|
355
409
|
*/
|
|
356
410
|
function useController(props) {
|
|
357
411
|
const methods = useFormContext();
|
|
358
|
-
const { name, disabled, control = methods === null || methods === void 0 ? void 0 : methods.control, shouldUnregister, } = props;
|
|
412
|
+
const { name, disabled, control = methods === null || methods === void 0 ? void 0 : methods.control, shouldUnregister, defaultValue, } = props;
|
|
359
413
|
if (!control) {
|
|
360
414
|
throw new Error('useController: missing `control`. Pass `control` as a prop or provide it via FormProvider.');
|
|
361
415
|
}
|
|
362
416
|
const isArrayField = isNameInFieldArray(control._names.array, name);
|
|
417
|
+
const defaultValueMemo = React.useMemo(() => get(control._formValues, name, get(control._defaultValues, name, defaultValue)), [control, name, defaultValue]);
|
|
363
418
|
const value = useWatch({
|
|
364
419
|
control,
|
|
365
420
|
name,
|
|
366
|
-
defaultValue:
|
|
421
|
+
defaultValue: defaultValueMemo,
|
|
367
422
|
exact: true,
|
|
368
423
|
});
|
|
369
424
|
const formState = useFormState({
|
|
@@ -371,13 +426,14 @@ function useController(props) {
|
|
|
371
426
|
name,
|
|
372
427
|
exact: true,
|
|
373
428
|
});
|
|
374
|
-
const _props =
|
|
375
|
-
const _registerProps =
|
|
429
|
+
const _props = React.useRef(props);
|
|
430
|
+
const _registerProps = React.useRef(control.register(name, {
|
|
376
431
|
...props.rules,
|
|
377
432
|
value,
|
|
378
433
|
...(isBoolean(props.disabled) ? { disabled: props.disabled } : {}),
|
|
379
434
|
}));
|
|
380
|
-
|
|
435
|
+
_props.current = props;
|
|
436
|
+
const fieldState = React.useMemo(() => Object.defineProperties({}, {
|
|
381
437
|
invalid: {
|
|
382
438
|
enumerable: true,
|
|
383
439
|
get: () => !!get(formState.errors, name),
|
|
@@ -399,21 +455,21 @@ function useController(props) {
|
|
|
399
455
|
get: () => get(formState.errors, name),
|
|
400
456
|
},
|
|
401
457
|
}), [formState, name]);
|
|
402
|
-
const onChange =
|
|
458
|
+
const onChange = React.useCallback((event) => _registerProps.current.onChange({
|
|
403
459
|
target: {
|
|
404
460
|
value: getEventValue(event),
|
|
405
461
|
name: name,
|
|
406
462
|
},
|
|
407
463
|
type: EVENTS.CHANGE,
|
|
408
464
|
}), [name]);
|
|
409
|
-
const onBlur =
|
|
465
|
+
const onBlur = React.useCallback(() => _registerProps.current.onBlur({
|
|
410
466
|
target: {
|
|
411
467
|
value: get(control._formValues, name),
|
|
412
468
|
name: name,
|
|
413
469
|
},
|
|
414
470
|
type: EVENTS.BLUR,
|
|
415
471
|
}), [name, control._formValues]);
|
|
416
|
-
const ref =
|
|
472
|
+
const ref = React.useCallback((elm) => {
|
|
417
473
|
const field = get(control._fields, name);
|
|
418
474
|
if (field && elm) {
|
|
419
475
|
field._f.ref = {
|
|
@@ -424,7 +480,7 @@ function useController(props) {
|
|
|
424
480
|
};
|
|
425
481
|
}
|
|
426
482
|
}, [control._fields, name]);
|
|
427
|
-
const field =
|
|
483
|
+
const field = React.useMemo(() => ({
|
|
428
484
|
name,
|
|
429
485
|
value,
|
|
430
486
|
...(isBoolean(disabled) || formState.disabled
|
|
@@ -434,7 +490,7 @@ function useController(props) {
|
|
|
434
490
|
onBlur,
|
|
435
491
|
ref,
|
|
436
492
|
}), [name, disabled, formState.disabled, onChange, onBlur, ref, value]);
|
|
437
|
-
|
|
493
|
+
React.useEffect(() => {
|
|
438
494
|
const _shouldUnregisterField = control._options.shouldUnregister || shouldUnregister;
|
|
439
495
|
control.register(name, {
|
|
440
496
|
..._props.current.rules,
|
|
@@ -465,13 +521,13 @@ function useController(props) {
|
|
|
465
521
|
: updateMounted(name, false);
|
|
466
522
|
};
|
|
467
523
|
}, [name, control, isArrayField, shouldUnregister]);
|
|
468
|
-
|
|
524
|
+
React.useEffect(() => {
|
|
469
525
|
control._setDisabledField({
|
|
470
526
|
disabled,
|
|
471
527
|
name,
|
|
472
528
|
});
|
|
473
529
|
}, [disabled, name, control]);
|
|
474
|
-
return
|
|
530
|
+
return React.useMemo(() => ({
|
|
475
531
|
field,
|
|
476
532
|
formState,
|
|
477
533
|
fieldState,
|
|
@@ -567,7 +623,7 @@ const POST_REQUEST = 'post';
|
|
|
567
623
|
*/
|
|
568
624
|
function Form(props) {
|
|
569
625
|
const methods = useFormContext();
|
|
570
|
-
const [mounted, setMounted] =
|
|
626
|
+
const [mounted, setMounted] = React.useState(false);
|
|
571
627
|
const { control = methods.control, onSubmit, children, action, method = POST_REQUEST, headers, encType, onError, render, onSuccess, validateStatus, ...rest } = props;
|
|
572
628
|
const submit = async (event) => {
|
|
573
629
|
let hasError = false;
|
|
@@ -602,7 +658,9 @@ function Form(props) {
|
|
|
602
658
|
method,
|
|
603
659
|
headers: {
|
|
604
660
|
...headers,
|
|
605
|
-
...(encType
|
|
661
|
+
...(encType && encType !== 'multipart/form-data'
|
|
662
|
+
? { 'Content-Type': encType }
|
|
663
|
+
: {}),
|
|
606
664
|
},
|
|
607
665
|
body: shouldStringifySubmissionData ? formDataJson : formData,
|
|
608
666
|
});
|
|
@@ -633,12 +691,12 @@ function Form(props) {
|
|
|
633
691
|
});
|
|
634
692
|
}
|
|
635
693
|
};
|
|
636
|
-
|
|
694
|
+
React.useEffect(() => {
|
|
637
695
|
setMounted(true);
|
|
638
696
|
}, []);
|
|
639
|
-
return render ? (
|
|
697
|
+
return render ? (React.createElement(React.Fragment, null, render({
|
|
640
698
|
submit,
|
|
641
|
-
}))) : (
|
|
699
|
+
}))) : (React.createElement("form", { noValidate: mounted, action: action, method: method, encType: encType, onSubmit: submit, ...rest }, children));
|
|
642
700
|
}
|
|
643
701
|
|
|
644
702
|
var appendErrors = (name, validateAllFieldCriteria, errors, type, message) => validateAllFieldCriteria
|
|
@@ -691,44 +749,6 @@ var createSubject = () => {
|
|
|
691
749
|
};
|
|
692
750
|
};
|
|
693
751
|
|
|
694
|
-
var isPrimitive = (value) => isNullOrUndefined(value) || !isObjectType(value);
|
|
695
|
-
|
|
696
|
-
function deepEqual(object1, object2, _internal_visited = new WeakSet()) {
|
|
697
|
-
if (isPrimitive(object1) || isPrimitive(object2)) {
|
|
698
|
-
return object1 === object2;
|
|
699
|
-
}
|
|
700
|
-
if (isDateObject(object1) && isDateObject(object2)) {
|
|
701
|
-
return object1.getTime() === object2.getTime();
|
|
702
|
-
}
|
|
703
|
-
const keys1 = Object.keys(object1);
|
|
704
|
-
const keys2 = Object.keys(object2);
|
|
705
|
-
if (keys1.length !== keys2.length) {
|
|
706
|
-
return false;
|
|
707
|
-
}
|
|
708
|
-
if (_internal_visited.has(object1) || _internal_visited.has(object2)) {
|
|
709
|
-
return true;
|
|
710
|
-
}
|
|
711
|
-
_internal_visited.add(object1);
|
|
712
|
-
_internal_visited.add(object2);
|
|
713
|
-
for (const key of keys1) {
|
|
714
|
-
const val1 = object1[key];
|
|
715
|
-
if (!keys2.includes(key)) {
|
|
716
|
-
return false;
|
|
717
|
-
}
|
|
718
|
-
if (key !== 'ref') {
|
|
719
|
-
const val2 = object2[key];
|
|
720
|
-
if ((isDateObject(val1) && isDateObject(val2)) ||
|
|
721
|
-
(isObject(val1) && isObject(val2)) ||
|
|
722
|
-
(Array.isArray(val1) && Array.isArray(val2))
|
|
723
|
-
? !deepEqual(val1, val2, _internal_visited)
|
|
724
|
-
: val1 !== val2) {
|
|
725
|
-
return false;
|
|
726
|
-
}
|
|
727
|
-
}
|
|
728
|
-
}
|
|
729
|
-
return true;
|
|
730
|
-
}
|
|
731
|
-
|
|
732
752
|
function deepMerge(target, source) {
|
|
733
753
|
if (isPrimitive(target) || isPrimitive(source)) {
|
|
734
754
|
return source;
|
|
@@ -1715,7 +1735,7 @@ function createFormControl(props = {}) {
|
|
|
1715
1735
|
? setValues(name, cloneValue, options)
|
|
1716
1736
|
: setFieldValue(name, cloneValue, options);
|
|
1717
1737
|
}
|
|
1718
|
-
isWatched(name, _names) && _subjects.state.next({ ..._formState });
|
|
1738
|
+
isWatched(name, _names) && _subjects.state.next({ ..._formState, name });
|
|
1719
1739
|
_subjects.state.next({
|
|
1720
1740
|
name: _state.mount ? name : undefined,
|
|
1721
1741
|
values: cloneObject(_formValues),
|
|
@@ -1897,7 +1917,8 @@ function createFormControl(props = {}) {
|
|
|
1897
1917
|
};
|
|
1898
1918
|
const watch = (name, defaultValue) => isFunction(name)
|
|
1899
1919
|
? _subjects.state.subscribe({
|
|
1900
|
-
next: (payload) =>
|
|
1920
|
+
next: (payload) => 'values' in payload &&
|
|
1921
|
+
name(_getWatch(undefined, defaultValue), payload),
|
|
1901
1922
|
})
|
|
1902
1923
|
: _getWatch(name, defaultValue, true);
|
|
1903
1924
|
const _subscribe = (props) => _subjects.state.subscribe({
|
|
@@ -1908,6 +1929,7 @@ function createFormControl(props = {}) {
|
|
|
1908
1929
|
values: { ..._formValues },
|
|
1909
1930
|
..._formState,
|
|
1910
1931
|
...formState,
|
|
1932
|
+
defaultValues: _defaultValues,
|
|
1911
1933
|
});
|
|
1912
1934
|
}
|
|
1913
1935
|
},
|
|
@@ -2512,28 +2534,26 @@ var updateAt = (fieldValues, index, value) => {
|
|
|
2512
2534
|
function useFieldArray(props) {
|
|
2513
2535
|
const methods = useFormContext();
|
|
2514
2536
|
const { control = methods.control, name, keyName = 'id', shouldUnregister, rules, } = props;
|
|
2515
|
-
const [fields, setFields] =
|
|
2516
|
-
const ids =
|
|
2517
|
-
const _fieldIds =
|
|
2518
|
-
const
|
|
2519
|
-
const _actioned = React__default.useRef(false);
|
|
2520
|
-
_name.current = name;
|
|
2537
|
+
const [fields, setFields] = React.useState(control._getFieldArray(name));
|
|
2538
|
+
const ids = React.useRef(control._getFieldArray(name).map(generateId));
|
|
2539
|
+
const _fieldIds = React.useRef(fields);
|
|
2540
|
+
const _actioned = React.useRef(false);
|
|
2521
2541
|
_fieldIds.current = fields;
|
|
2522
2542
|
control._names.array.add(name);
|
|
2523
|
-
rules &&
|
|
2524
|
-
control.register(name, rules);
|
|
2543
|
+
React.useMemo(() => rules &&
|
|
2544
|
+
control.register(name, rules), [control, rules, name]);
|
|
2525
2545
|
useIsomorphicLayoutEffect(() => control._subjects.array.subscribe({
|
|
2526
2546
|
next: ({ values, name: fieldArrayName, }) => {
|
|
2527
|
-
if (fieldArrayName ===
|
|
2528
|
-
const fieldValues = get(values,
|
|
2547
|
+
if (fieldArrayName === name || !fieldArrayName) {
|
|
2548
|
+
const fieldValues = get(values, name);
|
|
2529
2549
|
if (Array.isArray(fieldValues)) {
|
|
2530
2550
|
setFields(fieldValues);
|
|
2531
2551
|
ids.current = fieldValues.map(generateId);
|
|
2532
2552
|
}
|
|
2533
2553
|
}
|
|
2534
2554
|
},
|
|
2535
|
-
}).unsubscribe, [control]);
|
|
2536
|
-
const updateValues =
|
|
2555
|
+
}).unsubscribe, [control, name]);
|
|
2556
|
+
const updateValues = React.useCallback((updatedFieldArrayValues) => {
|
|
2537
2557
|
_actioned.current = true;
|
|
2538
2558
|
control._setFieldArray(name, updatedFieldArrayValues);
|
|
2539
2559
|
}, [control, name]);
|
|
@@ -2622,7 +2642,7 @@ function useFieldArray(props) {
|
|
|
2622
2642
|
setFields([...updatedFieldArrayValues]);
|
|
2623
2643
|
control._setFieldArray(name, [...updatedFieldArrayValues], (data) => data, {}, true, false);
|
|
2624
2644
|
};
|
|
2625
|
-
|
|
2645
|
+
React.useEffect(() => {
|
|
2626
2646
|
control._state.action = false;
|
|
2627
2647
|
isWatched(name, control._names) &&
|
|
2628
2648
|
control._subjects.state.next({
|
|
@@ -2682,7 +2702,7 @@ function useFieldArray(props) {
|
|
|
2682
2702
|
control._setValid();
|
|
2683
2703
|
_actioned.current = false;
|
|
2684
2704
|
}, [fields, name, control]);
|
|
2685
|
-
|
|
2705
|
+
React.useEffect(() => {
|
|
2686
2706
|
!get(control._formValues, name) && control._setFieldArray(name);
|
|
2687
2707
|
return () => {
|
|
2688
2708
|
const updateMounted = (name, value) => {
|
|
@@ -2697,15 +2717,15 @@ function useFieldArray(props) {
|
|
|
2697
2717
|
};
|
|
2698
2718
|
}, [name, control, keyName, shouldUnregister]);
|
|
2699
2719
|
return {
|
|
2700
|
-
swap:
|
|
2701
|
-
move:
|
|
2702
|
-
prepend:
|
|
2703
|
-
append:
|
|
2704
|
-
remove:
|
|
2705
|
-
insert:
|
|
2706
|
-
update:
|
|
2707
|
-
replace:
|
|
2708
|
-
fields:
|
|
2720
|
+
swap: React.useCallback(swap, [updateValues, name, control]),
|
|
2721
|
+
move: React.useCallback(move, [updateValues, name, control]),
|
|
2722
|
+
prepend: React.useCallback(prepend, [updateValues, name, control]),
|
|
2723
|
+
append: React.useCallback(append, [updateValues, name, control]),
|
|
2724
|
+
remove: React.useCallback(remove, [updateValues, name, control]),
|
|
2725
|
+
insert: React.useCallback(insert$1, [updateValues, name, control]),
|
|
2726
|
+
update: React.useCallback(update, [updateValues, name, control]),
|
|
2727
|
+
replace: React.useCallback(replace, [updateValues, name, control]),
|
|
2728
|
+
fields: React.useMemo(() => fields.map((field, index) => ({
|
|
2709
2729
|
...field,
|
|
2710
2730
|
[keyName]: ids.current[index] || generateId(),
|
|
2711
2731
|
})), [fields, keyName]),
|
|
@@ -2743,9 +2763,9 @@ function useFieldArray(props) {
|
|
|
2743
2763
|
*/
|
|
2744
2764
|
function useForm(props = {}) {
|
|
2745
2765
|
var _a;
|
|
2746
|
-
const _formControl =
|
|
2747
|
-
const _values =
|
|
2748
|
-
const [formState, updateFormState] =
|
|
2766
|
+
const _formControl = React.useRef(undefined);
|
|
2767
|
+
const _values = React.useRef(undefined);
|
|
2768
|
+
const [formState, updateFormState] = React.useState({
|
|
2749
2769
|
isDirty: false,
|
|
2750
2770
|
isValidating: false,
|
|
2751
2771
|
isLoading: props.isLoading || isFunction(props.defaultValues),
|
|
@@ -2800,11 +2820,11 @@ function useForm(props = {}) {
|
|
|
2800
2820
|
control._formState.isReady = true;
|
|
2801
2821
|
return sub;
|
|
2802
2822
|
}, [control]);
|
|
2803
|
-
|
|
2804
|
-
|
|
2823
|
+
React.useEffect(() => control._disableForm(props.disabled), [control, props.disabled]);
|
|
2824
|
+
React.useEffect(() => {
|
|
2805
2825
|
control._updateIsLoading(props.isLoading);
|
|
2806
2826
|
}, [control, props.isLoading]);
|
|
2807
|
-
|
|
2827
|
+
React.useEffect(() => {
|
|
2808
2828
|
if (props.mode) {
|
|
2809
2829
|
control._options.mode = props.mode;
|
|
2810
2830
|
}
|
|
@@ -2812,19 +2832,19 @@ function useForm(props = {}) {
|
|
|
2812
2832
|
control._options.reValidateMode = props.reValidateMode;
|
|
2813
2833
|
}
|
|
2814
2834
|
}, [control, props.mode, props.reValidateMode]);
|
|
2815
|
-
|
|
2835
|
+
React.useEffect(() => {
|
|
2816
2836
|
if (props.errors) {
|
|
2817
2837
|
control._setErrors(props.errors);
|
|
2818
2838
|
control._focusError();
|
|
2819
2839
|
}
|
|
2820
2840
|
}, [control, props.errors]);
|
|
2821
|
-
|
|
2841
|
+
React.useEffect(() => {
|
|
2822
2842
|
props.shouldUnregister &&
|
|
2823
2843
|
control._subjects.state.next({
|
|
2824
2844
|
values: control._getWatch(),
|
|
2825
2845
|
});
|
|
2826
2846
|
}, [control, props.shouldUnregister]);
|
|
2827
|
-
|
|
2847
|
+
React.useEffect(() => {
|
|
2828
2848
|
if (control._proxyFormState.isDirty) {
|
|
2829
2849
|
const isDirty = control._getDirty();
|
|
2830
2850
|
if (isDirty !== formState.isDirty) {
|
|
@@ -2834,7 +2854,7 @@ function useForm(props = {}) {
|
|
|
2834
2854
|
}
|
|
2835
2855
|
}
|
|
2836
2856
|
}, [control, formState.isDirty]);
|
|
2837
|
-
|
|
2857
|
+
React.useEffect(() => {
|
|
2838
2858
|
if (props.values && !deepEqual(props.values, _values.current)) {
|
|
2839
2859
|
control._reset(props.values, {
|
|
2840
2860
|
keepFieldsRef: true,
|
|
@@ -2847,7 +2867,7 @@ function useForm(props = {}) {
|
|
|
2847
2867
|
control._resetDefaultValues();
|
|
2848
2868
|
}
|
|
2849
2869
|
}, [control, props.values]);
|
|
2850
|
-
|
|
2870
|
+
React.useEffect(() => {
|
|
2851
2871
|
if (!control._state.mount) {
|
|
2852
2872
|
control._setValid();
|
|
2853
2873
|
control._state.mount = true;
|
|
@@ -2858,7 +2878,7 @@ function useForm(props = {}) {
|
|
|
2858
2878
|
}
|
|
2859
2879
|
control._removeUnmounted();
|
|
2860
2880
|
});
|
|
2861
|
-
|
|
2881
|
+
React.useEffect(() => {
|
|
2862
2882
|
if (_formControl.current &&
|
|
2863
2883
|
props.id !== undefined &&
|
|
2864
2884
|
_formControl.current.id !== props.id) {
|