react-hook-form 7.20.2 → 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/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 getControllerValue = (event) => isObject(event) && event.target
15
+ var getEventValue = (event) => isObject(event) && event.target
16
16
  ? isCheckBoxInput(event.target)
17
17
  ? event.target.checked
18
18
  : event.target.value
@@ -250,7 +250,7 @@ function useController(props) {
250
250
  onChange: (event) => {
251
251
  registerProps.onChange({
252
252
  target: {
253
- value: getControllerValue(event),
253
+ value: getEventValue(event),
254
254
  name: name,
255
255
  },
256
256
  type: EVENTS.CHANGE,
@@ -614,6 +614,9 @@ function deepEqual(object1, object2) {
614
614
  if (isPrimitive(object1) || isPrimitive(object2)) {
615
615
  return object1 === object2;
616
616
  }
617
+ if (!isNaN(object1) || !isNaN(object2)) {
618
+ return +object1 === +object2;
619
+ }
617
620
  if (isDateObject(object1) && isDateObject(object2)) {
618
621
  return object1.getTime() === object2.getTime();
619
622
  }
@@ -706,6 +709,47 @@ function unset(object, path) {
706
709
  return object;
707
710
  }
708
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
+
709
753
  const defaultResult = {
710
754
  value: false,
711
755
  isValid: false,
@@ -828,49 +872,6 @@ function schemaErrorLookup(errors, _fields, name) {
828
872
  };
829
873
  }
830
874
 
831
- function deepMerge(target, source) {
832
- if (isPrimitive(target) || isPrimitive(source)) {
833
- return source;
834
- }
835
- for (const key in source) {
836
- const targetValue = target[key];
837
- const sourceValue = source[key];
838
- try {
839
- target[key] =
840
- (isObject(targetValue) && isObject(sourceValue)) ||
841
- (Array.isArray(targetValue) && Array.isArray(sourceValue))
842
- ? deepMerge(targetValue, sourceValue)
843
- : sourceValue;
844
- }
845
- catch (_a) { }
846
- }
847
- return target;
848
- }
849
-
850
- function setDirtyFields(values, defaultValues, dirtyFields, parentNode, parentName) {
851
- let index = -1;
852
- while (++index < values.length) {
853
- for (const key in values[index]) {
854
- if (Array.isArray(values[index][key])) {
855
- !dirtyFields[index] && (dirtyFields[index] = {});
856
- dirtyFields[index][key] = [];
857
- setDirtyFields(values[index][key], get(defaultValues[index] || {}, key, []), dirtyFields[index][key], dirtyFields[index], key);
858
- }
859
- else {
860
- !isNullOrUndefined(defaultValues) &&
861
- deepEqual(get(defaultValues[index] || {}, key), values[index][key])
862
- ? set(dirtyFields[index] || {}, key)
863
- : (dirtyFields[index] = Object.assign(Object.assign({}, dirtyFields[index]), { [key]: true }));
864
- }
865
- }
866
- parentNode &&
867
- !dirtyFields.length &&
868
- delete parentNode[parentName];
869
- }
870
- return dirtyFields;
871
- }
872
- var setFieldArrayDirtyFields = (values, defaultValues, dirtyFields) => deepMerge(setDirtyFields(values, defaultValues, dirtyFields.slice(0, values.length)), setDirtyFields(defaultValues, values, dirtyFields.slice(0, values.length)));
873
-
874
875
  var skipValidation = (isBlurEvent, isTouched, isSubmitted, reValidateMode, mode) => {
875
876
  if (mode.isOnAll) {
876
877
  return false;
@@ -887,8 +888,6 @@ var skipValidation = (isBlurEvent, isTouched, isSubmitted, reValidateMode, mode)
887
888
  return true;
888
889
  };
889
890
 
890
- var unsetEmptyArray = (ref, name) => !compact(get(ref, name, [])).length && unset(ref, name);
891
-
892
891
  var isMessage = (value) => isString(value) || React.isValidElement(value);
893
892
 
894
893
  var isRegex = (value) => value instanceof RegExp;
@@ -918,7 +917,7 @@ var validateField = async (field, inputValue, validateAllFieldCriteria, shouldUs
918
917
  return {};
919
918
  }
920
919
  const inputRef = refs ? refs[0] : ref;
921
- const setCustomValidty = (message) => {
920
+ const setCustomValidity = (message) => {
922
921
  if (shouldUseNativeValidation && inputRef.reportValidity) {
923
922
  inputRef.setCustomValidity(isBoolean(message) ? '' : message || ' ');
924
923
  inputRef.reportValidity();
@@ -948,7 +947,7 @@ var validateField = async (field, inputValue, validateAllFieldCriteria, shouldUs
948
947
  if (value) {
949
948
  error[name] = Object.assign({ type: INPUT_VALIDATION_RULES.required, message, ref: inputRef }, appendErrorsCurry(INPUT_VALIDATION_RULES.required, message));
950
949
  if (!validateAllFieldCriteria) {
951
- setCustomValidty(message);
950
+ setCustomValidity(message);
952
951
  return error;
953
952
  }
954
953
  }
@@ -979,7 +978,7 @@ var validateField = async (field, inputValue, validateAllFieldCriteria, shouldUs
979
978
  if (exceedMax || exceedMin) {
980
979
  getMinMaxMessage(!!exceedMax, maxOutput.message, minOutput.message, INPUT_VALIDATION_RULES.max, INPUT_VALIDATION_RULES.min);
981
980
  if (!validateAllFieldCriteria) {
982
- setCustomValidty(error[name].message);
981
+ setCustomValidity(error[name].message);
983
982
  return error;
984
983
  }
985
984
  }
@@ -994,7 +993,7 @@ var validateField = async (field, inputValue, validateAllFieldCriteria, shouldUs
994
993
  if (exceedMax || exceedMin) {
995
994
  getMinMaxMessage(exceedMax, maxLengthOutput.message, minLengthOutput.message);
996
995
  if (!validateAllFieldCriteria) {
997
- setCustomValidty(error[name].message);
996
+ setCustomValidity(error[name].message);
998
997
  return error;
999
998
  }
1000
999
  }
@@ -1005,7 +1004,7 @@ var validateField = async (field, inputValue, validateAllFieldCriteria, shouldUs
1005
1004
  error[name] = Object.assign({ type: INPUT_VALIDATION_RULES.pattern, message,
1006
1005
  ref }, appendErrorsCurry(INPUT_VALIDATION_RULES.pattern, message));
1007
1006
  if (!validateAllFieldCriteria) {
1008
- setCustomValidty(message);
1007
+ setCustomValidity(message);
1009
1008
  return error;
1010
1009
  }
1011
1010
  }
@@ -1017,7 +1016,7 @@ var validateField = async (field, inputValue, validateAllFieldCriteria, shouldUs
1017
1016
  if (validateError) {
1018
1017
  error[name] = Object.assign(Object.assign({}, validateError), appendErrorsCurry(INPUT_VALIDATION_RULES.validate, validateError.message));
1019
1018
  if (!validateAllFieldCriteria) {
1020
- setCustomValidty(validateError.message);
1019
+ setCustomValidity(validateError.message);
1021
1020
  return error;
1022
1021
  }
1023
1022
  }
@@ -1031,7 +1030,7 @@ var validateField = async (field, inputValue, validateAllFieldCriteria, shouldUs
1031
1030
  const validateError = getValidateError(await validate[key](inputValue), inputRef, key);
1032
1031
  if (validateError) {
1033
1032
  validationResult = Object.assign(Object.assign({}, validateError), appendErrorsCurry(key, validateError.message));
1034
- setCustomValidty(validateError.message);
1033
+ setCustomValidity(validateError.message);
1035
1034
  if (validateAllFieldCriteria) {
1036
1035
  error[name] = validationResult;
1037
1036
  }
@@ -1045,7 +1044,7 @@ var validateField = async (field, inputValue, validateAllFieldCriteria, shouldUs
1045
1044
  }
1046
1045
  }
1047
1046
  }
1048
- setCustomValidty(true);
1047
+ setCustomValidity(true);
1049
1048
  return error;
1050
1049
  };
1051
1050
 
@@ -1132,16 +1131,14 @@ function createFormControl(props = {}) {
1132
1131
  if (Array.isArray(get(_formState.errors, name))) {
1133
1132
  const errors = method(get(_formState.errors, name), args.argA, args.argB);
1134
1133
  shouldSetValues && set(_formState.errors, name, errors);
1135
- unsetEmptyArray(_formState.errors, name);
1136
1134
  }
1137
1135
  if (_proxyFormState.touchedFields && get(_formState.touchedFields, name)) {
1138
1136
  const touchedFields = method(get(_formState.touchedFields, name), args.argA, args.argB);
1139
1137
  shouldSetValues &&
1140
1138
  set(_formState.touchedFields, name, touchedFields);
1141
- unsetEmptyArray(_formState.touchedFields, name);
1142
1139
  }
1143
1140
  if (_proxyFormState.dirtyFields || _proxyFormState.isDirty) {
1144
- updateFieldArrayDirty(name, values);
1141
+ _formState.dirtyFields = getDirtyFields(_defaultValues, _formValues);
1145
1142
  }
1146
1143
  _subjects.state.next({
1147
1144
  isDirty: _getDirty(name, values),
@@ -1199,8 +1196,6 @@ function createFormControl(props = {}) {
1199
1196
  isFieldDirty && shouldRender && _subjects.state.next(output);
1200
1197
  return isFieldDirty ? output : {};
1201
1198
  };
1202
- const updateFieldArrayDirty = (name, value) => (set(_formState.dirtyFields, name, setFieldArrayDirtyFields(value, get(_defaultValues, name, []), get(_formState.dirtyFields, name, []))),
1203
- unsetEmptyArray(_formState.dirtyFields, name));
1204
1199
  const shouldRenderByError = async (shouldSkipRender, name, isValid, error, fieldState) => {
1205
1200
  const previousFieldError = get(_formState.errors, name);
1206
1201
  const shouldUpdateValid = _proxyFormState.isValid && _formState.isValid !== isValid;
@@ -1312,10 +1307,7 @@ function createFormControl(props = {}) {
1312
1307
  isWeb && isHTMLElement(fieldReference.ref) && isNullOrUndefined(value)
1313
1308
  ? ''
1314
1309
  : value;
1315
- if (isFileInput(fieldReference.ref) && !isString(fieldValue)) {
1316
- fieldReference.ref.files = fieldValue;
1317
- }
1318
- else if (isMultipleSelect(fieldReference.ref)) {
1310
+ if (isMultipleSelect(fieldReference.ref)) {
1319
1311
  [...fieldReference.ref.options].forEach((selectRef) => (selectRef.selected = fieldValue.includes(selectRef.value)));
1320
1312
  }
1321
1313
  else if (fieldReference.refs) {
@@ -1330,7 +1322,7 @@ function createFormControl(props = {}) {
1330
1322
  fieldReference.refs.forEach((radioRef) => (radioRef.checked = radioRef.value === fieldValue));
1331
1323
  }
1332
1324
  }
1333
- else {
1325
+ else if (!isFileInput(fieldReference.ref)) {
1334
1326
  fieldReference.ref.value = fieldValue;
1335
1327
  }
1336
1328
  }
@@ -1363,7 +1355,7 @@ function createFormControl(props = {}) {
1363
1355
  });
1364
1356
  if ((_proxyFormState.isDirty || _proxyFormState.dirtyFields) &&
1365
1357
  options.shouldDirty) {
1366
- updateFieldArrayDirty(name, value);
1358
+ _formState.dirtyFields = getDirtyFields(_defaultValues, _formValues);
1367
1359
  _subjects.state.next({
1368
1360
  name,
1369
1361
  dirtyFields: _formState.dirtyFields,
@@ -1388,7 +1380,9 @@ function createFormControl(props = {}) {
1388
1380
  if (field) {
1389
1381
  let error;
1390
1382
  let isValid;
1391
- const fieldValue = target.type ? getFieldValue(field._f) : target.value;
1383
+ const fieldValue = target.type
1384
+ ? getFieldValue(field._f)
1385
+ : getEventValue(event);
1392
1386
  const isBlurEvent = event.type === EVENTS.BLUR;
1393
1387
  const shouldSkipValidation = (!hasValidation(field._f) &&
1394
1388
  !_options.resolver &&