react-f0rm 0.9.0 → 0.10.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.
package/dist/index.umd.js CHANGED
@@ -222,6 +222,7 @@
222
222
  mode: options?.mode ?? "onSubmit",
223
223
  reValidateMode: options?.reValidateMode ?? "onChange",
224
224
  disabled: options?.disabled ?? false,
225
+ validateDeps: options?.validateDeps ? new Set(options.validateDeps.map((dep) => create$1(dep).key)) : void 0,
225
226
  initialValues: options?.initialValues ?? {},
226
227
  values: /* @__PURE__ */ new Map(),
227
228
  deleted: /* @__PURE__ */ new Set(),
@@ -594,34 +595,75 @@
594
595
  function isFieldError(value) {
595
596
  return !!value && typeof value === "object" && typeof value.type === "string" && typeof value.message === "string";
596
597
  }
597
- function setFormErrors(form, result, segments = []) {
598
+ function setFormErrors(form, result, segments = [], footprint) {
598
599
  Object.entries(result).forEach(([key, value]) => {
599
600
  const path = [...segments, ...normalizePath(key)];
600
601
  if (typeof value === "string") {
601
- if (value) setError(form, path, value);
602
+ if (value) {
603
+ setError(form, path, value);
604
+ recordFootprint(form, path, footprint);
605
+ }
602
606
  } else if (Array.isArray(value)) {
603
607
  setError(form, path, value);
608
+ recordFootprint(form, path, footprint);
604
609
  } else if (isFieldError(value)) {
605
610
  setError(form, path, value);
611
+ recordFootprint(form, path, footprint);
606
612
  } else if (value && typeof value === "object") {
607
- setFormErrors(form, value, path);
613
+ setFormErrors(form, value, path, footprint);
608
614
  }
609
615
  });
610
616
  }
617
+ function recordFootprint(form, segments, footprint) {
618
+ if (!footprint) return;
619
+ const path = create$1(segments);
620
+ const stored = form.errors.get(path.key);
621
+ if (stored) footprint.set(path.key, stored);
622
+ }
611
623
  function setParsedValues(form, values) {
612
624
  if (values === void 0 || values === form.parsedValues) return;
613
625
  form.parsedValues = values;
614
626
  emit(form.emitter, "change");
615
627
  }
616
628
  function applyValidateResult(form, result) {
629
+ const footprint = form.validateDeps ? getFormErrorFootprint(form) : void 0;
630
+ if (footprint) {
631
+ clearFormValidateErrors(form, footprint);
632
+ footprint.clear();
633
+ }
617
634
  if (!result) return;
618
635
  if (typeof result === "object" && VALIDATION_OUTCOME in result) {
619
636
  const outcome = result;
620
- if (outcome.errors) setFormErrors(form, outcome.errors);
637
+ if (outcome.errors) setFormErrors(form, outcome.errors, [], footprint);
621
638
  setParsedValues(form, outcome.values);
622
639
  return;
623
640
  }
624
- setFormErrors(form, result);
641
+ setFormErrors(form, result, [], footprint);
642
+ }
643
+ const formErrorFootprints = /* @__PURE__ */ new WeakMap();
644
+ function getFormErrorFootprint(form) {
645
+ let footprint = formErrorFootprints.get(form);
646
+ if (!footprint) {
647
+ footprint = /* @__PURE__ */ new Map();
648
+ formErrorFootprints.set(form, footprint);
649
+ }
650
+ return footprint;
651
+ }
652
+ function hasFormValidateErrors(form) {
653
+ const footprint = formErrorFootprints.get(form);
654
+ if (!footprint) return false;
655
+ for (const [key, written] of footprint) {
656
+ if (form.errors.get(key) === written) return true;
657
+ }
658
+ return false;
659
+ }
660
+ function clearFormValidateErrors(form, footprint) {
661
+ for (const [key, written] of footprint) {
662
+ const stored = form.errors.get(key);
663
+ if (stored !== written) continue;
664
+ form.errors.delete(key);
665
+ emit(form.emitter, "errors", create$1(JSON.parse(key)));
666
+ }
625
667
  }
626
668
  const FORM_VALIDATING_KEY = "__form_validate__";
627
669
  function fieldsSettled(form) {
@@ -715,6 +757,13 @@
715
757
  else waiter.reject(outcome);
716
758
  }
717
759
  }
760
+ function revalidateFormOnChange(form, path, mode) {
761
+ if (!form.validateDeps?.has(path.key) || !form.validate) return;
762
+ if (mode === "onChange" || mode === "all" || mode === "onTouched" && hasTouchedByPath(form, path) || form.reValidateMode === "onChange" && hasFormValidateErrors(form)) {
763
+ runFormValidate(form).catch(() => {
764
+ });
765
+ }
766
+ }
718
767
  async function ensureValidate(form) {
719
768
  form.validators.forEach((validator) => validator());
720
769
  await waitUntil(
@@ -1466,6 +1515,7 @@
1466
1515
  setValueByPath(form, path, v);
1467
1516
  if (mode === "onChange" || mode === "all" || mode === "onTouched" && hasTouchedByPath(form, path) || liveErrors.length > 0 && form.reValidateMode === "onChange")
1468
1517
  validator();
1518
+ revalidateFormOnChange(form, path, mode);
1469
1519
  });
1470
1520
  React.useEffect(() => {
1471
1521
  form.changeHandlers.set(path.key, onChange);
@@ -1938,6 +1988,7 @@
1938
1988
  exports.removeFieldByPath = removeFieldByPath;
1939
1989
  exports.reset = reset;
1940
1990
  exports.resetField = resetField;
1991
+ exports.revalidateFormOnChange = revalidateFormOnChange;
1941
1992
  exports.setDisabled = setDisabled;
1942
1993
  exports.setError = setError;
1943
1994
  exports.setErrorByPath = setErrorByPath;