@bombillazo/rhf-plus 7.62.0-plus.4 → 7.62.0-plus.6

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,EACX,YAAY,EAgBZ,YAAY,EAIZ,aAAa,EAUd,MAAM,UAAU,CAAC;AAuDlB,wBAAgB,iBAAiB,CAC/B,YAAY,SAAS,WAAW,GAAG,WAAW,EAC9C,QAAQ,GAAG,GAAG,EACd,kBAAkB,GAAG,YAAY,EACjC,SAAS,SAAS,YAAY,GAAG,GAAG,EAEpC,KAAK,GAAE,YAAY,CACjB,YAAY,EACZ,QAAQ,EACR,kBAAkB,EAClB,SAAS,CACL,GACL,IAAI,CACL,aAAa,CAAC,YAAY,EAAE,QAAQ,EAAE,kBAAkB,EAAE,SAAS,CAAC,EACpE,WAAW,CACZ,GAAG;IACF,WAAW,EAAE,IAAI,CACf,aAAa,CAAC,YAAY,EAAE,QAAQ,EAAE,kBAAkB,EAAE,SAAS,CAAC,EACpE,WAAW,CACZ,CAAC;CACH,CA2kDA"}
1
+ {"version":3,"file":"createFormControl.d.ts","sourceRoot":"","sources":["../../src/logic/createFormControl.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAaV,WAAW,EACX,YAAY,EAgBZ,YAAY,EAIZ,aAAa,EAUd,MAAM,UAAU,CAAC;AAuDlB,wBAAgB,iBAAiB,CAC/B,YAAY,SAAS,WAAW,GAAG,WAAW,EAC9C,QAAQ,GAAG,GAAG,EACd,kBAAkB,GAAG,YAAY,EACjC,SAAS,SAAS,YAAY,GAAG,GAAG,EAEpC,KAAK,GAAE,YAAY,CACjB,YAAY,EACZ,QAAQ,EACR,kBAAkB,EAClB,SAAS,CACL,GACL,IAAI,CACL,aAAa,CAAC,YAAY,EAAE,QAAQ,EAAE,kBAAkB,EAAE,SAAS,CAAC,EACpE,WAAW,CACZ,GAAG;IACF,WAAW,EAAE,IAAI,CACf,aAAa,CAAC,YAAY,EAAE,QAAQ,EAAE,kBAAkB,EAAE,SAAS,CAAC,EACpE,WAAW,CACZ,CAAC;CACH,CAipDA"}
@@ -845,6 +845,8 @@ function createFormControl(props = {}) {
845
845
  let _formState = {
846
846
  submitCount: 0,
847
847
  isDirty: false,
848
+ isDirtySinceSubmit: false,
849
+ hasBeenSubmitted: false,
848
850
  isReady: false,
849
851
  isLoading: _internalLoading,
850
852
  isValidating: false,
@@ -885,6 +887,8 @@ function createFormControl(props = {}) {
885
887
  let timer = 0;
886
888
  const _proxyFormState = {
887
889
  isDirty: false,
890
+ isDirtySinceSubmit: false,
891
+ hasBeenSubmitted: false,
888
892
  dirtyFields: false,
889
893
  validatingFields: false,
890
894
  touchedFields: false,
@@ -902,6 +906,8 @@ function createFormControl(props = {}) {
902
906
  };
903
907
  const shouldDisplayAllAssociatedErrors = _options.criteriaMode === VALIDATION_MODE.all;
904
908
  const id = createId(props.id);
909
+ // Track if form was ever submitted (persists through resets)
910
+ let _hasBeenSubmitted = false;
905
911
  const debounce = (callback) => (wait) => {
906
912
  clearTimeout(timer);
907
913
  timer = setTimeout(callback, wait);
@@ -1025,6 +1031,20 @@ function createFormControl(props = {}) {
1025
1031
  _proxySubscribeFormState.dirtyFields) &&
1026
1032
  isPreviousDirty !== !isCurrentFieldPristine);
1027
1033
  }
1034
+ // Set isDirtySinceSubmit to true if form was ever submitted and a field value is being changed
1035
+ // For change events (not blur/focus), always set if form was ever submitted
1036
+ // shouldDirty is true for onChange events, false for blur
1037
+ if ((_formState.isSubmitted || _hasBeenSubmitted) &&
1038
+ !_formState.isDirtySinceSubmit &&
1039
+ !isBlurEvent &&
1040
+ !isFocusEvent &&
1041
+ shouldDirty) {
1042
+ _formState.isDirtySinceSubmit = output.isDirtySinceSubmit = true;
1043
+ shouldUpdateField =
1044
+ shouldUpdateField ||
1045
+ !!(_proxyFormState.isDirtySinceSubmit ||
1046
+ _proxySubscribeFormState.isDirtySinceSubmit);
1047
+ }
1028
1048
  if (isBlurEvent) {
1029
1049
  const isPreviousFieldTouched = get(_formState.touchedFields, name);
1030
1050
  if (!isPreviousFieldTouched) {
@@ -1220,9 +1240,15 @@ function createFormControl(props = {}) {
1220
1240
  }
1221
1241
  }
1222
1242
  }
1223
- (options.shouldDirty || options.shouldTouch) &&
1243
+ const isSubmittedAndDirty = (_formState.isSubmitted || _hasBeenSubmitted) &&
1244
+ !deepEqual(get(_defaultValues, name), fieldValue);
1245
+ // If the form was submitted, track value changes for isDirtySinceSubmit
1246
+ // only when the value actually differs from the default value,
1247
+ // even if shouldDirty is not explicitly set
1248
+ const shouldTrackChange = options.shouldDirty || options.shouldTouch || isSubmittedAndDirty;
1249
+ shouldTrackChange &&
1224
1250
  updateTouchAndDirty(name, fieldValue, options.shouldTouch, false, // isFocusEvent - not applicable for setValue
1225
- options.shouldDirty, true);
1251
+ options.shouldDirty || isSubmittedAndDirty, true);
1226
1252
  options.shouldValidate && trigger(name);
1227
1253
  };
1228
1254
  const setValues = (name, value, options) => {
@@ -1254,12 +1280,25 @@ function createFormControl(props = {}) {
1254
1280
  if ((_proxyFormState.isDirty ||
1255
1281
  _proxyFormState.dirtyFields ||
1256
1282
  _proxySubscribeFormState.isDirty ||
1257
- _proxySubscribeFormState.dirtyFields) &&
1283
+ _proxySubscribeFormState.dirtyFields ||
1284
+ _proxyFormState.isDirtySinceSubmit ||
1285
+ _proxySubscribeFormState.isDirtySinceSubmit) &&
1258
1286
  options.shouldDirty) {
1259
1287
  _subjects.state.next({
1260
1288
  name,
1261
1289
  dirtyFields: getDirtyFields(_defaultValues, _formValues),
1262
1290
  isDirty: _getDirty(name, cloneValue),
1291
+ ...((_formState.isSubmitted || _hasBeenSubmitted) &&
1292
+ !_formState.isDirtySinceSubmit
1293
+ ? { isDirtySinceSubmit: true }
1294
+ : {}),
1295
+ });
1296
+ }
1297
+ else if ((_formState.isSubmitted || _hasBeenSubmitted) &&
1298
+ !_formState.isDirtySinceSubmit) {
1299
+ _subjects.state.next({
1300
+ name,
1301
+ isDirtySinceSubmit: true,
1263
1302
  });
1264
1303
  }
1265
1304
  }
@@ -1273,6 +1312,14 @@ function createFormControl(props = {}) {
1273
1312
  name: _state.mount ? name : undefined,
1274
1313
  values: cloneObject(_formValues),
1275
1314
  });
1315
+ // Trigger validation when shouldValidate is true
1316
+ // This ensures validation happens for all cases including:
1317
+ // - Field arrays
1318
+ // - Empty arrays
1319
+ // - Nested fields with array values
1320
+ if (options.shouldValidate) {
1321
+ trigger(name);
1322
+ }
1276
1323
  };
