react-hook-form 7.61.1 → 7.63.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.
@@ -1 +1 @@
1
- {"version":3,"file":"createFormControl.d.ts","sourceRoot":"","sources":["../../src/logic/createFormControl.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAaV,WAAW,EAgBX,YAAY,EAIZ,aAAa,EAUd,MAAM,UAAU,CAAC;AAoDlB,wBAAgB,iBAAiB,CAC/B,YAAY,SAAS,WAAW,GAAG,WAAW,EAC9C,QAAQ,GAAG,GAAG,EACd,kBAAkB,GAAG,YAAY,EAEjC,KAAK,GAAE,YAAY,CAAC,YAAY,EAAE,QAAQ,EAAE,kBAAkB,CAAM,GACnE,IAAI,CACL,aAAa,CAAC,YAAY,EAAE,QAAQ,EAAE,kBAAkB,CAAC,EACzD,WAAW,CACZ,GAAG;IACF,WAAW,EAAE,IAAI,CACf,aAAa,CAAC,YAAY,EAAE,QAAQ,EAAE,kBAAkB,CAAC,EACzD,WAAW,CACZ,CAAC;CACH,CA46CA"}
1
+ {"version":3,"file":"createFormControl.d.ts","sourceRoot":"","sources":["../../src/logic/createFormControl.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAaV,WAAW,EAiBX,YAAY,EAIZ,aAAa,EAUd,MAAM,UAAU,CAAC;AAqDlB,wBAAgB,iBAAiB,CAC/B,YAAY,SAAS,WAAW,GAAG,WAAW,EAC9C,QAAQ,GAAG,GAAG,EACd,kBAAkB,GAAG,YAAY,EAEjC,KAAK,GAAE,YAAY,CAAC,YAAY,EAAE,QAAQ,EAAE,kBAAkB,CAAM,GACnE,IAAI,CACL,aAAa,CAAC,YAAY,EAAE,QAAQ,EAAE,kBAAkB,CAAC,EACzD,WAAW,CACZ,GAAG;IACF,WAAW,EAAE,IAAI,CACf,aAAa,CAAC,YAAY,EAAE,QAAQ,EAAE,kBAAkB,CAAC,EACzD,WAAW,CACZ,CAAC;CACH,CA07CA"}
@@ -1,3 +1,4 @@
1
- declare const _default: <T>(defaultValues: T, formValues: T) => Record<Extract<keyof T, string>, boolean | Record<string, any>>;
2
- export default _default;
1
+ declare function markFieldsDirty<T>(data: T, fields?: Record<string, any>): Record<string, any>;
2
+ export default function getDirtyFields<T>(data: T, formValues: T, dirtyFieldsFromValues?: Record<Extract<keyof T, string>, ReturnType<typeof markFieldsDirty> | boolean>): Record<Extract<keyof T, string>, boolean | Record<string, any>>;
3
+ export {};
3
4
  //# sourceMappingURL=getDirtyFields.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"getDirtyFields.d.ts","sourceRoot":"","sources":["../../src/logic/getDirtyFields.ts"],"names":[],"mappings":"yBAkEgB,CAAC,EAAE,eAAe,CAAC,EAAE,YAAY,CAAC;AAAlD,wBAKI"}
1
+ {"version":3,"file":"getDirtyFields.d.ts","sourceRoot":"","sources":["../../src/logic/getDirtyFields.ts"],"names":[],"mappings":"AAWA,iBAAS,eAAe,CAAC,CAAC,EAAE,IAAI,EAAE,CAAC,EAAE,MAAM,GAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAM,uBAWpE;AAED,MAAM,CAAC,OAAO,UAAU,cAAc,CAAC,CAAC,EACtC,IAAI,EAAE,CAAC,EACP,UAAU,EAAE,CAAC,EACb,qBAAqB,CAAC,EAAE,MAAM,CAC5B,OAAO,CAAC,MAAM,CAAC,EAAE,MAAM,CAAC,EACxB,UAAU,CAAC,OAAO,eAAe,CAAC,GAAG,OAAO,CAC7C,mEA0BF"}
@@ -1 +1 @@
1
- {"version":3,"file":"validateField.d.ts","sourceRoot":"","sources":["../../src/logic/validateField.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACV,KAAK,EAEL,WAAW,EACX,mBAAmB,EACnB,eAAe,EAKhB,MAAM,UAAU,CAAC;yBAsBI,CAAC,SAAS,WAAW,EACzC,OAAO,KAAK,EACZ,oBAAoB,eAAe,EACnC,YAAY,CAAC,EACb,0BAA0B,OAAO,EACjC,4BAA4B,OAAO,EACnC,eAAe,OAAO,KACrB,OAAO,CAAC,mBAAmB,CAAC;AAP/B,wBA6PE"}
1
+ {"version":3,"file":"validateField.d.ts","sourceRoot":"","sources":["../../src/logic/validateField.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACV,KAAK,EAEL,WAAW,EACX,mBAAmB,EACnB,eAAe,EAKhB,MAAM,UAAU,CAAC;yBAqBI,CAAC,SAAS,WAAW,EACzC,OAAO,KAAK,EACZ,oBAAoB,eAAe,EACnC,YAAY,CAAC,EACb,0BAA0B,OAAO,EACjC,4BAA4B,OAAO,EACnC,eAAe,OAAO,KACrB,OAAO,CAAC,mBAAmB,CAAC;AAP/B,wBA6PE"}
@@ -56,7 +56,7 @@ function cloneObject(data) {
56
56
  }
57
57
  else if (!(isWeb && (data instanceof Blob || isFileListInstance)) &&
58
58
  (isArray || isObject(data))) {
59
- copy = isArray ? [] : {};
59
+ copy = isArray ? [] : Object.create(Object.getPrototypeOf(data));
60
60
  if (!isArray && !isPlainObject(data)) {
61
61
  copy = data;
62
62
  }
@@ -144,6 +144,26 @@ function deepEqual(object1, object2, _internal_visited = new WeakSet()) {
144
144
  return true;
145
145
  }
146
146
 
147
+ function extractFormValues(fieldsState, formValues) {
148
+ const values = {};
149
+ for (const key in fieldsState) {
150
+ if (fieldsState.hasOwnProperty(key)) {
151
+ const fieldState = fieldsState[key];
152
+ const fieldValue = formValues[key];
153
+ if (fieldState && isObject(fieldState) && fieldValue) {
154
+ const nestedFieldsState = extractFormValues(fieldState, fieldValue);
155
+ if (isObject(nestedFieldsState)) {
156
+ values[key] = nestedFieldsState;
157
+ }
158
+ }
159
+ else if (fieldsState[key]) {
160
+ values[key] = fieldValue;
161
+ }
162
+ }
163
+ }
164
+ return values;
165
+ }
166
+
147
167
  var isKey = (value) => /^\w*$/.test(value);
148
168
 
149
169
  var isUndefined = (val) => val === undefined;
@@ -274,46 +294,40 @@ var objectHasFunction = (data) => {
274
294
  return false;
275
295
  };
276
296
 
297
+ function isTraversable(value) {
298
+ return Array.isArray(value) || (isObject(value) && !objectHasFunction(value));
299
+ }
277
300
  function markFieldsDirty(data, fields = {}) {
278
- const isParentNodeArray = Array.isArray(data);
279
- if (isObject(data) || isParentNodeArray) {
280
- for (const key in data) {
281
- if (Array.isArray(data[key]) ||
282
- (isObject(data[key]) && !objectHasFunction(data[key]))) {
283
- fields[key] = Array.isArray(data[key]) ? [] : {};
284
- markFieldsDirty(data[key], fields[key]);
285
- }
286
- else if (!isNullOrUndefined(data[key])) {
287
- fields[key] = true;
288
- }
301
+ for (const key in data) {
302
+ if (isTraversable(data[key])) {
303
+ fields[key] = Array.isArray(data[key]) ? [] : {};
304
+ markFieldsDirty(data[key], fields[key]);
305
+ }
306
+ else if (!isNullOrUndefined(data[key])) {
307
+ fields[key] = true;
289
308
  }
290
309
  }
291
310
  return fields;
292
311
  }
293
- function getDirtyFieldsFromDefaultValues(data, formValues, dirtyFieldsFromValues) {
294
- const isParentNodeArray = Array.isArray(data);
295
- if (isObject(data) || isParentNodeArray) {
296
- for (const key in data) {
297
- if (Array.isArray(data[key]) ||
298
- (isObject(data[key]) && !objectHasFunction(data[key]))) {
299
- if (isUndefined(formValues) ||
300
- isPrimitive(dirtyFieldsFromValues[key])) {
301
- dirtyFieldsFromValues[key] = Array.isArray(data[key])
302
- ? markFieldsDirty(data[key], [])
303
- : { ...markFieldsDirty(data[key]) };
304
- }
305
- else {
306
- getDirtyFieldsFromDefaultValues(data[key], isNullOrUndefined(formValues) ? {} : formValues[key], dirtyFieldsFromValues[key]);
307
- }
312
+ function getDirtyFields(data, formValues, dirtyFieldsFromValues) {
313
+ if (!dirtyFieldsFromValues) {
314
+ dirtyFieldsFromValues = markFieldsDirty(formValues);
315
+ }
316
+ for (const key in data) {
317
+ if (isTraversable(data[key])) {
318
+ if (isUndefined(formValues) || isPrimitive(dirtyFieldsFromValues[key])) {
319
+ dirtyFieldsFromValues[key] = markFieldsDirty(data[key], Array.isArray(data[key]) ? [] : {});
308
320
  }
309
321
  else {
310
- dirtyFieldsFromValues[key] = !deepEqual(data[key], formValues[key]);
322
+ getDirtyFields(data[key], isNullOrUndefined(formValues) ? {} : formValues[key], dirtyFieldsFromValues[key]);
311
323
  }
312
324
  }
325
+ else {
326
+ dirtyFieldsFromValues[key] = !deepEqual(data[key], formValues[key]);
327
+ }
313
328
  }
314
329
  return dirtyFieldsFromValues;
315
330
  }
316
- var getDirtyFields = (defaultValues, formValues) => getDirtyFieldsFromDefaultValues(defaultValues, formValues, markFieldsDirty(formValues));
317
331
 
318
332
  var getEventValue = (event) => isObject(event) && event.target
319
333
  ? isCheckBoxInput(event.target)
@@ -557,15 +571,13 @@ var updateFieldArrayRootError = (errors, error, name) => {
557
571
  return errors;
558
572
  };
559
573
 
560
- var isMessage = (value) => isString(value);
561
-
562
574
  function getValidateError(result, ref, type = 'validate') {
563
- if (isMessage(result) ||
564
- (Array.isArray(result) && result.every(isMessage)) ||
575
+ if (isString(result) ||
576
+ (Array.isArray(result) && result.every(isString)) ||
565
577
  (isBoolean(result) && !result)) {
566
578
  return {
567
579
  type,
568
- message: isMessage(result) ? result : '',
580
+ message: isString(result) ? result : '',
569
581
  ref,
570
582
  };
571
583
  }
@@ -618,7 +630,7 @@ var validateField = async (field, disabledFieldNames, formValues, validateAllFie
618
630
  (isBoolean(inputValue) && !inputValue) ||
619
631
  (isCheckBox && !getCheckboxValue(refs).isValid) ||
620
632
  (isRadio && !getRadioValue(refs).isValid))) {
621
- const { value, message } = isMessage(required)
633
+ const { value, message } = isString(required)
622
634
  ? { value: !!required, message: required }
623
635
  : getValueAndMessage(required);
624
636
  if (value) {
@@ -1024,11 +1036,11 @@ function createFormControl(props = {}) {
1024
1036
  const isFieldArrayRoot = _names.array.has(_f.name);
1025
1037
  const isPromiseFunction = field._f && hasPromiseValidation(field._f);
1026
1038
  if (isPromiseFunction && _proxyFormState.validatingFields) {
1027
- _updateIsValidating([name], true);
1039
+ _updateIsValidating([_f.name], true);
1028
1040
  }
1029
1041
  const fieldError = await validateField(field, _names.disabled, _formValues, shouldDisplayAllAssociatedErrors, _options.shouldUseNativeValidation && !shouldOnlyCheckValid, isFieldArrayRoot);
1030
1042
  if (isPromiseFunction && _proxyFormState.validatingFields) {
1031
- _updateIsValidating([name]);
1043
+ _updateIsValidating([_f.name]);
1032
1044
  }
1033
1045
  if (fieldError[_f.name]) {
1034
1046
  context.valid = false;
@@ -1202,8 +1214,10 @@ function createFormControl(props = {}) {
1202
1214
  const watched = isWatched(name, _names, isBlurEvent);
1203
1215
  set(_formValues, name, fieldValue);
1204
1216
  if (isBlurEvent) {
1205
- field._f.onBlur && field._f.onBlur(event);
1206
- delayErrorCallback && delayErrorCallback(0);
1217
+ if (!target || !target.readOnly) {
1218
+ field._f.onBlur && field._f.onBlur(event);
1219
+ delayErrorCallback && delayErrorCallback(0);
1220
+ }
1207
1221
  }
1208
1222
  else if (field._f.onChange) {
1209
1223
  field._f.onChange(event);
@@ -1259,6 +1273,7 @@ function createFormControl(props = {}) {
1259
1273
  }
1260
1274
  if (isFieldValueUpdated) {
1261
1275
  field._f.deps &&
1276
+ (!Array.isArray(field._f.deps) || field._f.deps.length > 0) &&
1262
1277
  trigger(field._f.deps);
1263
1278
  shouldRenderByError(name, isValid, error, fieldState);
1264
1279
  }
@@ -1306,10 +1321,13 @@ function createFormControl(props = {}) {
1306
1321
  iterateFieldsByAction(_fields, _focusInput, name ? fieldNames : _names.mount);
1307
1322
  return validationResult;
1308
1323
  };
1309
- const getValues = (fieldNames) => {
1310
- const values = {
1324
+ const getValues = (fieldNames, config) => {
1325
+ let values = {
1311
1326
  ...(_state.mount ? _formValues : _defaultValues),
1312
1327
  };
1328
+ if (config) {
1329
+ values = extractFormValues(config.dirtyFields ? _formState.dirtyFields : _formState.touchedFields, values);
1330
+ }
1313
1331
  return isUndefined(fieldNames)
1314
1332
  ? values
1315
1333
  : isString(fieldNames)
@@ -1698,6 +1716,7 @@ function createFormControl(props = {}) {
1698
1716
  ? _formState.isSubmitSuccessful
1699
1717
  : false,
1700
1718
  isSubmitting: false,
1719
+ defaultValues: _defaultValues,
1701
1720
  });
1702
1721
  };
1703
1722
  const reset = (formValues, keepStateOptions) => _reset(isFunction(formValues)