react-hook-form 7.19.5 → 7.20.3
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 +42 -0
- package/README.md +3 -0
- package/dist/index.cjs.js +1 -1
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.esm.js +129 -145
- 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 -1
- package/dist/logic/getDirtyFields.d.ts +2 -0
- package/dist/logic/{getControllerValue.d.ts → getEventValue.d.ts} +0 -0
- package/dist/logic/isWatched.d.ts +3 -0
- package/dist/logic/shouldSubscribeByName.d.ts +1 -1
- package/dist/types/form.d.ts +3 -1
- package/dist/useWatch.d.ts +4 -1
- package/dist/utils/objectHasFunction.d.ts +2 -0
- package/package.json +17 -15
- package/dist/logic/setFieldArrayDirtyFields.d.ts +0 -2
- package/dist/logic/unsetEmptyArray.d.ts +0 -2
package/dist/index.esm.js
CHANGED
@@ -12,7 +12,7 @@ var isObject = (value) => !isNullOrUndefined(value) &&
|
|
12
12
|
isObjectType(value) &&
|
13
13
|
!isDateObject(value);
|
14
14
|
|
15
|
-
var
|
15
|
+
var getEventValue = (event) => isObject(event) && event.target
|
16
16
|
? isCheckBoxInput(event.target)
|
17
17
|
? event.target.checked
|
18
18
|
: event.target.value
|
@@ -103,49 +103,35 @@ var shouldRenderFormState = (formStateData, _proxyFormState, isRoot) => {
|
|
103
103
|
|
104
104
|
var convertToArrayPayload = (value) => (Array.isArray(value) ? value : [value]);
|
105
105
|
|
106
|
-
var shouldSubscribeByName = (name, signalName) =>
|
107
|
-
|
108
|
-
name
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
_subscription.current.unsubscribe();
|
116
|
-
_subscription.current = undefined;
|
117
|
-
}
|
118
|
-
};
|
119
|
-
const updateSubscriptionProps = ({ _subscription, _props }) => {
|
120
|
-
if (_props.current.disabled) {
|
121
|
-
tearDown(_subscription);
|
122
|
-
}
|
123
|
-
else if (!_subscription.current) {
|
124
|
-
_subscription.current = _props.current.subject.subscribe({
|
125
|
-
next: _props.current.callback,
|
126
|
-
});
|
127
|
-
}
|
128
|
-
};
|
106
|
+
var shouldSubscribeByName = (name, signalName, exact) => exact && signalName
|
107
|
+
? name === signalName
|
108
|
+
: !name ||
|
109
|
+
!signalName ||
|
110
|
+
name === signalName ||
|
111
|
+
convertToArrayPayload(name).some((currentName) => currentName &&
|
112
|
+
(currentName.startsWith(signalName) ||
|
113
|
+
signalName.startsWith(currentName)));
|
114
|
+
|
129
115
|
function useSubscribe(props) {
|
130
|
-
const _subscription = React.useRef();
|
131
116
|
const _props = React.useRef(props);
|
132
117
|
_props.current = props;
|
133
|
-
updateSubscriptionProps({
|
134
|
-
_subscription,
|
135
|
-
_props,
|
136
|
-
});
|
137
118
|
React.useEffect(() => {
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
119
|
+
const tearDown = (subscription) => {
|
120
|
+
if (subscription) {
|
121
|
+
subscription.unsubscribe();
|
122
|
+
}
|
123
|
+
};
|
124
|
+
const subscription = !props.disabled &&
|
125
|
+
_props.current.subject.subscribe({
|
126
|
+
next: _props.current.callback,
|
127
|
+
});
|
128
|
+
return () => tearDown(subscription);
|
129
|
+
}, [props.disabled]);
|
144
130
|
}
|
145
131
|
|
146
132
|
function useFormState(props) {
|
147
133
|
const methods = useFormContext();
|
148
|
-
const { control = methods.control, disabled, name } = props || {};
|
134
|
+
const { control = methods.control, disabled, name, exact } = props || {};
|
149
135
|
const [formState, updateFormState] = React.useState(control._formState);
|
150
136
|
const _localProxyFormState = React.useRef({
|
151
137
|
isDirty: false,
|
@@ -159,9 +145,9 @@ function useFormState(props) {
|
|
159
145
|
_name.current = name;
|
160
146
|
useSubscribe({
|
161
147
|
disabled,
|
162
|
-
callback: (
|
163
|
-
shouldRenderFormState(
|
164
|
-
updateFormState(Object.assign(Object.assign({}, control._formState),
|
148
|
+
callback: (value) => shouldSubscribeByName(_name.current, value.name, exact) &&
|
149
|
+
shouldRenderFormState(value, _localProxyFormState.current) &&
|
150
|
+
updateFormState(Object.assign(Object.assign({}, control._formState), value)),
|
165
151
|
subject: control._subjects.state,
|
166
152
|
});
|
167
153
|
return getProxyFormState(formState, control._proxyFormState, _localProxyFormState.current, false);
|
@@ -169,7 +155,7 @@ function useFormState(props) {
|
|
169
155
|
|
170
156
|
var isString = (value) => typeof value === 'string';
|
171
157
|
|
172
|
-
|
158
|
+
var generateWatchOutput = (names, _names, formValues, isGlobal) => {
|
173
159
|
const isArray = Array.isArray(names);
|
174
160
|
if (isString(names)) {
|
175
161
|
isGlobal && _names.watch.add(names);
|
@@ -181,20 +167,32 @@ function generateWatchOutput(names, _names, formValues, isGlobal) {
|
|
181
167
|
}
|
182
168
|
isGlobal && (_names.watchAll = true);
|
183
169
|
return formValues;
|
184
|
-
}
|
170
|
+
};
|
171
|
+
|
172
|
+
var isFunction = (value) => typeof value === 'function';
|
173
|
+
|
174
|
+
var objectHasFunction = (data) => {
|
175
|
+
for (const key in data) {
|
176
|
+
if (isFunction(data[key])) {
|
177
|
+
return true;
|
178
|
+
}
|
179
|
+
}
|
180
|
+
return false;
|
181
|
+
};
|
185
182
|
|
186
183
|
function useWatch(props) {
|
187
184
|
const methods = useFormContext();
|
188
|
-
const { control = methods.control, name, defaultValue, disabled, } = props || {};
|
185
|
+
const { control = methods.control, name, defaultValue, disabled, exact, } = props || {};
|
189
186
|
const _name = React.useRef(name);
|
190
187
|
_name.current = name;
|
191
188
|
useSubscribe({
|
192
189
|
disabled,
|
193
190
|
subject: control._subjects.watch,
|
194
191
|
callback: (formState) => {
|
195
|
-
if (shouldSubscribeByName(_name.current, formState.name)) {
|
192
|
+
if (shouldSubscribeByName(_name.current, formState.name, exact)) {
|
196
193
|
const fieldValues = generateWatchOutput(_name.current, control._names, formState.values || control._formValues);
|
197
|
-
updateValue(isUndefined(_name.current)
|
194
|
+
updateValue(isUndefined(_name.current) ||
|
195
|
+
(isObject(fieldValues) && !objectHasFunction(fieldValues))
|
198
196
|
? Object.assign({}, fieldValues) : Array.isArray(fieldValues)
|
199
197
|
? [...fieldValues]
|
200
198
|
: fieldValues);
|
@@ -213,10 +211,12 @@ function useWatch(props) {
|
|
213
211
|
function useController(props) {
|
214
212
|
const methods = useFormContext();
|
215
213
|
const { name, control = methods.control, shouldUnregister } = props;
|
214
|
+
const isArrayField = isNameInFieldArray(control._names.array, name);
|
216
215
|
const value = useWatch({
|
217
216
|
control,
|
218
217
|
name,
|
219
218
|
defaultValue: get(control._formValues, name, get(control._defaultValues, name, props.defaultValue)),
|
219
|
+
exact: !isArrayField,
|
220
220
|
});
|
221
221
|
const formState = useFormState({
|
222
222
|
control,
|
@@ -235,7 +235,7 @@ function useController(props) {
|
|
235
235
|
updateMounted(name, true);
|
236
236
|
return () => {
|
237
237
|
const _shouldUnregisterField = control._options.shouldUnregister || shouldUnregister;
|
238
|
-
if (
|
238
|
+
if (isArrayField
|
239
239
|
? _shouldUnregisterField && !control._stateFlags.action
|
240
240
|
: _shouldUnregisterField) {
|
241
241
|
control.unregister(name, { keepDefaultValue: true });
|
@@ -244,13 +244,13 @@ function useController(props) {
|
|
244
244
|
updateMounted(name, false);
|
245
245
|
}
|
246
246
|
};
|
247
|
-
}, [name, control, shouldUnregister]);
|
247
|
+
}, [name, control, isArrayField, shouldUnregister]);
|
248
248
|
return {
|
249
249
|
field: {
|
250
250
|
onChange: (event) => {
|
251
251
|
registerProps.onChange({
|
252
252
|
target: {
|
253
|
-
value:
|
253
|
+
value: getEventValue(event),
|
254
254
|
name: name,
|
255
255
|
},
|
256
256
|
type: EVENTS.CHANGE,
|
@@ -259,7 +259,7 @@ function useController(props) {
|
|
259
259
|
onBlur: () => {
|
260
260
|
registerProps.onBlur({
|
261
261
|
target: {
|
262
|
-
value,
|
262
|
+
value: get(control._formValues, name),
|
263
263
|
name: name,
|
264
264
|
},
|
265
265
|
type: EVENTS.BLUR,
|
@@ -347,6 +347,12 @@ var getFocusFieldName = (name, index, options = {}) => options.shouldFocus || is
|
|
347
347
|
`${name}.${isUndefined(options.focusIndex) ? index : options.focusIndex}.`
|
348
348
|
: '';
|
349
349
|
|
350
|
+
var isWatched = (name, _names, isBlurEvent) => !isBlurEvent &&
|
351
|
+
(_names.watchAll ||
|
352
|
+
_names.watch.has(name) ||
|
353
|
+
[..._names.watch].some((watchName) => name.startsWith(watchName) &&
|
354
|
+
/^\.\w+/.test(name.slice(watchName.length))));
|
355
|
+
|
350
356
|
var mapCurrentIds = (values, _fieldIds, keyName) => values.map((value, index) => {
|
351
357
|
const output = _fieldIds.current[index];
|
352
358
|
return Object.assign(Object.assign({}, value), (output ? { [keyName]: output[keyName] } : {}));
|
@@ -442,44 +448,44 @@ const useFieldArray = (props) => {
|
|
442
448
|
const append$1 = (value, options) => {
|
443
449
|
const appendValue = convertToArrayPayload(value);
|
444
450
|
const updatedFieldArrayValuesWithKey = append(mapCurrentIds(control._getFieldArray(name), _fieldIds, keyName), mapIds(appendValue, keyName));
|
451
|
+
setFields(updatedFieldArrayValuesWithKey);
|
445
452
|
control._updateFieldArray(name, append, {
|
446
453
|
argA: fillEmptyArray(value),
|
447
454
|
}, updateValues(updatedFieldArrayValuesWithKey));
|
448
|
-
setFields(updatedFieldArrayValuesWithKey);
|
449
455
|
control._names.focus = getFocusFieldName(name, updatedFieldArrayValuesWithKey.length - appendValue.length, options);
|
450
456
|
};
|
451
457
|
const prepend$1 = (value, options) => {
|
452
458
|
const updatedFieldArrayValuesWithKey = prepend(mapCurrentIds(control._getFieldArray(name), _fieldIds, keyName), mapIds(convertToArrayPayload(value), keyName));
|
459
|
+
setFields(updatedFieldArrayValuesWithKey);
|
453
460
|
control._updateFieldArray(name, prepend, {
|
454
461
|
argA: fillEmptyArray(value),
|
455
462
|
}, updateValues(updatedFieldArrayValuesWithKey));
|
456
|
-
setFields(updatedFieldArrayValuesWithKey);
|
457
463
|
control._names.focus = getFocusFieldName(name, 0, options);
|
458
464
|
};
|
459
465
|
const remove = (index) => {
|
460
466
|
const updatedFieldArrayValuesWithKey = removeArrayAt(mapCurrentIds(control._getFieldArray(name), _fieldIds, keyName), index);
|
467
|
+
setFields(updatedFieldArrayValuesWithKey);
|
461
468
|
control._updateFieldArray(name, removeArrayAt, {
|
462
469
|
argA: index,
|
463
470
|
}, updateValues(updatedFieldArrayValuesWithKey));
|
464
|
-
setFields(updatedFieldArrayValuesWithKey);
|
465
471
|
};
|
466
472
|
const insert$1 = (index, value, options) => {
|
467
473
|
const updatedFieldArrayValuesWithKey = insert(mapCurrentIds(control._getFieldArray(name), _fieldIds, keyName), index, mapIds(convertToArrayPayload(value), keyName));
|
474
|
+
setFields(updatedFieldArrayValuesWithKey);
|
468
475
|
control._updateFieldArray(name, insert, {
|
469
476
|
argA: index,
|
470
477
|
argB: fillEmptyArray(value),
|
471
478
|
}, updateValues(updatedFieldArrayValuesWithKey));
|
472
|
-
setFields(updatedFieldArrayValuesWithKey);
|
473
479
|
control._names.focus = getFocusFieldName(name, index, options);
|
474
480
|
};
|
475
481
|
const swap = (indexA, indexB) => {
|
476
482
|
const updatedFieldArrayValuesWithKey = mapCurrentIds(control._getFieldArray(name), _fieldIds, keyName);
|
477
483
|
swapArrayAt(updatedFieldArrayValuesWithKey, indexA, indexB);
|
484
|
+
setFields(updatedFieldArrayValuesWithKey);
|
478
485
|
control._updateFieldArray(name, swapArrayAt, {
|
479
486
|
argA: indexA,
|
480
487
|
argB: indexB,
|
481
488
|
}, updateValues(updatedFieldArrayValuesWithKey), false);
|
482
|
-
setFields(updatedFieldArrayValuesWithKey);
|
483
489
|
};
|
484
490
|
const move = (from, to) => {
|
485
491
|
const updatedFieldArrayValuesWithKey = mapCurrentIds(control._getFieldArray(name), _fieldIds, keyName);
|
@@ -494,30 +500,20 @@ const useFieldArray = (props) => {
|
|
494
500
|
const updatedFieldArrayValuesWithKey = mapCurrentIds(control._getFieldArray(name), _fieldIds, keyName);
|
495
501
|
const updatedFieldArrayValues = updateAt(updatedFieldArrayValuesWithKey, index, value);
|
496
502
|
_fieldIds.current = mapIds(updatedFieldArrayValues, keyName);
|
503
|
+
setFields(_fieldIds.current);
|
497
504
|
control._updateFieldArray(name, updateAt, {
|
498
505
|
argA: index,
|
499
506
|
argB: value,
|
500
507
|
}, updateValues(_fieldIds.current), true, false);
|
501
|
-
setFields(_fieldIds.current);
|
502
508
|
};
|
503
509
|
const replace = (value) => {
|
504
510
|
const updatedFieldArrayValuesWithKey = mapIds(convertToArrayPayload(value), keyName);
|
505
|
-
control._updateFieldArray(name, () => updatedFieldArrayValuesWithKey, {}, updateValues(updatedFieldArrayValuesWithKey), true, false);
|
506
511
|
setFields(updatedFieldArrayValuesWithKey);
|
512
|
+
control._updateFieldArray(name, () => updatedFieldArrayValuesWithKey, {}, updateValues(updatedFieldArrayValuesWithKey), true, false);
|
507
513
|
};
|
508
514
|
React.useEffect(() => {
|
509
515
|
control._stateFlags.action = false;
|
510
|
-
|
511
|
-
control._subjects.state.next({});
|
512
|
-
}
|
513
|
-
else {
|
514
|
-
for (const watchField of control._names.watch) {
|
515
|
-
if (name.startsWith(watchField)) {
|
516
|
-
control._subjects.state.next({});
|
517
|
-
break;
|
518
|
-
}
|
519
|
-
}
|
520
|
-
}
|
516
|
+
isWatched(name, control._names) && control._subjects.state.next({});
|
521
517
|
if (_actioned.current) {
|
522
518
|
control._executeSchema([name]).then((result) => {
|
523
519
|
const error = get(result.errors, name);
|
@@ -559,8 +555,6 @@ const useFieldArray = (props) => {
|
|
559
555
|
};
|
560
556
|
};
|
561
557
|
|
562
|
-
var isFunction = (value) => typeof value === 'function';
|
563
|
-
|
564
558
|
function cloneObject(data) {
|
565
559
|
let copy;
|
566
560
|
const isArray = Array.isArray(data);
|
@@ -620,6 +614,9 @@ function deepEqual(object1, object2) {
|
|
620
614
|
if (isPrimitive(object1) || isPrimitive(object2)) {
|
621
615
|
return object1 === object2;
|
622
616
|
}
|
617
|
+
if (!isNaN(object1) || !isNaN(object2)) {
|
618
|
+
return +object1 === +object2;
|
619
|
+
}
|
623
620
|
if (isDateObject(object1) && isDateObject(object2)) {
|
624
621
|
return object1.getTime() === object2.getTime();
|
625
622
|
}
|
@@ -712,6 +709,47 @@ function unset(object, path) {
|
|
712
709
|
return object;
|
713
710
|
}
|
714
711
|
|
712
|
+
function markFieldsDirty(data, fields = {}) {
|
713
|
+
const isParentNodeArray = Array.isArray(data);
|
714
|
+
if (isObject(data) || isParentNodeArray) {
|
715
|
+
for (const key in data) {
|
716
|
+
if (Array.isArray(data[key]) ||
|
717
|
+
(isObject(data[key]) && !objectHasFunction(data[key]))) {
|
718
|
+
fields[key] = Array.isArray(data[key]) ? [] : {};
|
719
|
+
markFieldsDirty(data[key], fields[key]);
|
720
|
+
}
|
721
|
+
else if (!isNullOrUndefined(data[key])) {
|
722
|
+
fields[key] = true;
|
723
|
+
}
|
724
|
+
}
|
725
|
+
}
|
726
|
+
return fields;
|
727
|
+
}
|
728
|
+
function getDirtyFieldsFromDefaultValues(data, formValues, dirtyFieldsFromValues) {
|
729
|
+
const isParentNodeArray = Array.isArray(data);
|
730
|
+
if (isObject(data) || isParentNodeArray) {
|
731
|
+
for (const key in data) {
|
732
|
+
if (Array.isArray(data[key]) ||
|
733
|
+
(isObject(data[key]) && !objectHasFunction(data[key]))) {
|
734
|
+
if (isUndefined(formValues) ||
|
735
|
+
isPrimitive(dirtyFieldsFromValues[key])) {
|
736
|
+
dirtyFieldsFromValues[key] = Array.isArray(data[key])
|
737
|
+
? markFieldsDirty(data[key], [])
|
738
|
+
: Object.assign({}, markFieldsDirty(data[key]));
|
739
|
+
}
|
740
|
+
else {
|
741
|
+
getDirtyFieldsFromDefaultValues(data[key], isNullOrUndefined(formValues) ? {} : formValues[key], dirtyFieldsFromValues[key]);
|
742
|
+
}
|
743
|
+
}
|
744
|
+
else {
|
745
|
+
dirtyFieldsFromValues[key] = !deepEqual(data[key], formValues[key]);
|
746
|
+
}
|
747
|
+
}
|
748
|
+
}
|
749
|
+
return dirtyFieldsFromValues;
|
750
|
+
}
|
751
|
+
var getDirtyFields = (defaultValues, formValues) => getDirtyFieldsFromDefaultValues(defaultValues, formValues, markFieldsDirty(formValues));
|
752
|
+
|
715
753
|
const defaultResult = {
|
716
754
|
value: false,
|
717
755
|
isValid: false,
|
@@ -834,49 +872,6 @@ function schemaErrorLookup(errors, _fields, name) {
|
|
834
872
|
};
|
835
873
|
}
|
836
874
|
|
837
|
-
function deepMerge(target, source) {
|
838
|
-
if (isPrimitive(target) || isPrimitive(source)) {
|
839
|
-
return source;
|
840
|
-
}
|
841
|
-
for (const key in source) {
|
842
|
-
const targetValue = target[key];
|
843
|
-
const sourceValue = source[key];
|
844
|
-
try {
|
845
|
-
target[key] =
|
846
|
-
(isObject(targetValue) && isObject(sourceValue)) ||
|
847
|
-
(Array.isArray(targetValue) && Array.isArray(sourceValue))
|
848
|
-
? deepMerge(targetValue, sourceValue)
|
849
|
-
: sourceValue;
|
850
|
-
}
|
851
|
-
catch (_a) { }
|
852
|
-
}
|
853
|
-
return target;
|
854
|
-
}
|
855
|
-
|
856
|
-
function setDirtyFields(values, defaultValues, dirtyFields, parentNode, parentName) {
|
857
|
-
let index = -1;
|
858
|
-
while (++index < values.length) {
|
859
|
-
for (const key in values[index]) {
|
860
|
-
if (Array.isArray(values[index][key])) {
|
861
|
-
!dirtyFields[index] && (dirtyFields[index] = {});
|
862
|
-
dirtyFields[index][key] = [];
|
863
|
-
setDirtyFields(values[index][key], get(defaultValues[index] || {}, key, []), dirtyFields[index][key], dirtyFields[index], key);
|
864
|
-
}
|
865
|
-
else {
|
866
|
-
!isNullOrUndefined(defaultValues) &&
|
867
|
-
deepEqual(get(defaultValues[index] || {}, key), values[index][key])
|
868
|
-
? set(dirtyFields[index] || {}, key)
|
869
|
-
: (dirtyFields[index] = Object.assign(Object.assign({}, dirtyFields[index]), { [key]: true }));
|
870
|
-
}
|
871
|
-
}
|
872
|
-
parentNode &&
|
873
|
-
!dirtyFields.length &&
|
874
|
-
delete parentNode[parentName];
|
875
|
-
}
|
876
|
-
return dirtyFields;
|
877
|
-
}
|
878
|
-
var setFieldArrayDirtyFields = (values, defaultValues, dirtyFields) => deepMerge(setDirtyFields(values, defaultValues, dirtyFields.slice(0, values.length)), setDirtyFields(defaultValues, values, dirtyFields.slice(0, values.length)));
|
879
|
-
|
880
875
|
var skipValidation = (isBlurEvent, isTouched, isSubmitted, reValidateMode, mode) => {
|
881
876
|
if (mode.isOnAll) {
|
882
877
|
return false;
|
@@ -893,8 +888,6 @@ var skipValidation = (isBlurEvent, isTouched, isSubmitted, reValidateMode, mode)
|
|
893
888
|
return true;
|
894
889
|
};
|
895
890
|
|
896
|
-
var unsetEmptyArray = (ref, name) => !compact(get(ref, name, [])).length && unset(ref, name);
|
897
|
-
|
898
891
|
var isMessage = (value) => isString(value) || React.isValidElement(value);
|
899
892
|
|
900
893
|
var isRegex = (value) => value instanceof RegExp;
|
@@ -924,7 +917,7 @@ var validateField = async (field, inputValue, validateAllFieldCriteria, shouldUs
|
|
924
917
|
return {};
|
925
918
|
}
|
926
919
|
const inputRef = refs ? refs[0] : ref;
|
927
|
-
const
|
920
|
+
const setCustomValidity = (message) => {
|
928
921
|
if (shouldUseNativeValidation && inputRef.reportValidity) {
|
929
922
|
inputRef.setCustomValidity(isBoolean(message) ? '' : message || ' ');
|
930
923
|
inputRef.reportValidity();
|
@@ -954,7 +947,7 @@ var validateField = async (field, inputValue, validateAllFieldCriteria, shouldUs
|
|
954
947
|
if (value) {
|
955
948
|
error[name] = Object.assign({ type: INPUT_VALIDATION_RULES.required, message, ref: inputRef }, appendErrorsCurry(INPUT_VALIDATION_RULES.required, message));
|
956
949
|
if (!validateAllFieldCriteria) {
|
957
|
-
|
950
|
+
setCustomValidity(message);
|
958
951
|
return error;
|
959
952
|
}
|
960
953
|
}
|
@@ -985,7 +978,7 @@ var validateField = async (field, inputValue, validateAllFieldCriteria, shouldUs
|
|
985
978
|
if (exceedMax || exceedMin) {
|
986
979
|
getMinMaxMessage(!!exceedMax, maxOutput.message, minOutput.message, INPUT_VALIDATION_RULES.max, INPUT_VALIDATION_RULES.min);
|
987
980
|
if (!validateAllFieldCriteria) {
|
988
|
-
|
981
|
+
setCustomValidity(error[name].message);
|
989
982
|
return error;
|
990
983
|
}
|
991
984
|
}
|
@@ -1000,7 +993,7 @@ var validateField = async (field, inputValue, validateAllFieldCriteria, shouldUs
|
|
1000
993
|
if (exceedMax || exceedMin) {
|
1001
994
|
getMinMaxMessage(exceedMax, maxLengthOutput.message, minLengthOutput.message);
|
1002
995
|
if (!validateAllFieldCriteria) {
|
1003
|
-
|
996
|
+
setCustomValidity(error[name].message);
|
1004
997
|
return error;
|
1005
998
|
}
|
1006
999
|
}
|
@@ -1011,7 +1004,7 @@ var validateField = async (field, inputValue, validateAllFieldCriteria, shouldUs
|
|
1011
1004
|
error[name] = Object.assign({ type: INPUT_VALIDATION_RULES.pattern, message,
|
1012
1005
|
ref }, appendErrorsCurry(INPUT_VALIDATION_RULES.pattern, message));
|
1013
1006
|
if (!validateAllFieldCriteria) {
|
1014
|
-
|
1007
|
+
setCustomValidity(message);
|
1015
1008
|
return error;
|
1016
1009
|
}
|
1017
1010
|
}
|
@@ -1023,7 +1016,7 @@ var validateField = async (field, inputValue, validateAllFieldCriteria, shouldUs
|
|
1023
1016
|
if (validateError) {
|
1024
1017
|
error[name] = Object.assign(Object.assign({}, validateError), appendErrorsCurry(INPUT_VALIDATION_RULES.validate, validateError.message));
|
1025
1018
|
if (!validateAllFieldCriteria) {
|
1026
|
-
|
1019
|
+
setCustomValidity(validateError.message);
|
1027
1020
|
return error;
|
1028
1021
|
}
|
1029
1022
|
}
|
@@ -1037,7 +1030,7 @@ var validateField = async (field, inputValue, validateAllFieldCriteria, shouldUs
|
|
1037
1030
|
const validateError = getValidateError(await validate[key](inputValue), inputRef, key);
|
1038
1031
|
if (validateError) {
|
1039
1032
|
validationResult = Object.assign(Object.assign({}, validateError), appendErrorsCurry(key, validateError.message));
|
1040
|
-
|
1033
|
+
setCustomValidity(validateError.message);
|
1041
1034
|
if (validateAllFieldCriteria) {
|
1042
1035
|
error[name] = validationResult;
|
1043
1036
|
}
|
@@ -1051,7 +1044,7 @@ var validateField = async (field, inputValue, validateAllFieldCriteria, shouldUs
|
|
1051
1044
|
}
|
1052
1045
|
}
|
1053
1046
|
}
|
1054
|
-
|
1047
|
+
setCustomValidity(true);
|
1055
1048
|
return error;
|
1056
1049
|
};
|
1057
1050
|
|
@@ -1114,10 +1107,6 @@ function createFormControl(props = {}) {
|
|
1114
1107
|
clearTimeout(timer);
|
1115
1108
|
timer = window.setTimeout(() => callback(...args), wait);
|
1116
1109
|
};
|
1117
|
-
const isFieldWatched = (name, isBlurEvent) => !isBlurEvent &&
|
1118
|
-
(_names.watchAll ||
|
1119
|
-
_names.watch.has(name) ||
|
1120
|
-
_names.watch.has((name.match(/\w+/) || [])[0]));
|
1121
1110
|
const _updateValid = async (shouldSkipRender) => {
|
1122
1111
|
let isValid = false;
|
1123
1112
|
if (_proxyFormState.isValid) {
|
@@ -1142,16 +1131,14 @@ function createFormControl(props = {}) {
|
|
1142
1131
|
if (Array.isArray(get(_formState.errors, name))) {
|
1143
1132
|
const errors = method(get(_formState.errors, name), args.argA, args.argB);
|
1144
1133
|
shouldSetValues && set(_formState.errors, name, errors);
|
1145
|
-
unsetEmptyArray(_formState.errors, name);
|
1146
1134
|
}
|
1147
1135
|
if (_proxyFormState.touchedFields && get(_formState.touchedFields, name)) {
|
1148
1136
|
const touchedFields = method(get(_formState.touchedFields, name), args.argA, args.argB);
|
1149
1137
|
shouldSetValues &&
|
1150
1138
|
set(_formState.touchedFields, name, touchedFields);
|
1151
|
-
unsetEmptyArray(_formState.touchedFields, name);
|
1152
1139
|
}
|
1153
1140
|
if (_proxyFormState.dirtyFields || _proxyFormState.isDirty) {
|
1154
|
-
|
1141
|
+
_formState.dirtyFields = getDirtyFields(_defaultValues, _formValues);
|
1155
1142
|
}
|
1156
1143
|
_subjects.state.next({
|
1157
1144
|
isDirty: _getDirty(name, values),
|
@@ -1209,8 +1196,6 @@ function createFormControl(props = {}) {
|
|
1209
1196
|
isFieldDirty && shouldRender && _subjects.state.next(output);
|
1210
1197
|
return isFieldDirty ? output : {};
|
1211
1198
|
};
|
1212
|
-
const updateFieldArrayDirty = (name, value) => (set(_formState.dirtyFields, name, setFieldArrayDirtyFields(value, get(_defaultValues, name, []), get(_formState.dirtyFields, name, []))),
|
1213
|
-
unsetEmptyArray(_formState.dirtyFields, name));
|
1214
1199
|
const shouldRenderByError = async (shouldSkipRender, name, isValid, error, fieldState) => {
|
1215
1200
|
const previousFieldError = get(_formState.errors, name);
|
1216
1201
|
const shouldUpdateValid = _proxyFormState.isValid && _formState.isValid !== isValid;
|
@@ -1322,10 +1307,7 @@ function createFormControl(props = {}) {
|
|
1322
1307
|
isWeb && isHTMLElement(fieldReference.ref) && isNullOrUndefined(value)
|
1323
1308
|
? ''
|
1324
1309
|
: value;
|
1325
|
-
if (
|
1326
|
-
fieldReference.ref.files = fieldValue;
|
1327
|
-
}
|
1328
|
-
else if (isMultipleSelect(fieldReference.ref)) {
|
1310
|
+
if (isMultipleSelect(fieldReference.ref)) {
|
1329
1311
|
[...fieldReference.ref.options].forEach((selectRef) => (selectRef.selected = fieldValue.includes(selectRef.value)));
|
1330
1312
|
}
|
1331
1313
|
else if (fieldReference.refs) {
|
@@ -1340,7 +1322,7 @@ function createFormControl(props = {}) {
|
|
1340
1322
|
fieldReference.refs.forEach((radioRef) => (radioRef.checked = radioRef.value === fieldValue));
|
1341
1323
|
}
|
1342
1324
|
}
|
1343
|
-
else {
|
1325
|
+
else if (!isFileInput(fieldReference.ref)) {
|
1344
1326
|
fieldReference.ref.value = fieldValue;
|
1345
1327
|
}
|
1346
1328
|
}
|
@@ -1373,7 +1355,7 @@ function createFormControl(props = {}) {
|
|
1373
1355
|
});
|
1374
1356
|
if ((_proxyFormState.isDirty || _proxyFormState.dirtyFields) &&
|
1375
1357
|
options.shouldDirty) {
|
1376
|
-
|
1358
|
+
_formState.dirtyFields = getDirtyFields(_defaultValues, _formValues);
|
1377
1359
|
_subjects.state.next({
|
1378
1360
|
name,
|
1379
1361
|
dirtyFields: _formState.dirtyFields,
|
@@ -1386,7 +1368,7 @@ function createFormControl(props = {}) {
|
|
1386
1368
|
? setValues(name, value, options)
|
1387
1369
|
: setFieldValue(name, value, options);
|
1388
1370
|
}
|
1389
|
-
|
1371
|
+
isWatched(name, _names) && _subjects.state.next({});
|
1390
1372
|
_subjects.watch.next({
|
1391
1373
|
name,
|
1392
1374
|
});
|
@@ -1398,14 +1380,16 @@ function createFormControl(props = {}) {
|
|
1398
1380
|
if (field) {
|
1399
1381
|
let error;
|
1400
1382
|
let isValid;
|
1401
|
-
const fieldValue = target.type
|
1383
|
+
const fieldValue = target.type
|
1384
|
+
? getFieldValue(field._f)
|
1385
|
+
: getEventValue(event);
|
1402
1386
|
const isBlurEvent = event.type === EVENTS.BLUR;
|
1403
1387
|
const shouldSkipValidation = (!hasValidation(field._f) &&
|
1404
1388
|
!_options.resolver &&
|
1405
1389
|
!get(_formState.errors, name) &&
|
1406
1390
|
!field._f.deps) ||
|
1407
1391
|
skipValidation(isBlurEvent, get(_formState.touchedFields, name), _formState.isSubmitted, validationModeAfterSubmit, validationModeBeforeSubmit);
|
1408
|
-
const
|
1392
|
+
const watched = isWatched(name, _names, isBlurEvent);
|
1409
1393
|
if (isBlurEvent) {
|
1410
1394
|
field._f.onBlur && field._f.onBlur(event);
|
1411
1395
|
}
|
@@ -1414,7 +1398,7 @@ function createFormControl(props = {}) {
|
|
1414
1398
|
}
|
1415
1399
|
set(_formValues, name, fieldValue);
|
1416
1400
|
const fieldState = updateTouchAndDirty(name, fieldValue, isBlurEvent, false);
|
1417
|
-
const shouldRender = !isEmptyObject(fieldState) ||
|
1401
|
+
const shouldRender = !isEmptyObject(fieldState) || watched;
|
1418
1402
|
!isBlurEvent &&
|
1419
1403
|
_subjects.watch.next({
|
1420
1404
|
name,
|
@@ -1422,9 +1406,9 @@ function createFormControl(props = {}) {
|
|
1422
1406
|
});
|
1423
1407
|
if (shouldSkipValidation) {
|
1424
1408
|
return (shouldRender &&
|
1425
|
-
_subjects.state.next(Object.assign({ name }, (
|
1409
|
+
_subjects.state.next(Object.assign({ name }, (watched ? {} : fieldState))));
|
1426
1410
|
}
|
1427
|
-
!isBlurEvent &&
|
1411
|
+
!isBlurEvent && watched && _subjects.state.next({});
|
1428
1412
|
validateFields[name] = validateFields[name] ? +1 : 1;
|
1429
1413
|
_proxyFormState.isValidating &&
|
1430
1414
|
_subjects.state.next({
|
@@ -1838,9 +1822,9 @@ function useForm(props = {}) {
|
|
1838
1822
|
const control = _formControl.current.control;
|
1839
1823
|
useSubscribe({
|
1840
1824
|
subject: control._subjects.state,
|
1841
|
-
callback: (
|
1842
|
-
if (shouldRenderFormState(
|
1843
|
-
control._formState = Object.assign(Object.assign({}, control._formState),
|
1825
|
+
callback: (value) => {
|
1826
|
+
if (shouldRenderFormState(value, control._proxyFormState, true)) {
|
1827
|
+
control._formState = Object.assign(Object.assign({}, control._formState), value);
|
1844
1828
|
updateFormState(Object.assign({}, control._formState));
|
1845
1829
|
}
|
1846
1830
|
},
|