@bombillazo/rhf-plus 7.59.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 +145 -119
- 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/generateWatchOutput.d.ts.map +1 -1
- package/dist/react-server.esm.mjs +17 -12
- package/dist/react-server.esm.mjs.map +1 -1
- package/dist/types/form.d.ts +2 -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/useForm.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/dist/utils/cloneObject.d.ts.map +1 -1
- package/package.json +15 -16
- 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';
|
|
@@ -40,9 +39,6 @@ function cloneObject(data) {
|
|
|
40
39
|
if (data instanceof Date) {
|
|
41
40
|
copy = new Date(data);
|
|
42
41
|
}
|
|
43
|
-
else if (data instanceof Set) {
|
|
44
|
-
copy = new Set(data);
|
|
45
|
-
}
|
|
46
42
|
else if (!(isWeb && (data instanceof Blob || isFileListInstance)) &&
|
|
47
43
|
(isArray || isObject(data))) {
|
|
48
44
|
copy = isArray ? [] : {};
|
|
@@ -132,7 +128,7 @@ const INPUT_VALIDATION_RULES = {
|
|
|
132
128
|
validate: 'validate',
|
|
133
129
|
};
|
|
134
130
|
|
|
135
|
-
const HookFormContext =
|
|
131
|
+
const HookFormContext = React.createContext(null);
|
|
136
132
|
HookFormContext.displayName = 'HookFormContext';
|
|
137
133
|
/**
|
|
138
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}.
|
|
@@ -164,7 +160,7 @@ HookFormContext.displayName = 'HookFormContext';
|
|
|
164
160
|
* }
|
|
165
161
|
* ```
|
|
166
162
|
*/
|
|
167
|
-
const useFormContext = () =>
|
|
163
|
+
const useFormContext = () => React.useContext(HookFormContext);
|
|
168
164
|
/**
|
|
169
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}.
|
|
170
166
|
*
|
|
@@ -197,7 +193,7 @@ const useFormContext = () => React__default.useContext(HookFormContext);
|
|
|
197
193
|
*/
|
|
198
194
|
const FormProvider = (props) => {
|
|
199
195
|
const { children, ...data } = props;
|
|
200
|
-
return (
|
|
196
|
+
return (React.createElement(HookFormContext.Provider, { value: data }, children));
|
|
201
197
|
};
|
|
202
198
|
|
|
203
199
|
var getProxyFormState = (formState, control, localProxyFormState, isRoot = true) => {
|
|
@@ -254,8 +250,8 @@ const useIsomorphicLayoutEffect = typeof window !== 'undefined' ? React.useLayou
|
|
|
254
250
|
function useFormState(props) {
|
|
255
251
|
const methods = useFormContext();
|
|
256
252
|
const { control = methods.control, disabled, name, exact } = props || {};
|
|
257
|
-
const [formState, updateFormState] =
|
|
258
|
-
const _localProxyFormState =
|
|
253
|
+
const [formState, updateFormState] = React.useState(control._formState);
|
|
254
|
+
const _localProxyFormState = React.useRef({
|
|
259
255
|
isDirty: false,
|
|
260
256
|
isLoading: false,
|
|
261
257
|
dirtyFields: false,
|
|
@@ -277,10 +273,10 @@ function useFormState(props) {
|
|
|
277
273
|
});
|
|
278
274
|
},
|
|
279
275
|
}), [name, disabled, exact]);
|
|
280
|
-
|
|
276
|
+
React.useEffect(() => {
|
|
281
277
|
_localProxyFormState.current.isValid && control._setValid(true);
|
|
282
278
|
}, [control]);
|
|
283
|
-
return
|
|
279
|
+
return React.useMemo(() => getProxyFormState(formState, control, _localProxyFormState.current, false), [formState, control]);
|
|
284
280
|
}
|
|
285
281
|
|
|
286
282
|
var isString = (value) => typeof value === 'string';
|
|
@@ -291,12 +287,51 @@ var generateWatchOutput = (names, _names, formValues, isGlobal, defaultValue) =>
|
|
|
291
287
|
return get(formValues, names, defaultValue);
|
|
292
288
|
}
|
|
293
289
|
if (Array.isArray(names)) {
|
|
294
|
-
return names.map((fieldName) => (isGlobal && _names.watch.add(fieldName),
|
|
290
|
+
return names.map((fieldName) => (isGlobal && _names.watch.add(fieldName),
|
|
291
|
+
get(formValues, fieldName)));
|
|
295
292
|
}
|
|
296
293
|
isGlobal && (_names.watchAll = true);
|
|
297
294
|
return formValues;
|
|
298
295
|
};
|
|
299
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
|
+
|
|
300
335
|
/**
|
|
301
336
|
* Custom hook to subscribe to field change and isolate re-rendering at the component level.
|
|
302
337
|
*
|
|
@@ -315,19 +350,36 @@ var generateWatchOutput = (names, _names, formValues, isGlobal, defaultValue) =>
|
|
|
315
350
|
*/
|
|
316
351
|
function useWatch(props) {
|
|
317
352
|
const methods = useFormContext();
|
|
318
|
-
const { control = methods.control, name, defaultValue, disabled, exact, } = props || {};
|
|
319
|
-
const _defaultValue =
|
|
320
|
-
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);
|
|
321
360
|
useIsomorphicLayoutEffect(() => control._subscribe({
|
|
322
361
|
name,
|
|
323
362
|
formState: {
|
|
324
363
|
values: true,
|
|
325
364
|
},
|
|
326
365
|
exact,
|
|
327
|
-
callback: (formState) =>
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
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());
|
|
331
383
|
return value;
|
|
332
384
|
}
|
|
333
385
|
|
|
@@ -357,15 +409,16 @@ function useWatch(props) {
|
|
|
357
409
|
*/
|
|
358
410
|
function useController(props) {
|
|
359
411
|
const methods = useFormContext();
|
|
360
|
-
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;
|
|
361
413
|
if (!control) {
|
|
362
414
|
throw new Error('useController: missing `control`. Pass `control` as a prop or provide it via FormProvider.');
|
|
363
415
|
}
|
|
364
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]);
|
|
365
418
|
const value = useWatch({
|
|
366
419
|
control,
|
|
367
420
|
name,
|
|
368
|
-
defaultValue:
|
|
421
|
+
defaultValue: defaultValueMemo,
|
|
369
422
|
exact: true,
|
|
370
423
|
});
|
|
371
424
|
const formState = useFormState({
|
|
@@ -373,13 +426,14 @@ function useController(props) {
|
|
|
373
426
|
name,
|
|
374
427
|
exact: true,
|
|
375
428
|
});
|
|
376
|
-
const _props =
|
|
377
|
-
const _registerProps =
|
|
429
|
+
const _props = React.useRef(props);
|
|
430
|
+
const _registerProps = React.useRef(control.register(name, {
|
|
378
431
|
...props.rules,
|
|
379
432
|
value,
|
|
380
433
|
...(isBoolean(props.disabled) ? { disabled: props.disabled } : {}),
|
|
381
434
|
}));
|
|
382
|
-
|
|
435
|
+
_props.current = props;
|
|
436
|
+
const fieldState = React.useMemo(() => Object.defineProperties({}, {
|
|
383
437
|
invalid: {
|
|
384
438
|
enumerable: true,
|
|
385
439
|
get: () => !!get(formState.errors, name),
|
|
@@ -401,21 +455,21 @@ function useController(props) {
|
|
|
401
455
|
get: () => get(formState.errors, name),
|
|
402
456
|
},
|
|
403
457
|
}), [formState, name]);
|
|
404
|
-
const onChange =
|
|
458
|
+
const onChange = React.useCallback((event) => _registerProps.current.onChange({
|
|
405
459
|
target: {
|
|
406
460
|
value: getEventValue(event),
|
|
407
461
|
name: name,
|
|
408
462
|
},
|
|
409
463
|
type: EVENTS.CHANGE,
|
|
410
464
|
}), [name]);
|
|
411
|
-
const onBlur =
|
|
465
|
+
const onBlur = React.useCallback(() => _registerProps.current.onBlur({
|
|
412
466
|
target: {
|
|
413
467
|
value: get(control._formValues, name),
|
|
414
468
|
name: name,
|
|
415
469
|
},
|
|
416
470
|
type: EVENTS.BLUR,
|
|
417
471
|
}), [name, control._formValues]);
|
|
418
|
-
const ref =
|
|
472
|
+
const ref = React.useCallback((elm) => {
|
|
419
473
|
const field = get(control._fields, name);
|
|
420
474
|
if (field && elm) {
|
|
421
475
|
field._f.ref = {
|
|
@@ -426,7 +480,7 @@ function useController(props) {
|
|
|
426
480
|
};
|
|
427
481
|
}
|
|
428
482
|
}, [control._fields, name]);
|
|
429
|
-
const field =
|
|
483
|
+
const field = React.useMemo(() => ({
|
|
430
484
|
name,
|
|
431
485
|
value,
|
|
432
486
|
...(isBoolean(disabled) || formState.disabled
|
|
@@ -436,7 +490,7 @@ function useController(props) {
|
|
|
436
490
|
onBlur,
|
|
437
491
|
ref,
|
|
438
492
|
}), [name, disabled, formState.disabled, onChange, onBlur, ref, value]);
|
|
439
|
-
|
|
493
|
+
React.useEffect(() => {
|
|
440
494
|
const _shouldUnregisterField = control._options.shouldUnregister || shouldUnregister;
|
|
441
495
|
control.register(name, {
|
|
442
496
|
..._props.current.rules,
|
|
@@ -467,13 +521,13 @@ function useController(props) {
|
|
|
467
521
|
: updateMounted(name, false);
|
|
468
522
|
};
|
|
469
523
|
}, [name, control, isArrayField, shouldUnregister]);
|
|
470
|
-
|
|
524
|
+
React.useEffect(() => {
|
|
471
525
|
control._setDisabledField({
|
|
472
526
|
disabled,
|
|
473
527
|
name,
|
|
474
528
|
});
|
|
475
529
|
}, [disabled, name, control]);
|
|
476
|
-
return
|
|
530
|
+
return React.useMemo(() => ({
|
|
477
531
|
field,
|
|
478
532
|
formState,
|
|
479
533
|
fieldState,
|
|
@@ -569,7 +623,7 @@ const POST_REQUEST = 'post';
|
|
|
569
623
|
*/
|
|
570
624
|
function Form(props) {
|
|
571
625
|
const methods = useFormContext();
|
|
572
|
-
const [mounted, setMounted] =
|
|
626
|
+
const [mounted, setMounted] = React.useState(false);
|
|
573
627
|
const { control = methods.control, onSubmit, children, action, method = POST_REQUEST, headers, encType, onError, render, onSuccess, validateStatus, ...rest } = props;
|
|
574
628
|
const submit = async (event) => {
|
|
575
629
|
let hasError = false;
|
|
@@ -604,7 +658,9 @@ function Form(props) {
|
|
|
604
658
|
method,
|
|
605
659
|
headers: {
|
|
606
660
|
...headers,
|
|
607
|
-
...(encType
|
|
661
|
+
...(encType && encType !== 'multipart/form-data'
|
|
662
|
+
? { 'Content-Type': encType }
|
|
663
|
+
: {}),
|
|
608
664
|
},
|
|
609
665
|
body: shouldStringifySubmissionData ? formDataJson : formData,
|
|
610
666
|
});
|
|
@@ -635,12 +691,12 @@ function Form(props) {
|
|
|
635
691
|
});
|
|
636
692
|
}
|
|
637
693
|
};
|
|
638
|
-
|
|
694
|
+
React.useEffect(() => {
|
|
639
695
|
setMounted(true);
|
|
640
696
|
}, []);
|
|
641
|
-
return render ? (
|
|
697
|
+
return render ? (React.createElement(React.Fragment, null, render({
|
|
642
698
|
submit,
|
|
643
|
-
}))) : (
|
|
699
|
+
}))) : (React.createElement("form", { noValidate: mounted, action: action, method: method, encType: encType, onSubmit: submit, ...rest }, children));
|
|
644
700
|
}
|
|
645
701
|
|
|
646
702
|
var appendErrors = (name, validateAllFieldCriteria, errors, type, message) => validateAllFieldCriteria
|
|
@@ -693,44 +749,6 @@ var createSubject = () => {
|
|
|
693
749
|
};
|
|
694
750
|
};
|
|
695
751
|
|
|
696
|
-
var isPrimitive = (value) => isNullOrUndefined(value) || !isObjectType(value);
|
|
697
|
-
|
|
698
|
-
function deepEqual(object1, object2, _internal_visited = new WeakSet()) {
|
|
699
|
-
if (isPrimitive(object1) || isPrimitive(object2)) {
|
|
700
|
-
return object1 === object2;
|
|
701
|
-
}
|
|
702
|
-
if (isDateObject(object1) && isDateObject(object2)) {
|
|
703
|
-
return object1.getTime() === object2.getTime();
|
|
704
|
-
}
|
|
705
|
-
const keys1 = Object.keys(object1);
|
|
706
|
-
const keys2 = Object.keys(object2);
|
|
707
|
-
if (keys1.length !== keys2.length) {
|
|
708
|
-
return false;
|
|
709
|
-
}
|
|
710
|
-
if (_internal_visited.has(object1) || _internal_visited.has(object2)) {
|
|
711
|
-
return true;
|
|
712
|
-
}
|
|
713
|
-
_internal_visited.add(object1);
|
|
714
|
-
_internal_visited.add(object2);
|
|
715
|
-
for (const key of keys1) {
|
|
716
|
-
const val1 = object1[key];
|
|
717
|
-
if (!keys2.includes(key)) {
|
|
718
|
-
return false;
|
|
719
|
-
}
|
|
720
|
-
if (key !== 'ref') {
|
|
721
|
-
const val2 = object2[key];
|
|
722
|
-
if ((isDateObject(val1) && isDateObject(val2)) ||
|
|
723
|
-
(isObject(val1) && isObject(val2)) ||
|
|
724
|
-
(Array.isArray(val1) && Array.isArray(val2))
|
|
725
|
-
? !deepEqual(val1, val2, _internal_visited)
|
|
726
|
-
: val1 !== val2) {
|
|
727
|
-
return false;
|
|
728
|
-
}
|
|
729
|
-
}
|
|
730
|
-
}
|
|
731
|
-
return true;
|
|
732
|
-
}
|
|
733
|
-
|
|
734
752
|
function deepMerge(target, source) {
|
|
735
753
|
if (isPrimitive(target) || isPrimitive(source)) {
|
|
736
754
|
return source;
|
|
@@ -1332,7 +1350,7 @@ function createFormControl(props = {}) {
|
|
|
1332
1350
|
disabled: _options.disabled || false,
|
|
1333
1351
|
metadata: _options.defaultMetadata || {},
|
|
1334
1352
|
};
|
|
1335
|
-
|
|
1353
|
+
let _fields = {};
|
|
1336
1354
|
let _defaultValues = isObject(_options.defaultValues) || isObject(_options.values)
|
|
1337
1355
|
? cloneObject(_options.defaultValues || _options.values) || {}
|
|
1338
1356
|
: {};
|
|
@@ -1717,7 +1735,7 @@ function createFormControl(props = {}) {
|
|
|
1717
1735
|
? setValues(name, cloneValue, options)
|
|
1718
1736
|
: setFieldValue(name, cloneValue, options);
|
|
1719
1737
|
}
|
|
1720
|
-
isWatched(name, _names) && _subjects.state.next({ ..._formState });
|
|
1738
|
+
isWatched(name, _names) && _subjects.state.next({ ..._formState, name });
|
|
1721
1739
|
_subjects.state.next({
|
|
1722
1740
|
name: _state.mount ? name : undefined,
|
|
1723
1741
|
values: cloneObject(_formValues),
|
|
@@ -1899,7 +1917,8 @@ function createFormControl(props = {}) {
|
|
|
1899
1917
|
};
|
|
1900
1918
|
const watch = (name, defaultValue) => isFunction(name)
|
|
1901
1919
|
? _subjects.state.subscribe({
|
|
1902
|
-
next: (payload) =>
|
|
1920
|
+
next: (payload) => 'values' in payload &&
|
|
1921
|
+
name(_getWatch(undefined, defaultValue), payload),
|
|
1903
1922
|
})
|
|
1904
1923
|
: _getWatch(name, defaultValue, true);
|
|
1905
1924
|
const _subscribe = (props) => _subjects.state.subscribe({
|
|
@@ -1910,6 +1929,7 @@ function createFormControl(props = {}) {
|
|
|
1910
1929
|
values: { ..._formValues },
|
|
1911
1930
|
..._formState,
|
|
1912
1931
|
...formState,
|
|
1932
|
+
defaultValues: _defaultValues,
|
|
1913
1933
|
});
|
|
1914
1934
|
}
|
|
1915
1935
|
},
|
|
@@ -2179,15 +2199,20 @@ function createFormControl(props = {}) {
|
|
|
2179
2199
|
}
|
|
2180
2200
|
}
|
|
2181
2201
|
}
|
|
2182
|
-
|
|
2183
|
-
|
|
2184
|
-
if (!isUndefined(value)) {
|
|
2185
|
-
set(values, fieldName, value);
|
|
2202
|
+
if (keepStateOptions.keepFieldsRef) {
|
|
2203
|
+
for (const fieldName of _names.mount) {
|
|
2186
2204
|
setValue(fieldName, get(values, fieldName));
|
|
2187
2205
|
}
|
|
2188
2206
|
}
|
|
2207
|
+
else {
|
|
2208
|
+
_fields = {};
|
|
2209
|
+
}
|
|
2189
2210
|
}
|
|
2190
|
-
_formValues =
|
|
2211
|
+
_formValues = _options.shouldUnregister
|
|
2212
|
+
? keepStateOptions.keepDefaultValues
|
|
2213
|
+
? cloneObject(_defaultValues)
|
|
2214
|
+
: {}
|
|
2215
|
+
: cloneObject(values);
|
|
2191
2216
|
_subjects.array.next({
|
|
2192
2217
|
values: { ...values },
|
|
2193
2218
|
});
|
|
@@ -2509,28 +2534,26 @@ var updateAt = (fieldValues, index, value) => {
|
|
|
2509
2534
|
function useFieldArray(props) {
|
|
2510
2535
|
const methods = useFormContext();
|
|
2511
2536
|
const { control = methods.control, name, keyName = 'id', shouldUnregister, rules, } = props;
|
|
2512
|
-
const [fields, setFields] =
|
|
2513
|
-
const ids =
|
|
2514
|
-
const _fieldIds =
|
|
2515
|
-
const
|
|
2516
|
-
const _actioned = React__default.useRef(false);
|
|
2517
|
-
_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);
|
|
2518
2541
|
_fieldIds.current = fields;
|
|
2519
2542
|
control._names.array.add(name);
|
|
2520
|
-
rules &&
|
|
2521
|
-
control.register(name, rules);
|
|
2543
|
+
React.useMemo(() => rules &&
|
|
2544
|
+
control.register(name, rules), [control, rules, name]);
|
|
2522
2545
|
useIsomorphicLayoutEffect(() => control._subjects.array.subscribe({
|
|
2523
2546
|
next: ({ values, name: fieldArrayName, }) => {
|
|
2524
|
-
if (fieldArrayName ===
|
|
2525
|
-
const fieldValues = get(values,
|
|
2547
|
+
if (fieldArrayName === name || !fieldArrayName) {
|
|
2548
|
+
const fieldValues = get(values, name);
|
|
2526
2549
|
if (Array.isArray(fieldValues)) {
|
|
2527
2550
|
setFields(fieldValues);
|
|
2528
2551
|
ids.current = fieldValues.map(generateId);
|
|
2529
2552
|
}
|
|
2530
2553
|
}
|
|
2531
2554
|
},
|
|
2532
|
-
}).unsubscribe, [control]);
|
|
2533
|
-
const updateValues =
|
|
2555
|
+
}).unsubscribe, [control, name]);
|
|
2556
|
+
const updateValues = React.useCallback((updatedFieldArrayValues) => {
|
|
2534
2557
|
_actioned.current = true;
|
|
2535
2558
|
control._setFieldArray(name, updatedFieldArrayValues);
|
|
2536
2559
|
}, [control, name]);
|
|
@@ -2619,7 +2642,7 @@ function useFieldArray(props) {
|
|
|
2619
2642
|
setFields([...updatedFieldArrayValues]);
|
|
2620
2643
|
control._setFieldArray(name, [...updatedFieldArrayValues], (data) => data, {}, true, false);
|
|
2621
2644
|
};
|
|
2622
|
-
|
|
2645
|
+
React.useEffect(() => {
|
|
2623
2646
|
control._state.action = false;
|
|
2624
2647
|
isWatched(name, control._names) &&
|
|
2625
2648
|
control._subjects.state.next({
|
|
@@ -2679,7 +2702,7 @@ function useFieldArray(props) {
|
|
|
2679
2702
|
control._setValid();
|
|
2680
2703
|
_actioned.current = false;
|
|
2681
2704
|
}, [fields, name, control]);
|
|
2682
|
-
|
|
2705
|
+
React.useEffect(() => {
|
|
2683
2706
|
!get(control._formValues, name) && control._setFieldArray(name);
|
|
2684
2707
|
return () => {
|
|
2685
2708
|
const updateMounted = (name, value) => {
|
|
@@ -2694,15 +2717,15 @@ function useFieldArray(props) {
|
|
|
2694
2717
|
};
|
|
2695
2718
|
}, [name, control, keyName, shouldUnregister]);
|
|
2696
2719
|
return {
|
|
2697
|
-
swap:
|
|
2698
|
-
move:
|
|
2699
|
-
prepend:
|
|
2700
|
-
append:
|
|
2701
|
-
remove:
|
|
2702
|
-
insert:
|
|
2703
|
-
update:
|
|
2704
|
-
replace:
|
|
2705
|
-
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) => ({
|
|
2706
2729
|
...field,
|
|
2707
2730
|
[keyName]: ids.current[index] || generateId(),
|
|
2708
2731
|
})), [fields, keyName]),
|
|
@@ -2740,9 +2763,9 @@ function useFieldArray(props) {
|
|
|
2740
2763
|
*/
|
|
2741
2764
|
function useForm(props = {}) {
|
|
2742
2765
|
var _a;
|
|
2743
|
-
const _formControl =
|
|
2744
|
-
const _values =
|
|
2745
|
-
const [formState, updateFormState] =
|
|
2766
|
+
const _formControl = React.useRef(undefined);
|
|
2767
|
+
const _values = React.useRef(undefined);
|
|
2768
|
+
const [formState, updateFormState] = React.useState({
|
|
2746
2769
|
isDirty: false,
|
|
2747
2770
|
isValidating: false,
|
|
2748
2771
|
isLoading: props.isLoading || isFunction(props.defaultValues),
|
|
@@ -2797,11 +2820,11 @@ function useForm(props = {}) {
|
|
|
2797
2820
|
control._formState.isReady = true;
|
|
2798
2821
|
return sub;
|
|
2799
2822
|
}, [control]);
|
|
2800
|
-
|
|
2801
|
-
|
|
2823
|
+
React.useEffect(() => control._disableForm(props.disabled), [control, props.disabled]);
|
|
2824
|
+
React.useEffect(() => {
|
|
2802
2825
|
control._updateIsLoading(props.isLoading);
|
|
2803
2826
|
}, [control, props.isLoading]);
|
|
2804
|
-
|
|
2827
|
+
React.useEffect(() => {
|
|
2805
2828
|
if (props.mode) {
|
|
2806
2829
|
control._options.mode = props.mode;
|
|
2807
2830
|
}
|
|
@@ -2809,19 +2832,19 @@ function useForm(props = {}) {
|
|
|
2809
2832
|
control._options.reValidateMode = props.reValidateMode;
|
|
2810
2833
|
}
|
|
2811
2834
|
}, [control, props.mode, props.reValidateMode]);
|
|
2812
|
-
|
|
2835
|
+
React.useEffect(() => {
|
|
2813
2836
|
if (props.errors) {
|
|
2814
2837
|
control._setErrors(props.errors);
|
|
2815
2838
|
control._focusError();
|
|
2816
2839
|
}
|
|
2817
2840
|
}, [control, props.errors]);
|
|
2818
|
-
|
|
2841
|
+
React.useEffect(() => {
|
|
2819
2842
|
props.shouldUnregister &&
|
|
2820
2843
|
control._subjects.state.next({
|
|
2821
2844
|
values: control._getWatch(),
|
|
2822
2845
|
});
|
|
2823
2846
|
}, [control, props.shouldUnregister]);
|
|
2824
|
-
|
|
2847
|
+
React.useEffect(() => {
|
|
2825
2848
|
if (control._proxyFormState.isDirty) {
|
|
2826
2849
|
const isDirty = control._getDirty();
|
|
2827
2850
|
if (isDirty !== formState.isDirty) {
|
|
@@ -2831,9 +2854,12 @@ function useForm(props = {}) {
|
|
|
2831
2854
|
}
|
|
2832
2855
|
}
|
|
2833
2856
|
}, [control, formState.isDirty]);
|
|
2834
|
-
|
|
2857
|
+
React.useEffect(() => {
|
|
2835
2858
|
if (props.values && !deepEqual(props.values, _values.current)) {
|
|
2836
|
-
control._reset(props.values,
|
|
2859
|
+
control._reset(props.values, {
|
|
2860
|
+
keepFieldsRef: true,
|
|
2861
|
+
...control._options.resetOptions,
|
|
2862
|
+
});
|
|
2837
2863
|
_values.current = props.values;
|
|
2838
2864
|
updateFormState((state) => ({ ...state }));
|
|
2839
2865
|
}
|
|
@@ -2841,7 +2867,7 @@ function useForm(props = {}) {
|
|
|
2841
2867
|
control._resetDefaultValues();
|
|
2842
2868
|
}
|
|
2843
2869
|
}, [control, props.values]);
|
|
2844
|
-
|
|
2870
|
+
React.useEffect(() => {
|
|
2845
2871
|
if (!control._state.mount) {
|
|
2846
2872
|
control._setValid();
|
|
2847
2873
|
control._state.mount = true;
|
|
@@ -2852,7 +2878,7 @@ function useForm(props = {}) {
|
|
|
2852
2878
|
}
|
|
2853
2879
|
control._removeUnmounted();
|
|
2854
2880
|
});
|
|
2855
|
-
|
|
2881
|
+
React.useEffect(() => {
|
|
2856
2882
|
if (_formControl.current &&
|
|
2857
2883
|
props.id !== undefined &&
|
|
2858
2884
|
_formControl.current.id !== props.id) {
|