@bombillazo/rhf-plus 7.60.0-plus.0 → 7.62.0-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/README.md +15 -1
- 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 +134 -111
- 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 +10 -5
- 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/types/utils.d.ts +1 -1
- package/dist/types/utils.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';
|
|
@@ -42,7 +41,7 @@ function cloneObject(data) {
|
|
|
42
41
|
}
|
|
43
42
|
else if (!(isWeb && (data instanceof Blob || isFileListInstance)) &&
|
|
44
43
|
(isArray || isObject(data))) {
|
|
45
|
-
copy = isArray ? [] :
|
|
44
|
+
copy = isArray ? [] : Object.create(Object.getPrototypeOf(data));
|
|
46
45
|
if (!isArray && !isPlainObject(data)) {
|
|
47
46
|
copy = data;
|
|
48
47
|
}
|
|
@@ -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),
|
|
@@ -1750,8 +1770,10 @@ function createFormControl(props = {}) {
|
|
|
1750
1770
|
const watched = isWatched(name, _names, isBlurEvent);
|
|
1751
1771
|
set(_formValues, name, fieldValue);
|
|
1752
1772
|
if (isBlurEvent) {
|
|
1753
|
-
|
|
1754
|
-
|
|
1773
|
+
if (!target || !target.readOnly) {
|
|
1774
|
+
field._f.onBlur && field._f.onBlur(event);
|
|
1775
|
+
delayErrorCallback && delayErrorCallback(0);
|
|
1776
|
+
}
|
|
1755
1777
|
}
|
|
1756
1778
|
else if (field._f.onChange) {
|
|
1757
1779
|
field._f.onChange(event);
|
|
@@ -1897,7 +1919,8 @@ function createFormControl(props = {}) {
|
|
|
1897
1919
|
};
|
|
1898
1920
|
const watch = (name, defaultValue) => isFunction(name)
|
|
1899
1921
|
? _subjects.state.subscribe({
|
|
1900
|
-
next: (payload) =>
|
|
1922
|
+
next: (payload) => 'values' in payload &&
|
|
1923
|
+
name(_getWatch(undefined, defaultValue), payload),
|
|
1901
1924
|
})
|
|
1902
1925
|
: _getWatch(name, defaultValue, true);
|
|
1903
1926
|
const _subscribe = (props) => _subjects.state.subscribe({
|
|
@@ -1908,6 +1931,7 @@ function createFormControl(props = {}) {
|
|
|
1908
1931
|
values: { ..._formValues },
|
|
1909
1932
|
..._formState,
|
|
1910
1933
|
...formState,
|
|
1934
|
+
defaultValues: _defaultValues,
|
|
1911
1935
|
});
|
|
1912
1936
|
}
|
|
1913
1937
|
},
|
|
@@ -2244,6 +2268,7 @@ function createFormControl(props = {}) {
|
|
|
2244
2268
|
? _formState.isSubmitSuccessful
|
|
2245
2269
|
: false,
|
|
2246
2270
|
isSubmitting: false,
|
|
2271
|
+
defaultValues: _defaultValues,
|
|
2247
2272
|
});
|
|
2248
2273
|
};
|
|
2249
2274
|
const reset = (formValues, keepStateOptions) => _reset(isFunction(formValues)
|
|
@@ -2512,28 +2537,26 @@ var updateAt = (fieldValues, index, value) => {
|
|
|
2512
2537
|
function useFieldArray(props) {
|
|
2513
2538
|
const methods = useFormContext();
|
|
2514
2539
|
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;
|
|
2540
|
+
const [fields, setFields] = React.useState(control._getFieldArray(name));
|
|
2541
|
+
const ids = React.useRef(control._getFieldArray(name).map(generateId));
|
|
2542
|
+
const _fieldIds = React.useRef(fields);
|
|
2543
|
+
const _actioned = React.useRef(false);
|
|
2521
2544
|
_fieldIds.current = fields;
|
|
2522
2545
|
control._names.array.add(name);
|
|
2523
|
-
rules &&
|
|
2524
|
-
control.register(name, rules);
|
|
2546
|
+
React.useMemo(() => rules &&
|
|
2547
|
+
control.register(name, rules), [control, rules, name]);
|
|
2525
2548
|
useIsomorphicLayoutEffect(() => control._subjects.array.subscribe({
|
|
2526
2549
|
next: ({ values, name: fieldArrayName, }) => {
|
|
2527
|
-
if (fieldArrayName ===
|
|
2528
|
-
const fieldValues = get(values,
|
|
2550
|
+
if (fieldArrayName === name || !fieldArrayName) {
|
|
2551
|
+
const fieldValues = get(values, name);
|
|
2529
2552
|
if (Array.isArray(fieldValues)) {
|
|
2530
2553
|
setFields(fieldValues);
|
|
2531
2554
|
ids.current = fieldValues.map(generateId);
|
|
2532
2555
|
}
|
|
2533
2556
|
}
|
|
2534
2557
|
},
|
|
2535
|
-
}).unsubscribe, [control]);
|
|
2536
|
-
const updateValues =
|
|
2558
|
+
}).unsubscribe, [control, name]);
|
|
2559
|
+
const updateValues = React.useCallback((updatedFieldArrayValues) => {
|
|
2537
2560
|
_actioned.current = true;
|
|
2538
2561
|
control._setFieldArray(name, updatedFieldArrayValues);
|
|
2539
2562
|
}, [control, name]);
|
|
@@ -2622,7 +2645,7 @@ function useFieldArray(props) {
|
|
|
2622
2645
|
setFields([...updatedFieldArrayValues]);
|
|
2623
2646
|
control._setFieldArray(name, [...updatedFieldArrayValues], (data) => data, {}, true, false);
|
|
2624
2647
|
};
|
|
2625
|
-
|
|
2648
|
+
React.useEffect(() => {
|
|
2626
2649
|
control._state.action = false;
|
|
2627
2650
|
isWatched(name, control._names) &&
|
|
2628
2651
|
control._subjects.state.next({
|
|
@@ -2682,7 +2705,7 @@ function useFieldArray(props) {
|
|
|
2682
2705
|
control._setValid();
|
|
2683
2706
|
_actioned.current = false;
|
|
2684
2707
|
}, [fields, name, control]);
|
|
2685
|
-
|
|
2708
|
+
React.useEffect(() => {
|
|
2686
2709
|
!get(control._formValues, name) && control._setFieldArray(name);
|
|
2687
2710
|
return () => {
|
|
2688
2711
|
const updateMounted = (name, value) => {
|
|
@@ -2697,15 +2720,15 @@ function useFieldArray(props) {
|
|
|
2697
2720
|
};
|
|
2698
2721
|
}, [name, control, keyName, shouldUnregister]);
|
|
2699
2722
|
return {
|
|
2700
|
-
swap:
|
|
2701
|
-
move:
|
|
2702
|
-
prepend:
|
|
2703
|
-
append:
|
|
2704
|
-
remove:
|
|
2705
|
-
insert:
|
|
2706
|
-
update:
|
|
2707
|
-
replace:
|
|
2708
|
-
fields:
|
|
2723
|
+
swap: React.useCallback(swap, [updateValues, name, control]),
|
|
2724
|
+
move: React.useCallback(move, [updateValues, name, control]),
|
|
2725
|
+
prepend: React.useCallback(prepend, [updateValues, name, control]),
|
|
2726
|
+
append: React.useCallback(append, [updateValues, name, control]),
|
|
2727
|
+
remove: React.useCallback(remove, [updateValues, name, control]),
|
|
2728
|
+
insert: React.useCallback(insert$1, [updateValues, name, control]),
|
|
2729
|
+
update: React.useCallback(update, [updateValues, name, control]),
|
|
2730
|
+
replace: React.useCallback(replace, [updateValues, name, control]),
|
|
2731
|
+
fields: React.useMemo(() => fields.map((field, index) => ({
|
|
2709
2732
|
...field,
|
|
2710
2733
|
[keyName]: ids.current[index] || generateId(),
|
|
2711
2734
|
})), [fields, keyName]),
|
|
@@ -2743,9 +2766,9 @@ function useFieldArray(props) {
|
|
|
2743
2766
|
*/
|
|
2744
2767
|
function useForm(props = {}) {
|
|
2745
2768
|
var _a;
|
|
2746
|
-
const _formControl =
|
|
2747
|
-
const _values =
|
|
2748
|
-
const [formState, updateFormState] =
|
|
2769
|
+
const _formControl = React.useRef(undefined);
|
|
2770
|
+
const _values = React.useRef(undefined);
|
|
2771
|
+
const [formState, updateFormState] = React.useState({
|
|
2749
2772
|
isDirty: false,
|
|
2750
2773
|
isValidating: false,
|
|
2751
2774
|
isLoading: props.isLoading || isFunction(props.defaultValues),
|
|
@@ -2800,11 +2823,11 @@ function useForm(props = {}) {
|
|
|
2800
2823
|
control._formState.isReady = true;
|
|
2801
2824
|
return sub;
|
|
2802
2825
|
}, [control]);
|
|
2803
|
-
|
|
2804
|
-
|
|
2826
|
+
React.useEffect(() => control._disableForm(props.disabled), [control, props.disabled]);
|
|
2827
|
+
React.useEffect(() => {
|
|
2805
2828
|
control._updateIsLoading(props.isLoading);
|
|
2806
2829
|
}, [control, props.isLoading]);
|
|
2807
|
-
|
|
2830
|
+
React.useEffect(() => {
|
|
2808
2831
|
if (props.mode) {
|
|
2809
2832
|
control._options.mode = props.mode;
|
|
2810
2833
|
}
|
|
@@ -2812,19 +2835,19 @@ function useForm(props = {}) {
|
|
|
2812
2835
|
control._options.reValidateMode = props.reValidateMode;
|
|
2813
2836
|
}
|
|
2814
2837
|
}, [control, props.mode, props.reValidateMode]);
|
|
2815
|
-
|
|
2838
|
+
React.useEffect(() => {
|
|
2816
2839
|
if (props.errors) {
|
|
2817
2840
|
control._setErrors(props.errors);
|
|
2818
2841
|
control._focusError();
|
|
2819
2842
|
}
|
|
2820
2843
|
}, [control, props.errors]);
|
|
2821
|
-
|
|
2844
|
+
React.useEffect(() => {
|
|
2822
2845
|
props.shouldUnregister &&
|
|
2823
2846
|
control._subjects.state.next({
|
|
2824
2847
|
values: control._getWatch(),
|
|
2825
2848
|
});
|
|
2826
2849
|
}, [control, props.shouldUnregister]);
|
|
2827
|
-
|
|
2850
|
+
React.useEffect(() => {
|
|
2828
2851
|
if (control._proxyFormState.isDirty) {
|
|
2829
2852
|
const isDirty = control._getDirty();
|
|
2830
2853
|
if (isDirty !== formState.isDirty) {
|
|
@@ -2834,7 +2857,7 @@ function useForm(props = {}) {
|
|
|
2834
2857
|
}
|
|
2835
2858
|
}
|
|
2836
2859
|
}, [control, formState.isDirty]);
|
|
2837
|
-
|
|
2860
|
+
React.useEffect(() => {
|
|
2838
2861
|
if (props.values && !deepEqual(props.values, _values.current)) {
|
|
2839
2862
|
control._reset(props.values, {
|
|
2840
2863
|
keepFieldsRef: true,
|
|
@@ -2847,7 +2870,7 @@ function useForm(props = {}) {
|
|
|
2847
2870
|
control._resetDefaultValues();
|
|
2848
2871
|
}
|
|
2849
2872
|
}, [control, props.values]);
|
|
2850
|
-
|
|
2873
|
+
React.useEffect(() => {
|
|
2851
2874
|
if (!control._state.mount) {
|
|
2852
2875
|
control._setValid();
|
|
2853
2876
|
control._state.mount = true;
|
|
@@ -2858,7 +2881,7 @@ function useForm(props = {}) {
|
|
|
2858
2881
|
}
|
|
2859
2882
|
control._removeUnmounted();
|
|
2860
2883
|
});
|
|
2861
|
-
|
|
2884
|
+
React.useEffect(() => {
|
|
2862
2885
|
if (_formControl.current &&
|
|
2863
2886
|
props.id !== undefined &&
|
|
2864
2887
|
_formControl.current.id !== props.id) {
|