1277
1324
  const onChange = async (event) => {
1278
1325
  _state.mount = true;
@@ -1328,7 +1375,7 @@ function createFormControl(props = {}) {
1328
1375
  else if (field._f.onChange) {
1329
1376
  field._f.onChange(event);
1330
1377
  }
1331
- const fieldState = updateTouchAndDirty(name, fieldValue, isBlurEvent, isFocusEvent);
1378
+ const fieldState = updateTouchAndDirty(name, fieldValue, isBlurEvent, isFocusEvent, !isBlurEvent);
1332
1379
  const shouldRender = !isEmptyObject(fieldState) || watched;
1333
1380
  !isBlurEvent &&
1334
1381
  _subjects.state.next({
@@ -1712,11 +1759,14 @@ function createFormControl(props = {}) {
1712
1759
  _focusError();
1713
1760
  setTimeout(_focusError);
1714
1761
  }
1762
+ _hasBeenSubmitted = true; // Mark that form was submitted at least once
1715
1763
  _subjects.state.next({
1716
1764
  isSubmitted: true,
1717
1765
  isSubmitting: false,
1718
1766
  isSubmitSuccessful: isEmptyObject(_formState.errors) && !onValidError,
1719
1767
  submitCount: _formState.submitCount + 1,
1768
+ isDirtySinceSubmit: false,
1769
+ hasBeenSubmitted: _hasBeenSubmitted,
1720
1770
  errors: _formState.errors,
1721
1771
  });
1722
1772
  if (onValidError) {
@@ -1779,7 +1829,7 @@ function createFormControl(props = {}) {
1779
1829
  if (isHTMLElement(fieldReference)) {
1780
1830
  const form = fieldReference.closest('form');
1781
1831
  if (form) {
1782
- form.reset();
1832
+ HTMLFormElement.prototype.reset.call(form);
1783
1833
  break;
1784
1834
  }
1785
1835
  }
@@ -1831,6 +1881,8 @@ function createFormControl(props = {}) {
1831
1881
  ? _formState.isDirty
1832
1882
  : !!(keepStateOptions.keepDefaultValues &&
1833
1883
  !deepEqual(formValues, _defaultValues)),
1884
+ isDirtySinceSubmit: false,
1885
+ hasBeenSubmitted: _hasBeenSubmitted, // Persist the hasBeenSubmitted flag
1834
1886
  isSubmitted: keepStateOptions.keepIsSubmitted
1835
1887
  ? _formState.isSubmitted
1836
1888
  : false,