react-hook-form 7.17.4 → 7.19.0-next.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/CHANGELOG.md +24 -0
- package/README.md +23 -6
- package/dist/controller.d.ts +2 -2
- package/dist/index.cjs.js +1 -1
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.esm.js +204 -157
- package/dist/index.esm.js.map +1 -1
- package/dist/index.umd.js +1 -1
- package/dist/index.umd.js.map +1 -1
- package/dist/logic/generateWatchOutput.d.ts +2 -0
- package/dist/logic/schemaErrorLookup.d.ts +5 -0
- package/dist/logic/shouldSubscribeByName.d.ts +1 -1
- package/dist/types/controller.d.ts +10 -10
- package/dist/types/fieldArray.d.ts +1 -1
- package/dist/types/form.d.ts +17 -11
- package/dist/types/utils.d.ts +3 -0
- package/dist/useController.d.ts +2 -2
- package/dist/useSubscribe.d.ts +2 -3
- package/dist/utils/createSubject.d.ts +13 -0
- package/package.json +14 -14
- package/dist/utils/subject.d.ts +0 -24
package/dist/index.esm.js
CHANGED
@@ -105,37 +105,35 @@ var convertToArrayPayload = (value) => (Array.isArray(value) ? value : [value]);
|
|
105
105
|
|
106
106
|
var shouldSubscribeByName = (name, signalName) => !name ||
|
107
107
|
!signalName ||
|
108
|
+
name === signalName ||
|
108
109
|
convertToArrayPayload(name).some((currentName) => currentName &&
|
109
110
|
(currentName.startsWith(signalName) ||
|
110
111
|
signalName.startsWith(currentName)));
|
111
112
|
|
112
|
-
const tearDown = (
|
113
|
-
if (
|
114
|
-
|
115
|
-
|
113
|
+
const tearDown = (_subscription) => {
|
114
|
+
if (_subscription.current) {
|
115
|
+
_subscription.current.unsubscribe();
|
116
|
+
_subscription.current = undefined;
|
116
117
|
}
|
117
118
|
};
|
118
|
-
const updateSubscriptionProps = ({
|
119
|
+
const updateSubscriptionProps = ({ _subscription, props }) => {
|
119
120
|
if (props.disabled) {
|
120
|
-
tearDown(
|
121
|
+
tearDown(_subscription);
|
121
122
|
}
|
122
|
-
else if (!
|
123
|
-
|
123
|
+
else if (!_subscription.current) {
|
124
|
+
_subscription.current = props.subject.subscribe({
|
124
125
|
next: props.callback,
|
125
126
|
});
|
126
127
|
}
|
127
128
|
};
|
128
129
|
function useSubscribe(props) {
|
129
|
-
const
|
130
|
-
|
131
|
-
|
132
|
-
_unsubscribe,
|
130
|
+
const _subscription = React.useRef();
|
131
|
+
updateSubscriptionProps({
|
132
|
+
_subscription,
|
133
133
|
props,
|
134
134
|
});
|
135
|
-
!props.skipEarlySubscription && _updateSubscription.current();
|
136
135
|
React.useEffect(() => {
|
137
|
-
|
138
|
-
return () => tearDown(_unsubscribe);
|
136
|
+
return () => tearDown(_subscription);
|
139
137
|
}, []);
|
140
138
|
}
|
141
139
|
|
@@ -163,29 +161,71 @@ function useFormState(props) {
|
|
163
161
|
return getProxyFormState(formState, control._proxyFormState, _localProxyFormState.current, false);
|
164
162
|
}
|
165
163
|
|
164
|
+
var isString = (value) => typeof value === 'string';
|
165
|
+
|
166
|
+
function generateWatchOutput(names, _names, formValues, isGlobal) {
|
167
|
+
const isArray = Array.isArray(names);
|
168
|
+
if (isString(names)) {
|
169
|
+
isGlobal && _names.watch.add(names);
|
170
|
+
return get(formValues, names);
|
171
|
+
}
|
172
|
+
if (isArray) {
|
173
|
+
return names.map((fieldName) => (isGlobal && _names.watch.add(fieldName),
|
174
|
+
get(formValues, fieldName)));
|
175
|
+
}
|
176
|
+
isGlobal && (_names.watchAll = true);
|
177
|
+
return formValues;
|
178
|
+
}
|
179
|
+
|
180
|
+
function useWatch(props) {
|
181
|
+
const methods = useFormContext();
|
182
|
+
const { control = methods.control, name, defaultValue, disabled, } = props || {};
|
183
|
+
const _name = React.useRef(name);
|
184
|
+
_name.current = name;
|
185
|
+
useSubscribe({
|
186
|
+
disabled,
|
187
|
+
subject: control._subjects.watch,
|
188
|
+
callback: (formState) => {
|
189
|
+
if (shouldSubscribeByName(_name.current, formState.name)) {
|
190
|
+
const fieldValues = generateWatchOutput(_name.current, control._names, control._formValues);
|
191
|
+
updateValue(isObject(fieldValues)
|
192
|
+
? Object.assign({}, fieldValues) : Array.isArray(fieldValues)
|
193
|
+
? [...fieldValues]
|
194
|
+
: fieldValues);
|
195
|
+
}
|
196
|
+
},
|
197
|
+
});
|
198
|
+
const [value, updateValue] = React.useState(isUndefined(defaultValue)
|
199
|
+
? control._getWatch(name)
|
200
|
+
: defaultValue);
|
201
|
+
React.useEffect(() => {
|
202
|
+
control._removeUnmounted();
|
203
|
+
});
|
204
|
+
return value;
|
205
|
+
}
|
206
|
+
|
166
207
|
function useController(props) {
|
167
208
|
const methods = useFormContext();
|
168
209
|
const { name, control = methods.control, shouldUnregister } = props;
|
169
|
-
const
|
210
|
+
const value = useWatch({
|
211
|
+
control,
|
212
|
+
name,
|
213
|
+
defaultValue: get(control._formValues, name, get(control._defaultValues, name, props.defaultValue)),
|
214
|
+
});
|
170
215
|
const formState = useFormState({
|
171
|
-
control
|
216
|
+
control,
|
172
217
|
name,
|
173
218
|
});
|
174
219
|
const _name = React.useRef(name);
|
175
220
|
_name.current = name;
|
176
|
-
useSubscribe({
|
177
|
-
subject: control._subjects.control,
|
178
|
-
callback: (data) => (!data.name || _name.current === data.name) &&
|
179
|
-
setInputStateValue(get(data.values, _name.current)),
|
180
|
-
});
|
181
221
|
const registerProps = control.register(name, Object.assign(Object.assign({}, props.rules), { value }));
|
182
|
-
const updateMounted = React.useCallback((name, value) => {
|
183
|
-
const field = get(control._fields, name);
|
184
|
-
if (field) {
|
185
|
-
field._f.mount = value;
|
186
|
-
}
|
187
|
-
}, [control]);
|
188
222
|
React.useEffect(() => {
|
223
|
+
const updateMounted = (name, value) => {
|
224
|
+
const field = get(control._fields, name);
|
225
|
+
if (field) {
|
226
|
+
field._f.mount = value;
|
227
|
+
}
|
228
|
+
};
|
189
229
|
updateMounted(name, true);
|
190
230
|
return () => {
|
191
231
|
const _shouldUnregisterField = control._options.shouldUnregister || shouldUnregister;
|
@@ -198,15 +238,13 @@ function useController(props) {
|
|
198
238
|
updateMounted(name, false);
|
199
239
|
}
|
200
240
|
};
|
201
|
-
}, [name, control, shouldUnregister
|
241
|
+
}, [name, control, shouldUnregister]);
|
202
242
|
return {
|
203
243
|
field: {
|
204
244
|
onChange: (event) => {
|
205
|
-
const value = getControllerValue(event);
|
206
|
-
setInputStateValue(value);
|
207
245
|
registerProps.onChange({
|
208
246
|
target: {
|
209
|
-
value,
|
247
|
+
value: getControllerValue(event),
|
210
248
|
name: name,
|
211
249
|
},
|
212
250
|
type: EVENTS.CHANGE,
|
@@ -377,6 +415,7 @@ const useFieldArray = (props) => {
|
|
377
415
|
const [fields, setFields] = React.useState(mapIds(control._getFieldArray(name), keyName));
|
378
416
|
const _fieldIds = React.useRef(fields);
|
379
417
|
const _name = React.useRef(name);
|
418
|
+
const _actioned = React.useRef(false);
|
380
419
|
_name.current = name;
|
381
420
|
_fieldIds.current = fields;
|
382
421
|
control._names.array.add(name);
|
@@ -387,10 +426,10 @@ const useFieldArray = (props) => {
|
|
387
426
|
}
|
388
427
|
},
|
389
428
|
subject: control._subjects.array,
|
390
|
-
skipEarlySubscription: true,
|
391
429
|
});
|
392
430
|
const updateValues = React.useCallback((updatedFieldArrayValuesWithKey) => {
|
393
431
|
const updatedFieldArrayValues = omitKeys(updatedFieldArrayValuesWithKey, keyName);
|
432
|
+
_actioned.current = true;
|
394
433
|
set(control._formValues, name, updatedFieldArrayValues);
|
395
434
|
setFields(updatedFieldArrayValuesWithKey);
|
396
435
|
return updatedFieldArrayValues;
|
@@ -466,6 +505,17 @@ const useFieldArray = (props) => {
|
|
466
505
|
}
|
467
506
|
}
|
468
507
|
}
|
508
|
+
if (_actioned.current) {
|
509
|
+
control._executeSchema([name]).then((result) => {
|
510
|
+
const error = get(result.errors, name);
|
511
|
+
if (error && error.type && !get(control._formState.errors, name)) {
|
512
|
+
set(control._formState.errors, name, error);
|
513
|
+
control._subjects.state.next({
|
514
|
+
errors: control._formState.errors,
|
515
|
+
});
|
516
|
+
}
|
517
|
+
});
|
518
|
+
}
|
469
519
|
control._subjects.watch.next({
|
470
520
|
name,
|
471
521
|
values: control._formValues,
|
@@ -523,6 +573,34 @@ function cloneObject(data) {
|
|
523
573
|
return copy;
|
524
574
|
}
|
525
575
|
|
576
|
+
function createSubject() {
|
577
|
+
let _observers = [];
|
578
|
+
const next = (value) => {
|
579
|
+
for (const observer of _observers) {
|
580
|
+
observer.next(value);
|
581
|
+
}
|
582
|
+
};
|
583
|
+
const subscribe = (observer) => {
|
584
|
+
_observers.push(observer);
|
585
|
+
return {
|
586
|
+
unsubscribe: () => {
|
587
|
+
_observers = _observers.filter((o) => o !== observer);
|
588
|
+
},
|
589
|
+
};
|
590
|
+
};
|
591
|
+
const unsubscribe = () => {
|
592
|
+
_observers = [];
|
593
|
+
};
|
594
|
+
return {
|
595
|
+
get observers() {
|
596
|
+
return _observers;
|
597
|
+
},
|
598
|
+
next,
|
599
|
+
subscribe,
|
600
|
+
unsubscribe,
|
601
|
+
};
|
602
|
+
}
|
603
|
+
|
526
604
|
var isPrimitive = (value) => isNullOrUndefined(value) || !isObjectType(value);
|
527
605
|
|
528
606
|
function deepEqual(object1, object2) {
|
@@ -566,6 +644,8 @@ var getValidationModes = (mode) => ({
|
|
566
644
|
|
567
645
|
var isBoolean = (value) => typeof value === 'boolean';
|
568
646
|
|
647
|
+
var isFileInput = (element) => element.type === 'file';
|
648
|
+
|
569
649
|
var isHTMLElement = (value) => value instanceof HTMLElement;
|
570
650
|
|
571
651
|
var isMultipleSelect = (element) => element.type === `select-multiple`;
|
@@ -574,60 +654,12 @@ var isRadioInput = (element) => element.type === 'radio';
|
|
574
654
|
|
575
655
|
var isRadioOrCheckboxFunction = (ref) => isRadioInput(ref) || isCheckBoxInput(ref);
|
576
656
|
|
577
|
-
var isString = (value) => typeof value === 'string';
|
578
|
-
|
579
657
|
var isWeb = typeof window !== 'undefined' &&
|
580
658
|
typeof window.HTMLElement !== 'undefined' &&
|
581
659
|
typeof document !== 'undefined';
|
582
660
|
|
583
661
|
var live = (ref) => isHTMLElement(ref) && document.contains(ref);
|
584
662
|
|
585
|
-
class Subscription {
|
586
|
-
constructor() {
|
587
|
-
this.tearDowns = [];
|
588
|
-
}
|
589
|
-
add(tearDown) {
|
590
|
-
this.tearDowns.push(tearDown);
|
591
|
-
}
|
592
|
-
unsubscribe() {
|
593
|
-
for (const teardown of this.tearDowns) {
|
594
|
-
teardown();
|
595
|
-
}
|
596
|
-
this.tearDowns = [];
|
597
|
-
}
|
598
|
-
}
|
599
|
-
class Subscriber {
|
600
|
-
constructor(observer, subscription) {
|
601
|
-
this.observer = observer;
|
602
|
-
this.closed = false;
|
603
|
-
subscription.add(() => (this.closed = true));
|
604
|
-
}
|
605
|
-
next(value) {
|
606
|
-
if (!this.closed) {
|
607
|
-
this.observer.next(value);
|
608
|
-
}
|
609
|
-
}
|
610
|
-
}
|
611
|
-
class Subject {
|
612
|
-
constructor() {
|
613
|
-
this.observers = [];
|
614
|
-
}
|
615
|
-
next(value) {
|
616
|
-
for (const observer of this.observers) {
|
617
|
-
observer.next(value);
|
618
|
-
}
|
619
|
-
}
|
620
|
-
subscribe(observer) {
|
621
|
-
const subscription = new Subscription();
|
622
|
-
const subscriber = new Subscriber(observer, subscription);
|
623
|
-
this.observers.push(subscriber);
|
624
|
-
return subscription;
|
625
|
-
}
|
626
|
-
unsubscribe() {
|
627
|
-
this.observers = [];
|
628
|
-
}
|
629
|
-
}
|
630
|
-
|
631
663
|
function baseGet(object, updatePath) {
|
632
664
|
const length = updatePath.slice(0, -1).length;
|
633
665
|
let index = 0;
|
@@ -667,8 +699,6 @@ function unset(object, path) {
|
|
667
699
|
return object;
|
668
700
|
}
|
669
701
|
|
670
|
-
var isFileInput = (element) => element.type === 'file';
|
671
|
-
|
672
702
|
const defaultResult = {
|
673
703
|
value: false,
|
674
704
|
isValid: false,
|
@@ -762,6 +792,35 @@ var hasValidation = (options) => options.mount &&
|
|
762
792
|
options.pattern ||
|
763
793
|
options.validate);
|
764
794
|
|
795
|
+
function schemaErrorLookup(errors, _fields, name) {
|
796
|
+
const error = get(errors, name);
|
797
|
+
if (error || isKey(name)) {
|
798
|
+
return {
|
799
|
+
error,
|
800
|
+
name,
|
801
|
+
};
|
802
|
+
}
|
803
|
+
const names = name.split('.');
|
804
|
+
while (names.length) {
|
805
|
+
const fieldName = names.join('.');
|
806
|
+
const field = get(_fields, fieldName);
|
807
|
+
const foundError = get(errors, fieldName);
|
808
|
+
if (field && !Array.isArray(field) && name !== fieldName) {
|
809
|
+
return { name };
|
810
|
+
}
|
811
|
+
if (foundError && foundError.type) {
|
812
|
+
return {
|
813
|
+
name: fieldName,
|
814
|
+
error: foundError,
|
815
|
+
};
|
816
|
+
}
|
817
|
+
names.pop();
|
818
|
+
}
|
819
|
+
return {
|
820
|
+
name,
|
821
|
+
};
|
822
|
+
}
|
823
|
+
|
765
824
|
function deepMerge(target, source) {
|
766
825
|
if (isPrimitive(target) || isPrimitive(source)) {
|
767
826
|
return source;
|
@@ -1031,10 +1090,9 @@ function createFormControl(props = {}) {
|
|
1031
1090
|
errors: false,
|
1032
1091
|
};
|
1033
1092
|
const _subjects = {
|
1034
|
-
watch:
|
1035
|
-
|
1036
|
-
|
1037
|
-
state: new Subject(),
|
1093
|
+
watch: createSubject(),
|
1094
|
+
array: createSubject(),
|
1095
|
+
state: createSubject(),
|
1038
1096
|
};
|
1039
1097
|
const validationModeBeforeSubmit = getValidationModes(_options.mode);
|
1040
1098
|
const validationModeAfterSubmit = getValidationModes(_options.reValidateMode);
|
@@ -1051,7 +1109,7 @@ function createFormControl(props = {}) {
|
|
1051
1109
|
let isValid = false;
|
1052
1110
|
if (_proxyFormState.isValid) {
|
1053
1111
|
isValid = _options.resolver
|
1054
|
-
? isEmptyObject((await
|
1112
|
+
? isEmptyObject((await _executeSchema()).errors)
|
1055
1113
|
: await executeBuildInValidation(_fields, true);
|
1056
1114
|
if (!shouldSkipRender && isValid !== _formState.isValid) {
|
1057
1115
|
_formState.isValid = isValid;
|
@@ -1170,11 +1228,11 @@ function createFormControl(props = {}) {
|
|
1170
1228
|
validateFields = {};
|
1171
1229
|
}
|
1172
1230
|
};
|
1173
|
-
const
|
1231
|
+
const _executeSchema = async (name) => _options.resolver
|
1174
1232
|
? await _options.resolver(Object.assign({}, _formValues), _options.context, getResolverOptions(name || _names.mount, _fields, _options.criteriaMode, _options.shouldUseNativeValidation))
|
1175
1233
|
: {};
|
1176
|
-
const
|
1177
|
-
const { errors } = await
|
1234
|
+
const executeSchemaAndUpdateState = async (names) => {
|
1235
|
+
const { errors } = await _executeSchema();
|
1178
1236
|
if (names) {
|
1179
1237
|
for (const name of names) {
|
1180
1238
|
const error = get(errors, name);
|
@@ -1237,16 +1295,10 @@ function createFormControl(props = {}) {
|
|
1237
1295
|
: isString(names)
|
1238
1296
|
? { [names]: defaultValue }
|
1239
1297
|
: defaultValue));
|
1240
|
-
|
1241
|
-
const result = convertToArrayPayload(names).map((fieldName) => (isGlobal && _names.watch.add(fieldName),
|
1242
|
-
get(fieldValues, fieldName)));
|
1243
|
-
return Array.isArray(names) ? result : result[0];
|
1244
|
-
}
|
1245
|
-
isGlobal && (_names.watchAll = true);
|
1246
|
-
return fieldValues;
|
1298
|
+
return generateWatchOutput(names, _names, fieldValues, isGlobal);
|
1247
1299
|
};
|
1248
1300
|
const _getFieldArray = (name) => get(_stateFlags.mount ? _formValues : _defaultValues, name, props.shouldUnregister ? get(_defaultValues, name, []) : []);
|
1249
|
-
const setFieldValue = (name, value, options = {}
|
1301
|
+
const setFieldValue = (name, value, options = {}) => {
|
1250
1302
|
const field = get(_fields, name);
|
1251
1303
|
let fieldValue = value;
|
1252
1304
|
if (field) {
|
@@ -1257,7 +1309,10 @@ function createFormControl(props = {}) {
|
|
1257
1309
|
isWeb && isHTMLElement(fieldReference.ref) && isNullOrUndefined(value)
|
1258
1310
|
? ''
|
1259
1311
|
: value;
|
1260
|
-
if (
|
1312
|
+
if (isFileInput(fieldReference.ref) && !isString(fieldValue)) {
|
1313
|
+
fieldReference.ref.files = fieldValue;
|
1314
|
+
}
|
1315
|
+
else if (isMultipleSelect(fieldReference.ref)) {
|
1261
1316
|
[...fieldReference.ref.options].forEach((selectRef) => (selectRef.selected = fieldValue.includes(selectRef.value)));
|
1262
1317
|
}
|
1263
1318
|
else if (fieldReference.refs) {
|
@@ -1275,11 +1330,6 @@ function createFormControl(props = {}) {
|
|
1275
1330
|
else {
|
1276
1331
|
fieldReference.ref.value = fieldValue;
|
1277
1332
|
}
|
1278
|
-
shouldRender &&
|
1279
|
-
_subjects.control.next({
|
1280
|
-
values: _formValues,
|
1281
|
-
name,
|
1282
|
-
});
|
1283
1333
|
}
|
1284
1334
|
}
|
1285
1335
|
(options.shouldDirty || options.shouldTouch) &&
|
@@ -1296,7 +1346,7 @@ function createFormControl(props = {}) {
|
|
1296
1346
|
(field && !field._f)) &&
|
1297
1347
|
!isDateObject(fieldValue)
|
1298
1348
|
? setValues(fieldName, fieldValue, options)
|
1299
|
-
: setFieldValue(fieldName, fieldValue, options
|
1349
|
+
: setFieldValue(fieldName, fieldValue, options);
|
1300
1350
|
}
|
1301
1351
|
};
|
1302
1352
|
const setValue = (name, value, options = {}) => {
|
@@ -1321,7 +1371,7 @@ function createFormControl(props = {}) {
|
|
1321
1371
|
else {
|
1322
1372
|
field && !field._f && !isNullOrUndefined(value)
|
1323
1373
|
? setValues(name, value, options)
|
1324
|
-
: setFieldValue(name, value, options
|
1374
|
+
: setFieldValue(name, value, options);
|
1325
1375
|
}
|
1326
1376
|
isFieldWatched(name) && _subjects.state.next({});
|
1327
1377
|
_subjects.watch.next({
|
@@ -1368,18 +1418,11 @@ function createFormControl(props = {}) {
|
|
1368
1418
|
isValidating: true,
|
1369
1419
|
});
|
1370
1420
|
if (_options.resolver) {
|
1371
|
-
const { errors } = await
|
1372
|
-
|
1373
|
-
|
1374
|
-
|
1375
|
-
|
1376
|
-
if (Array.isArray(parentField) &&
|
1377
|
-
parentField.every((field) => field._f && isCheckBoxInput(field._f.ref))) {
|
1378
|
-
const parentError = get(errors, parentNodeName, {});
|
1379
|
-
parentError.type && (error = parentError);
|
1380
|
-
name = parentNodeName;
|
1381
|
-
}
|
1382
|
-
}
|
1421
|
+
const { errors } = await _executeSchema([name]);
|
1422
|
+
const previousErrorLookupResult = schemaErrorLookup(_formState.errors, _fields, name);
|
1423
|
+
const errorLookupResult = schemaErrorLookup(errors, _fields, previousErrorLookupResult.name || name);
|
1424
|
+
error = errorLookupResult.error;
|
1425
|
+
name = errorLookupResult.name;
|
1383
1426
|
isValid = isEmptyObject(errors);
|
1384
1427
|
}
|
1385
1428
|
else {
|
@@ -1398,7 +1441,7 @@ function createFormControl(props = {}) {
|
|
1398
1441
|
isValidating: true,
|
1399
1442
|
});
|
1400
1443
|
if (_options.resolver) {
|
1401
|
-
const errors = await
|
1444
|
+
const errors = await executeSchemaAndUpdateState(isUndefined(name) ? name : fieldNames);
|
1402
1445
|
isValid = isEmptyObject(errors);
|
1403
1446
|
validationResult = name
|
1404
1447
|
? !fieldNames.some((name) => get(errors, name))
|
@@ -1414,7 +1457,10 @@ function createFormControl(props = {}) {
|
|
1414
1457
|
else {
|
1415
1458
|
validationResult = isValid = await executeBuildInValidation(_fields);
|
1416
1459
|
}
|
1417
|
-
_subjects.state.next(Object.assign(Object.assign({}, (!isString(name) ||
|
1460
|
+
_subjects.state.next(Object.assign(Object.assign(Object.assign({}, (!isString(name) ||
|
1461
|
+
(_proxyFormState.isValid && isValid !== _formState.isValid)
|
1462
|
+
? {}
|
1463
|
+
: { name })), (_options.resolver ? { isValid } : {})), { errors: _formState.errors, isValidating: false }));
|
1418
1464
|
options.shouldFocus &&
|
1419
1465
|
!validationResult &&
|
1420
1466
|
focusFieldBy(_fields, (key) => get(_formState.errors, key), name ? fieldNames : _names.mount);
|
@@ -1434,6 +1480,7 @@ function createFormControl(props = {}) {
|
|
1434
1480
|
: (_formState.errors = {});
|
1435
1481
|
_subjects.state.next({
|
1436
1482
|
errors: _formState.errors,
|
1483
|
+
isValid: true,
|
1437
1484
|
});
|
1438
1485
|
};
|
1439
1486
|
const setError = (name, error, options) => {
|
@@ -1478,7 +1525,8 @@ function createFormControl(props = {}) {
|
|
1478
1525
|
_f: Object.assign(Object.assign(Object.assign({}, (field && field._f ? field._f : { ref: { name } })), { name, mount: true }), options),
|
1479
1526
|
});
|
1480
1527
|
_names.mount.add(name);
|
1481
|
-
!isUndefined(options.value) &&
|
1528
|
+
!isUndefined(options.value) &&
|
1529
|
+
set(_formValues, name, get(_formValues, name, options.value));
|
1482
1530
|
field
|
1483
1531
|
? isBoolean(options.disabled) &&
|
1484
1532
|
set(_formValues, name, options.disabled
|
@@ -1540,7 +1588,7 @@ function createFormControl(props = {}) {
|
|
1540
1588
|
});
|
1541
1589
|
try {
|
1542
1590
|
if (_options.resolver) {
|
1543
|
-
const { errors, values } = await
|
1591
|
+
const { errors, values } = await _executeSchema();
|
1544
1592
|
_formState.errors = errors;
|
1545
1593
|
fieldValues = values;
|
1546
1594
|
}
|
@@ -1576,10 +1624,35 @@ function createFormControl(props = {}) {
|
|
1576
1624
|
});
|
1577
1625
|
}
|
1578
1626
|
};
|
1627
|
+
const resetField = (name, options = {}) => {
|
1628
|
+
if (isUndefined(options.defaultValue)) {
|
1629
|
+
setValue(name, get(_defaultValues, name));
|
1630
|
+
}
|
1631
|
+
else {
|
1632
|
+
setValue(name, options.defaultValue);
|
1633
|
+
set(_defaultValues, name, options.defaultValue);
|
1634
|
+
}
|
1635
|
+
if (!options.keepTouched) {
|
1636
|
+
unset(_formState.touchedFields, name);
|
1637
|
+
}
|
1638
|
+
if (!options.keepDirty) {
|
1639
|
+
unset(_formState.dirtyFields, name);
|
1640
|
+
_formState.isDirty = options.defaultValue
|
1641
|
+
? _getDirty(name, get(_defaultValues, name))
|
1642
|
+
: _getDirty();
|
1643
|
+
}
|
1644
|
+
if (!options.keepError) {
|
1645
|
+
unset(_formState.errors, name);
|
1646
|
+
_proxyFormState.isValid && _updateValid();
|
1647
|
+
}
|
1648
|
+
_subjects.state.next(Object.assign({}, _formState));
|
1649
|
+
};
|
1579
1650
|
const reset = (formValues, keepStateOptions = {}) => {
|
1580
|
-
const hasUpdatedFormValues = !isEmptyObject(formValues);
|
1581
1651
|
const updatedValues = formValues || _defaultValues;
|
1582
1652
|
const cloneUpdatedValues = cloneObject(updatedValues);
|
1653
|
+
const values = !isEmptyObject(formValues)
|
1654
|
+
? cloneUpdatedValues
|
1655
|
+
: _defaultValues;
|
1583
1656
|
if (!keepStateOptions.keepDefaultValues) {
|
1584
1657
|
_defaultValues = updatedValues;
|
1585
1658
|
}
|
@@ -1602,16 +1675,15 @@ function createFormControl(props = {}) {
|
|
1602
1675
|
}
|
1603
1676
|
_formValues = props.shouldUnregister
|
1604
1677
|
? keepStateOptions.keepDefaultValues
|
1605
|
-
? _defaultValues
|
1678
|
+
? cloneObject(_defaultValues)
|
1606
1679
|
: {}
|
1607
1680
|
: cloneUpdatedValues;
|
1608
1681
|
_fields = {};
|
1609
|
-
_subjects.
|
1610
|
-
values
|
1682
|
+
_subjects.watch.next({
|
1683
|
+
values,
|
1611
1684
|
});
|
1612
|
-
_subjects.watch.next({});
|
1613
1685
|
_subjects.array.next({
|
1614
|
-
values
|
1686
|
+
values,
|
1615
1687
|
});
|
1616
1688
|
}
|
1617
1689
|
_names = {
|
@@ -1660,6 +1732,7 @@ function createFormControl(props = {}) {
|
|
1660
1732
|
control: {
|
1661
1733
|
register,
|
1662
1734
|
unregister,
|
1735
|
+
_executeSchema,
|
1663
1736
|
_getWatch,
|
1664
1737
|
_getDirty,
|
1665
1738
|
_updateValid,
|
@@ -1718,6 +1791,7 @@ function createFormControl(props = {}) {
|
|
1718
1791
|
setValue,
|
1719
1792
|
getValues,
|
1720
1793
|
reset,
|
1794
|
+
resetField,
|
1721
1795
|
clearErrors,
|
1722
1796
|
unregister,
|
1723
1797
|
setError,
|
@@ -1766,37 +1840,10 @@ function useForm(props = {}) {
|
|
1766
1840
|
}
|
1767
1841
|
control._removeUnmounted();
|
1768
1842
|
});
|
1843
|
+
React.useEffect(() => () => Object.values(control._subjects).forEach((subject) => subject.unsubscribe()), [control]);
|
1769
1844
|
_formControl.current.formState = getProxyFormState(formState, control._proxyFormState);
|
1770
1845
|
return _formControl.current;
|
1771
1846
|
}
|
1772
1847
|
|
1773
|
-
function useWatch(props) {
|
1774
|
-
const methods = useFormContext();
|
1775
|
-
const { control = methods.control, name, defaultValue, disabled, } = props || {};
|
1776
|
-
const _name = React.useRef(name);
|
1777
|
-
_name.current = name;
|
1778
|
-
useSubscribe({
|
1779
|
-
disabled,
|
1780
|
-
subject: control._subjects.watch,
|
1781
|
-
callback: (formState) => {
|
1782
|
-
if (shouldSubscribeByName(_name.current, formState.name)) {
|
1783
|
-
control._stateFlags.mount = true;
|
1784
|
-
const fieldValues = control._getWatch(_name.current, defaultValue);
|
1785
|
-
updateValue(isObject(fieldValues)
|
1786
|
-
? Object.assign({}, fieldValues) : Array.isArray(fieldValues)
|
1787
|
-
? [...fieldValues]
|
1788
|
-
: fieldValues);
|
1789
|
-
}
|
1790
|
-
},
|
1791
|
-
});
|
1792
|
-
const [value, updateValue] = React.useState(isUndefined(defaultValue)
|
1793
|
-
? control._getWatch(name)
|
1794
|
-
: defaultValue);
|
1795
|
-
React.useEffect(() => {
|
1796
|
-
control._removeUnmounted();
|
1797
|
-
});
|
1798
|
-
return value;
|
1799
|
-
}
|
1800
|
-
|
1801
1848
|
export { Controller, FormProvider, appendErrors, get, set, useController, useFieldArray, useForm, useFormContext, useFormState, useWatch };
|
1802
1849
|
//# sourceMappingURL=index.esm.js.map
|