formstand 0.14.0 → 0.15.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.d.cts CHANGED
@@ -19,6 +19,7 @@ type PersistOptions<TSchema extends z.ZodType> = Readonly<{
19
19
  key: string;
20
20
  storage?: PersistStorage;
21
21
  debounceMs?: number;
22
+ version?: string | number;
22
23
  apply?: "adopt" | "restore" | "manual";
23
24
  }>;
24
25
  type PersistHandle = Readonly<{
package/dist/index.d.ts CHANGED
@@ -19,6 +19,7 @@ type PersistOptions<TSchema extends z.ZodType> = Readonly<{
19
19
  key: string;
20
20
  storage?: PersistStorage;
21
21
  debounceMs?: number;
22
+ version?: string | number;
22
23
  apply?: "adopt" | "restore" | "manual";
23
24
  }>;
24
25
  type PersistHandle = Readonly<{
package/dist/index.js CHANGED
@@ -1006,6 +1006,25 @@ var shouldValidateOn = (trigger, mode, reValidateMode, submitAttempted, touched
1006
1006
  };
1007
1007
 
1008
1008
  // src/core/persist.ts
1009
+ var kindOf = (value) => value === null ? "null" : Array.isArray(value) ? "array" : typeof value;
1010
+ var conflictsWith = (draft, reference) => {
1011
+ if (draft === void 0 || draft === null) return false;
1012
+ if (reference === void 0 || reference === null) return false;
1013
+ const draftKind = kindOf(draft);
1014
+ const referenceKind = kindOf(reference);
1015
+ if (draftKind !== referenceKind) return true;
1016
+ if (draftKind === "array") {
1017
+ const [d] = draft;
1018
+ const [r] = reference;
1019
+ return conflictsWith(d, r);
1020
+ }
1021
+ if (draftKind === "object") {
1022
+ const d = draft;
1023
+ const r = reference;
1024
+ return Object.keys(d).some((key) => conflictsWith(d[key], r[key]));
1025
+ }
1026
+ return false;
1027
+ };
1009
1028
  var defaultStorage = () => {
1010
1029
  try {
1011
1030
  return typeof globalThis.localStorage === "undefined" ? null : globalThis.localStorage;
@@ -1018,11 +1037,29 @@ var persistForm = (form, options) => {
1018
1037
  const debounceMs = options.debounceMs ?? 300;
1019
1038
  const apply = options.apply ?? "adopt";
1020
1039
  const applyDraft = (values) => apply === "restore" ? form.setValues(values) : form.adoptValues(values);
1040
+ const referenceShape = () => {
1041
+ try {
1042
+ return JSON.parse(JSON.stringify(form.getState().initialValues));
1043
+ } catch {
1044
+ return void 0;
1045
+ }
1046
+ };
1021
1047
  const restore = () => {
1022
1048
  try {
1023
1049
  const raw = storage === null ? null : storage.getItem(options.key);
1024
1050
  if (raw === null) return false;
1025
- applyDraft(JSON.parse(raw));
1051
+ const parsed = JSON.parse(raw);
1052
+ const versioned = options.version !== void 0 && typeof parsed === "object" && parsed !== null && "__v" in parsed;
1053
+ if (options.version !== void 0 && !versioned) return false;
1054
+ const wrapper = parsed;
1055
+ if (versioned && wrapper.__v !== options.version) return false;
1056
+ if (versioned && wrapper.values === void 0) return false;
1057
+ const values = versioned ? wrapper.values : parsed;
1058
+ const reference = referenceShape();
1059
+ if (reference !== void 0 && conflictsWith(values, reference)) {
1060
+ return false;
1061
+ }
1062
+ applyDraft(values);
1026
1063
  return true;
1027
1064
  } catch {
1028
1065
  return false;
@@ -1030,7 +1067,12 @@ var persistForm = (form, options) => {
1030
1067
  };
1031
1068
  const writeDraft = (values) => {
1032
1069
  try {
1033
- storage?.setItem(options.key, JSON.stringify(values));
1070
+ storage?.setItem(
1071
+ options.key,
1072
+ JSON.stringify(
1073
+ options.version === void 0 ? values : { __v: options.version, values }
1074
+ )
1075
+ );
1034
1076
  } catch {
1035
1077
  }
1036
1078
  };