react-hook-form 7.20.1 → 7.20.5
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 +13 -0
- package/README.md +9 -0
- package/dist/index.cjs.js +1 -1
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.esm.js +73 -68
- 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/utils/objectHasFunction.d.ts +2 -0
- package/package.json +3 -4
- package/dist/logic/setFieldArrayDirtyFields.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
|
@@ -155,7 +155,7 @@ function useFormState(props) {
|
|
155
155
|
|
156
156
|
var isString = (value) => typeof value === 'string';
|
157
157
|
|
158
|
-
|
158
|
+
var generateWatchOutput = (names, _names, formValues, isGlobal) => {
|
159
159
|
const isArray = Array.isArray(names);
|
160
160
|
if (isString(names)) {
|
161
161
|
isGlobal && _names.watch.add(names);
|
@@ -167,7 +167,18 @@ function generateWatchOutput(names, _names, formValues, isGlobal) {
|
|
167
167
|
}
|
168
168
|
isGlobal && (_names.watchAll = true);
|
169
169
|
return formValues;
|
170
|
-
}
|
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
|
+
};
|
171
182
|
|
172
183
|
function useWatch(props) {
|
173
184
|
const methods = useFormContext();
|
@@ -180,7 +191,8 @@ function useWatch(props) {
|
|
180
191
|
callback: (formState) => {
|
181
192
|
if (shouldSubscribeByName(_name.current, formState.name, exact)) {
|
182
193
|
const fieldValues = generateWatchOutput(_name.current, control._names, formState.values || control._formValues);
|
183
|
-
updateValue(isUndefined(_name.current)
|
194
|
+
updateValue(isUndefined(_name.current) ||
|
195
|
+
(isObject(fieldValues) && !objectHasFunction(fieldValues))
|
184
196
|
? Object.assign({}, fieldValues) : Array.isArray(fieldValues)
|
185
197
|
? [...fieldValues]
|
186
198
|
: fieldValues);
|
@@ -238,7 +250,7 @@ function useController(props) {
|
|
238
250
|
onChange: (event) => {
|
239
251
|
registerProps.onChange({
|
240
252
|
target: {
|
241
|
-
value:
|
253
|
+
value: getEventValue(event),
|
242
254
|
name: name,
|
243
255
|
},
|
244
256
|
type: EVENTS.CHANGE,
|
@@ -543,8 +555,6 @@ const useFieldArray = (props) => {
|
|
543
555
|
};
|
544
556
|
};
|
545
557
|
|
546
|
-
var isFunction = (value) => typeof value === 'function';
|
547
|
-
|
548
558
|
function cloneObject(data) {
|
549
559
|
let copy;
|
550
560
|
const isArray = Array.isArray(data);
|
@@ -696,6 +706,47 @@ function unset(object, path) {
|
|
696
706
|
return object;
|
697
707
|
}
|
698
708
|
|
709
|
+
function markFieldsDirty(data, fields = {}) {
|
710
|
+
const isParentNodeArray = Array.isArray(data);
|
711
|
+
if (isObject(data) || isParentNodeArray) {
|
712
|
+
for (const key in data) {
|
713
|
+
if (Array.isArray(data[key]) ||
|
714
|
+
(isObject(data[key]) && !objectHasFunction(data[key]))) {
|
715
|
+
fields[key] = Array.isArray(data[key]) ? [] : {};
|
716
|
+
markFieldsDirty(data[key], fields[key]);
|
717
|
+
}
|
718
|
+
else if (!isNullOrUndefined(data[key])) {
|
719
|
+
fields[key] = true;
|
720
|
+
}
|
721
|
+
}
|
722
|
+
}
|
723
|
+
return fields;
|
724
|
+
}
|
725
|
+
function getDirtyFieldsFromDefaultValues(data, formValues, dirtyFieldsFromValues) {
|
726
|
+
const isParentNodeArray = Array.isArray(data);
|
727
|
+
if (isObject(data) || isParentNodeArray) {
|
728
|
+
for (const key in data) {
|
729
|
+
if (Array.isArray(data[key]) ||
|
730
|
+
(isObject(data[key]) && !objectHasFunction(data[key]))) {
|
731
|
+
if (isUndefined(formValues) ||
|
732
|
+
isPrimitive(dirtyFieldsFromValues[key])) {
|
733
|
+
dirtyFieldsFromValues[key] = Array.isArray(data[key])
|
734
|
+
? markFieldsDirty(data[key], [])
|
735
|
+
: Object.assign({}, markFieldsDirty(data[key]));
|
736
|
+
}
|
737
|
+
else {
|
738
|
+
getDirtyFieldsFromDefaultValues(data[key], isNullOrUndefined(formValues) ? {} : formValues[key], dirtyFieldsFromValues[key]);
|
739
|
+
}
|
740
|
+
}
|
741
|
+
else {
|
742
|
+
dirtyFieldsFromValues[key] = !deepEqual(data[key], formValues[key]);
|
743
|
+
}
|
744
|
+
}
|
745
|
+
}
|
746
|
+
return dirtyFieldsFromValues;
|
747
|
+
}
|
748
|
+
var getDirtyFields = (defaultValues, formValues) => getDirtyFieldsFromDefaultValues(defaultValues, formValues, markFieldsDirty(formValues));
|
749
|
+
|
699
750
|
const defaultResult = {
|
700
751
|
value: false,
|
701
752
|
isValid: false,
|
@@ -818,49 +869,6 @@ function schemaErrorLookup(errors, _fields, name) {
|
|
818
869
|
};
|
819
870
|
}
|
820
871
|
|
821
|
-
function deepMerge(target, source) {
|
822
|
-
if (isPrimitive(target) || isPrimitive(source)) {
|
823
|
-
return source;
|
824
|
-
}
|
825
|
-
for (const key in source) {
|
826
|
-
const targetValue = target[key];
|
827
|
-
const sourceValue = source[key];
|
828
|
-
try {
|
829
|
-
target[key] =
|
830
|
-
(isObject(targetValue) && isObject(sourceValue)) ||
|
831
|
-
(Array.isArray(targetValue) && Array.isArray(sourceValue))
|
832
|
-
? deepMerge(targetValue, sourceValue)
|
833
|
-
: sourceValue;
|
834
|
-
}
|
835
|
-
catch (_a) { }
|
836
|
-
}
|
837
|
-
return target;
|
838
|
-
}
|
839
|
-
|
840
|
-
function setDirtyFields(values, defaultValues, dirtyFields, parentNode, parentName) {
|
841
|
-
let index = -1;
|
842
|
-
while (++index < values.length) {
|
843
|
-
for (const key in values[index]) {
|
844
|
-
if (Array.isArray(values[index][key])) {
|
845
|
-
!dirtyFields[index] && (dirtyFields[index] = {});
|
846
|
-
dirtyFields[index][key] = [];
|
847
|
-
setDirtyFields(values[index][key], get(defaultValues[index] || {}, key, []), dirtyFields[index][key], dirtyFields[index], key);
|
848
|
-
}
|
849
|
-
else {
|
850
|
-
!isNullOrUndefined(defaultValues) &&
|
851
|
-
deepEqual(get(defaultValues[index] || {}, key), values[index][key])
|
852
|
-
? set(dirtyFields[index] || {}, key)
|
853
|
-
: (dirtyFields[index] = Object.assign(Object.assign({}, dirtyFields[index]), { [key]: true }));
|
854
|
-
}
|
855
|
-
}
|
856
|
-
parentNode &&
|
857
|
-
!dirtyFields.length &&
|
858
|
-
delete parentNode[parentName];
|
859
|
-
}
|
860
|
-
return dirtyFields;
|
861
|
-
}
|
862
|
-
var setFieldArrayDirtyFields = (values, defaultValues, dirtyFields) => deepMerge(setDirtyFields(values, defaultValues, dirtyFields.slice(0, values.length)), setDirtyFields(defaultValues, values, dirtyFields.slice(0, values.length)));
|
863
|
-
|
864
872
|
var skipValidation = (isBlurEvent, isTouched, isSubmitted, reValidateMode, mode) => {
|
865
873
|
if (mode.isOnAll) {
|
866
874
|
return false;
|
@@ -908,7 +916,7 @@ var validateField = async (field, inputValue, validateAllFieldCriteria, shouldUs
|
|
908
916
|
return {};
|
909
917
|
}
|
910
918
|
const inputRef = refs ? refs[0] : ref;
|
911
|
-
const
|
919
|
+
const setCustomValidity = (message) => {
|
912
920
|
if (shouldUseNativeValidation && inputRef.reportValidity) {
|
913
921
|
inputRef.setCustomValidity(isBoolean(message) ? '' : message || ' ');
|
914
922
|
inputRef.reportValidity();
|
@@ -938,7 +946,7 @@ var validateField = async (field, inputValue, validateAllFieldCriteria, shouldUs
|
|
938
946
|
if (value) {
|
939
947
|
error[name] = Object.assign({ type: INPUT_VALIDATION_RULES.required, message, ref: inputRef }, appendErrorsCurry(INPUT_VALIDATION_RULES.required, message));
|
940
948
|
if (!validateAllFieldCriteria) {
|
941
|
-
|
949
|
+
setCustomValidity(message);
|
942
950
|
return error;
|
943
951
|
}
|
944
952
|
}
|
@@ -969,7 +977,7 @@ var validateField = async (field, inputValue, validateAllFieldCriteria, shouldUs
|
|
969
977
|
if (exceedMax || exceedMin) {
|
970
978
|
getMinMaxMessage(!!exceedMax, maxOutput.message, minOutput.message, INPUT_VALIDATION_RULES.max, INPUT_VALIDATION_RULES.min);
|
971
979
|
if (!validateAllFieldCriteria) {
|
972
|
-
|
980
|
+
setCustomValidity(error[name].message);
|
973
981
|
return error;
|
974
982
|
}
|
975
983
|
}
|
@@ -984,7 +992,7 @@ var validateField = async (field, inputValue, validateAllFieldCriteria, shouldUs
|
|
984
992
|
if (exceedMax || exceedMin) {
|
985
993
|
getMinMaxMessage(exceedMax, maxLengthOutput.message, minLengthOutput.message);
|
986
994
|
if (!validateAllFieldCriteria) {
|
987
|
-
|
995
|
+
setCustomValidity(error[name].message);
|
988
996
|
return error;
|
989
997
|
}
|
990
998
|
}
|
@@ -995,7 +1003,7 @@ var validateField = async (field, inputValue, validateAllFieldCriteria, shouldUs
|
|
995
1003
|
error[name] = Object.assign({ type: INPUT_VALIDATION_RULES.pattern, message,
|
996
1004
|
ref }, appendErrorsCurry(INPUT_VALIDATION_RULES.pattern, message));
|
997
1005
|
if (!validateAllFieldCriteria) {
|
998
|
-
|
1006
|
+
setCustomValidity(message);
|
999
1007
|
return error;
|
1000
1008
|
}
|
1001
1009
|
}
|
@@ -1007,7 +1015,7 @@ var validateField = async (field, inputValue, validateAllFieldCriteria, shouldUs
|
|
1007
1015
|
if (validateError) {
|
1008
1016
|
error[name] = Object.assign(Object.assign({}, validateError), appendErrorsCurry(INPUT_VALIDATION_RULES.validate, validateError.message));
|
1009
1017
|
if (!validateAllFieldCriteria) {
|
1010
|
-
|
1018
|
+
setCustomValidity(validateError.message);
|
1011
1019
|
return error;
|
1012
1020
|
}
|
1013
1021
|
}
|
@@ -1021,7 +1029,7 @@ var validateField = async (field, inputValue, validateAllFieldCriteria, shouldUs
|
|
1021
1029
|
const validateError = getValidateError(await validate[key](inputValue), inputRef, key);
|
1022
1030
|
if (validateError) {
|
1023
1031
|
validationResult = Object.assign(Object.assign({}, validateError), appendErrorsCurry(key, validateError.message));
|
1024
|
-
|
1032
|
+
setCustomValidity(validateError.message);
|
1025
1033
|
if (validateAllFieldCriteria) {
|
1026
1034
|
error[name] = validationResult;
|
1027
1035
|
}
|
@@ -1035,7 +1043,7 @@ var validateField = async (field, inputValue, validateAllFieldCriteria, shouldUs
|
|
1035
1043
|
}
|
1036
1044
|
}
|
1037
1045
|
}
|
1038
|
-
|
1046
|
+
setCustomValidity(true);
|
1039
1047
|
return error;
|
1040
1048
|
};
|
1041
1049
|
|
@@ -1131,7 +1139,7 @@ function createFormControl(props = {}) {
|
|
1131
1139
|
unsetEmptyArray(_formState.touchedFields, name);
|
1132
1140
|
}
|
1133
1141
|
if (_proxyFormState.dirtyFields || _proxyFormState.isDirty) {
|
1134
|
-
|
1142
|
+
_formState.dirtyFields = getDirtyFields(_defaultValues, _formValues);
|
1135
1143
|
}
|
1136
1144
|
_subjects.state.next({
|
1137
1145
|
isDirty: _getDirty(name, values),
|
@@ -1189,8 +1197,6 @@ function createFormControl(props = {}) {
|
|
1189
1197
|
isFieldDirty && shouldRender && _subjects.state.next(output);
|
1190
1198
|
return isFieldDirty ? output : {};
|
1191
1199
|
};
|
1192
|
-
const updateFieldArrayDirty = (name, value) => (set(_formState.dirtyFields, name, setFieldArrayDirtyFields(value, get(_defaultValues, name, []), get(_formState.dirtyFields, name, []))),
|
1193
|
-
unsetEmptyArray(_formState.dirtyFields, name));
|
1194
1200
|
const shouldRenderByError = async (shouldSkipRender, name, isValid, error, fieldState) => {
|
1195
1201
|
const previousFieldError = get(_formState.errors, name);
|
1196
1202
|
const shouldUpdateValid = _proxyFormState.isValid && _formState.isValid !== isValid;
|
@@ -1302,10 +1308,7 @@ function createFormControl(props = {}) {
|
|
1302
1308
|
isWeb && isHTMLElement(fieldReference.ref) && isNullOrUndefined(value)
|
1303
1309
|
? ''
|
1304
1310
|
: value;
|
1305
|
-
if (
|
1306
|
-
fieldReference.ref.files = fieldValue;
|
1307
|
-
}
|
1308
|
-
else if (isMultipleSelect(fieldReference.ref)) {
|
1311
|
+
if (isMultipleSelect(fieldReference.ref)) {
|
1309
1312
|
[...fieldReference.ref.options].forEach((selectRef) => (selectRef.selected = fieldValue.includes(selectRef.value)));
|
1310
1313
|
}
|
1311
1314
|
else if (fieldReference.refs) {
|
@@ -1320,7 +1323,7 @@ function createFormControl(props = {}) {
|
|
1320
1323
|
fieldReference.refs.forEach((radioRef) => (radioRef.checked = radioRef.value === fieldValue));
|
1321
1324
|
}
|
1322
1325
|
}
|
1323
|
-
else {
|
1326
|
+
else if (!isFileInput(fieldReference.ref)) {
|
1324
1327
|
fieldReference.ref.value = fieldValue;
|
1325
1328
|
}
|
1326
1329
|
}
|
@@ -1353,7 +1356,7 @@ function createFormControl(props = {}) {
|
|
1353
1356
|
});
|
1354
1357
|
if ((_proxyFormState.isDirty || _proxyFormState.dirtyFields) &&
|
1355
1358
|
options.shouldDirty) {
|
1356
|
-
|
1359
|
+
_formState.dirtyFields = getDirtyFields(_defaultValues, _formValues);
|
1357
1360
|
_subjects.state.next({
|
1358
1361
|
name,
|
1359
1362
|
dirtyFields: _formState.dirtyFields,
|
@@ -1378,7 +1381,9 @@ function createFormControl(props = {}) {
|
|
1378
1381
|
if (field) {
|
1379
1382
|
let error;
|
1380
1383
|
let isValid;
|
1381
|
-
const fieldValue = target.type
|
1384
|
+
const fieldValue = target.type
|
1385
|
+
? getFieldValue(field._f)
|
1386
|
+
: getEventValue(event);
|
1382
1387
|
const isBlurEvent = event.type === EVENTS.BLUR;
|
1383
1388
|
const shouldSkipValidation = (!hasValidation(field._f) &&
|
1384
1389
|
!_options.resolver &&
|