cogsbox-state 0.5.127 → 0.5.128

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,19 +1,19 @@
1
1
  import { jsx as V, Fragment as M } from "react/jsx-runtime";
2
2
  import "./CogsState.jsx";
3
- import { getNestedValue as R, isFunction as U, updateNestedProperty as F } from "./utility.js";
4
- import G, { useRef as b, useState as z, useEffect as j } from "react";
3
+ import { getNestedValue as N, isFunction as U, updateNestedProperty as T } from "./utility.js";
4
+ import I, { useRef as b, useState as z, useEffect as R } from "react";
5
5
  import { getGlobalStore as d, formRefStore as O } from "./store.js";
6
6
  import { validateZodPathFunc as W } from "./useValidateZodPath.js";
7
- function I(n, e, t, r) {
7
+ function j(n, e, t, r) {
8
8
  n(
9
9
  (o) => {
10
10
  if (U(e)) {
11
- const s = e(R(o, t));
12
- let i = F(t, o, s);
11
+ const u = e(N(o, t));
12
+ let i = T(t, o, u);
13
13
  return typeof i == "string" && (i = i.trim()), i;
14
14
  } else {
15
- let s = !t || t.length == 0 ? e : F(t, o, e);
16
- return typeof s == "string" && (s = s.trim()), s;
15
+ let u = !t || t.length == 0 ? e : T(t, o, e);
16
+ return typeof u == "string" && (u = u.trim()), u;
17
17
  }
18
18
  },
19
19
  t,
@@ -22,19 +22,19 @@ function I(n, e, t, r) {
22
22
  );
23
23
  }
24
24
  function p(n, e, t, r, o) {
25
- const s = d.getState().getNestedState(r, t);
25
+ const u = d.getState().getNestedState(r, t);
26
26
  n(
27
27
  (i) => {
28
- let u = !t || t.length == 0 ? i : R(i, [...t]), c = [...u];
28
+ let s = !t || t.length == 0 ? i : N(i, [...t]), c = [...s];
29
29
  return c.splice(
30
- Number(o) == 0 ? o : u.length,
30
+ Number(o) == 0 ? o : s.length,
31
31
  0,
32
- U(e) ? e(u) : e
33
- ), t.length == 0 ? c : F([...t], i, c);
32
+ U(e) ? e(s) : e
33
+ ), t.length == 0 ? c : T([...t], i, c);
34
34
  },
35
35
  [
36
36
  ...t,
37
- (s.length - 1).toString()
37
+ (u.length - 1).toString()
38
38
  ],
39
39
  {
40
40
  updateType: "insert"
@@ -44,15 +44,15 @@ function p(n, e, t, r, o) {
44
44
  function ee(n, e, t, r) {
45
45
  const o = d.getState().getNestedState(t, e);
46
46
  n(
47
- (s) => {
48
- const i = R(s, [...e]);
47
+ (u) => {
48
+ const i = N(u, [...e]);
49
49
  if (r < 0 || r >= i?.length)
50
50
  throw new Error(`Index ${r} does not exist in the array.`);
51
- const u = r || Number(r) == 0 ? r : i.length - 1, c = [
52
- ...i.slice(0, u),
53
- ...i.slice(u + 1)
51
+ const s = r || Number(r) == 0 ? r : i.length - 1, c = [
52
+ ...i.slice(0, s),
53
+ ...i.slice(s + 1)
54
54
  ];
55
- return e.length == 0 ? c : F([...e], s, c);
55
+ return e.length == 0 ? c : T([...e], u, c);
56
56
  },
57
57
  [
58
58
  ...e,
@@ -61,33 +61,33 @@ function ee(n, e, t, r) {
61
61
  { updateType: "cut" }
62
62
  );
63
63
  }
64
- const N = (n, e, t = (r, o) => JSON.stringify(r) === JSON.stringify(o)) => {
64
+ const w = (n, e, t = (r, o) => JSON.stringify(r) === JSON.stringify(o)) => {
65
65
  const [r, o] = z(
66
66
  () => e(d.getState(), n)
67
- ), s = b(r), i = b(n);
68
- return j(() => {
67
+ ), u = b(r), i = b(n);
68
+ return R(() => {
69
69
  i.current = n, o(e(d.getState(), n));
70
- const u = (f) => {
71
- const l = e(f, i.current);
72
- t(s.current, l) || (s.current = l, o(l));
73
- }, c = d.subscribe(u);
70
+ const s = (m) => {
71
+ const g = e(m, i.current);
72
+ t(u.current, g) || (u.current = g, o(g));
73
+ }, c = d.subscribe(s);
74
74
  return () => {
75
75
  c();
76
76
  };
77
77
  }, [n]), r;
78
78
  }, Z = (n, e, t) => {
79
79
  const r = n + "." + (e.length > 0 ? [e.join(".")] : []) + (t && t.length > 0 ? "." + t : "");
80
- return N(
80
+ return w(
81
81
  r,
82
- (s, i) => s.getValidationErrors(i) || []
82
+ (u, i) => u.getValidationErrors(i) || []
83
83
  );
84
84
  }, q = (n, e) => {
85
85
  const t = `${n}:${e.join(".")}`;
86
- return N(
86
+ return w(
87
87
  t,
88
88
  (r, o) => r.getSyncInfo(o)
89
89
  );
90
- }, H = (n, e) => N(
90
+ }, H = (n, e) => w(
91
91
  `${n}:${e.join(".")}`,
92
92
  (t, r) => t.getNestedState(n, e)
93
93
  ), te = ({
@@ -98,36 +98,40 @@ const N = (n, e, t = (r, o) => JSON.stringify(r) === JSON.stringify(o)) => {
98
98
  formOpts: r,
99
99
  stateKey: o
100
100
  }) => {
101
- const { registerFormRef: s, getFormRef: i } = O.getState(), {
102
- getValidationErrors: u,
101
+ const { registerFormRef: u, getFormRef: i } = O.getState(), {
102
+ getValidationErrors: s,
103
103
  addValidationError: c,
104
- getInitialOptions: f,
105
- removeValidationError: l
106
- } = d.getState(), w = o + "." + e.join("."), k = b(null), C = i(w);
107
- C || s(w, k);
108
- const A = C || k, S = H(o, e), [E, $] = z(S), y = b(!1), m = b(null);
109
- j(() => {
110
- !y.current && S !== E && $(S);
111
- }, [S]), j(() => () => {
112
- m.current && (clearTimeout(m.current), y.current = !1);
104
+ getInitialOptions: m,
105
+ removeValidationError: g
106
+ } = d.getState(), k = o + "." + e.join("."), C = b(null), $ = i(k);
107
+ $ || u(k, C);
108
+ const A = $ || C, y = H(o, e), [E, B] = z(y), S = b(!1), l = b(null);
109
+ R(() => {
110
+ !S.current && y !== E && B(y);
111
+ }, [y]), R(() => () => {
112
+ l.current && (clearTimeout(l.current), l.current = null, S.current = !1);
113
113
  }, []);
114
- const B = (a) => {
115
- $(a), y.current = !0, m.current && clearTimeout(m.current), m.current = setTimeout(
114
+ const G = (a) => {
115
+ if (B(a), S.current = !0, a === "") {
116
+ l.current && (clearTimeout(l.current), l.current = null), j(n, a, e, f), S.current = !1;
117
+ return;
118
+ }
119
+ l.current && clearTimeout(l.current), l.current = setTimeout(
116
120
  () => {
117
- y.current = !1, I(n, a, e, g);
121
+ S.current = !1, j(n, a, e, f);
118
122
  },
119
- r?.debounceTime ?? (typeof S == "boolean" ? 20 : 200)
123
+ r?.debounceTime ?? (typeof y == "boolean" ? 20 : 200)
120
124
  );
121
- }, v = f(o);
125
+ }, v = m(o);
122
126
  if (!v?.validation?.key)
123
127
  throw new Error("Validation key not found.");
124
- const g = v.validation.key, D = v.validation.onBlur === !0, J = async () => {
125
- if (m.current && (clearTimeout(m.current), y.current = !1, I(n, E, e, g)), !(!v.validation?.zodSchema || !D)) {
126
- l(g + "." + e.join("."));
128
+ const f = v.validation.key, D = v.validation.onBlur === !0, J = async () => {
129
+ if (l.current && (clearTimeout(l.current), l.current = null, S.current = !1, j(n, E, e, f)), !(!v.validation?.zodSchema || !D)) {
130
+ g(f + "." + e.join("."));
127
131
  try {
128
132
  const a = d.getState().getNestedState(o, e);
129
133
  await W(
130
- g,
134
+ f,
131
135
  v.validation.zodSchema,
132
136
  e,
133
137
  a
@@ -136,31 +140,31 @@ const N = (n, e, t = (r, o) => JSON.stringify(r) === JSON.stringify(o)) => {
136
140
  console.error("Validation error on blur:", a);
137
141
  }
138
142
  }
139
- }, T = q(o, e), P = T ? { ...T, date: new Date(T.timeStamp) } : null, L = t({
143
+ }, F = q(o, e), P = F ? { ...F, date: new Date(F.timeStamp) } : null, L = t({
140
144
  // --- START CHANGES ---
141
145
  get: () => E,
142
146
  // Get should return the immediate local value
143
- set: B,
147
+ set: G,
144
148
  // Use the new debounced updater
145
149
  // --- END CHANGES ---
146
150
  syncStatus: P,
147
151
  path: e,
148
- validationErrors: () => u(g + "." + e.join(".")),
152
+ validationErrors: () => s(f + "." + e.join(".")),
149
153
  addValidationError: (a) => {
150
- l(g + "." + e.join(".")), c(g + "." + e.join("."), a ?? "");
154
+ g(f + "." + e.join(".")), c(f + "." + e.join("."), a ?? "");
151
155
  },
152
156
  inputProps: {
153
157
  // --- START CHANGES ---
154
158
  value: E ?? "",
155
159
  // Input value is always the local state
156
- onChange: (a) => B(a.target.value),
160
+ onChange: (a) => G(a.target.value),
157
161
  // Use debounced updater
158
162
  // --- END CHANGES ---
159
163
  onBlur: J,
160
164
  ref: A
161
165
  }
162
166
  });
163
- return /* @__PURE__ */ V(M, { children: /* @__PURE__ */ V(Q, { formOpts: r, path: e, validationKey: g, stateKey: o, children: L }) });
167
+ return /* @__PURE__ */ V(M, { children: /* @__PURE__ */ V(Q, { formOpts: r, path: e, validationKey: f, stateKey: o, children: L }) });
164
168
  };
165
169
  function Q({
166
170
  formOpts: n,
@@ -168,35 +172,35 @@ function Q({
168
172
  validationKey: t,
169
173
  stateKey: r,
170
174
  children: o,
171
- validIndices: s
175
+ validIndices: u
172
176
  }) {
173
- const { getInitialOptions: i } = d.getState(), u = Z(
177
+ const { getInitialOptions: i } = d.getState(), s = Z(
174
178
  t,
175
179
  e,
176
- s
180
+ u
177
181
  ), c = [];
178
- if (u) {
179
- const l = u.join(", ");
180
- c.includes(l) || c.push(l);
182
+ if (s) {
183
+ const g = s.join(", ");
184
+ c.includes(g) || c.push(g);
181
185
  }
182
- const f = i(r);
183
- return /* @__PURE__ */ V(M, { children: f?.formElements?.validation && !n?.validation?.disable ? f.formElements.validation({
184
- children: /* @__PURE__ */ V(G.Fragment, { children: o }, e.toString()),
185
- active: u.length > 0,
186
- message: n?.validation?.hideMessage ? "" : n?.validation?.message ? n?.validation?.message : c.map((l) => l).join(", "),
186
+ const m = i(r);
187
+ return /* @__PURE__ */ V(M, { children: m?.formElements?.validation && !n?.validation?.disable ? m.formElements.validation({
188
+ children: /* @__PURE__ */ V(I.Fragment, { children: o }, e.toString()),
189
+ active: s.length > 0,
190
+ message: n?.validation?.hideMessage ? "" : n?.validation?.message ? n?.validation?.message : c.map((g) => g).join(", "),
187
191
  path: e,
188
192
  ...n?.key && { key: n?.key }
189
- }) : /* @__PURE__ */ V(G.Fragment, { children: o }, e.toString()) });
193
+ }) : /* @__PURE__ */ V(I.Fragment, { children: o }, e.toString()) });
190
194
  }
191
195
  export {
192
196
  te as FormControlComponent,
193
197
  Q as ValidationWrapper,
194
198
  ee as cutFunc,
195
199
  p as pushFunc,
196
- I as updateFn,
200
+ j as updateFn,
197
201
  H as useGetKeyState,
198
202
  q as useGetSyncInfo,
199
203
  Z as useGetValidationErrors,
200
- N as useStoreSubscription
204
+ w as useStoreSubscription
201
205
  };
202
206
  //# sourceMappingURL=Functions.jsx.map
@@ -1 +1 @@
1
- {"version":3,"file":"Functions.jsx","sources":["../src/Functions.tsx"],"sourcesContent":["import {\r\n notifyComponent,\r\n type EffectiveSetState,\r\n type FormElementParams,\r\n type FormOptsType,\r\n type UpdateArg,\r\n type UpdateOpts,\r\n} from \"./CogsState\";\r\n\r\nimport { getNestedValue, isFunction, updateNestedProperty } from \"./utility\";\r\nimport { useEffect, useRef, useState } from \"react\";\r\nimport React from \"react\";\r\nimport { getGlobalStore, formRefStore } from \"./store\";\r\nimport { validateZodPathFunc } from \"./useValidateZodPath\";\r\n\r\nexport function updateFn<U>(\r\n setState: EffectiveSetState<U>,\r\n payload: UpdateArg<U>,\r\n path: string[],\r\n validationKey?: string\r\n): void {\r\n setState(\r\n (prevState) => {\r\n if (isFunction<U>(payload)) {\r\n const nestedValue = payload(getNestedValue(prevState, path));\r\n let value = updateNestedProperty(path, prevState, nestedValue);\r\n if (typeof value == \"string\") {\r\n value = value.trim();\r\n }\r\n return value;\r\n } else {\r\n let value =\r\n !path || path.length == 0\r\n ? payload\r\n : updateNestedProperty(path, prevState, payload);\r\n if (typeof value == \"string\") {\r\n value = value.trim();\r\n }\r\n return value;\r\n }\r\n },\r\n path,\r\n { updateType: \"update\" },\r\n validationKey\r\n );\r\n}\r\n\r\nexport function pushFunc<U>(\r\n setState: EffectiveSetState<U>,\r\n payload: UpdateArg<U>,\r\n path: string[],\r\n stateKey: string,\r\n index?: number\r\n): void {\r\n const array = getGlobalStore.getState().getNestedState(stateKey, path) as U[];\r\n setState(\r\n (prevState) => {\r\n let arrayToUpdate =\r\n !path || path.length == 0\r\n ? prevState\r\n : getNestedValue(prevState, [...path]);\r\n let returnedArray = [...arrayToUpdate];\r\n\r\n returnedArray.splice(\r\n index || Number(index) == 0 ? index : arrayToUpdate.length,\r\n 0,\r\n isFunction<U>(payload)\r\n ? payload(index == -1 ? undefined : arrayToUpdate)\r\n : payload\r\n );\r\n const value =\r\n path.length == 0\r\n ? returnedArray\r\n : updateNestedProperty([...path], prevState, returnedArray);\r\n\r\n return value as U;\r\n },\r\n [\r\n ...path,\r\n index || index === 0 ? index?.toString() : (array!.length - 1).toString(),\r\n ],\r\n {\r\n updateType: \"insert\",\r\n }\r\n );\r\n}\r\n\r\nexport function cutFunc<U>(\r\n setState: EffectiveSetState<U>,\r\n path: string[],\r\n stateKey: string,\r\n index: number\r\n): void {\r\n const array = getGlobalStore.getState().getNestedState(stateKey, path) as U[];\r\n setState(\r\n (prevState) => {\r\n const arrayToUpdate = getNestedValue(prevState, [...path]);\r\n if (index < 0 || index >= arrayToUpdate?.length) {\r\n throw new Error(`Index ${index} does not exist in the array.`);\r\n }\r\n const indexToCut =\r\n index || Number(index) == 0 ? index : arrayToUpdate.length - 1;\r\n\r\n const updatedArray = [\r\n ...arrayToUpdate.slice(0, indexToCut),\r\n ...arrayToUpdate.slice(indexToCut + 1),\r\n ] as U;\r\n\r\n return path.length == 0\r\n ? updatedArray\r\n : updateNestedProperty([...path], prevState, updatedArray);\r\n },\r\n [\r\n ...path,\r\n index || index === 0 ? index?.toString() : (array!.length - 1).toString(),\r\n ],\r\n { updateType: \"cut\" }\r\n );\r\n}\r\n\r\nexport const useStoreSubscription = <T,>(\r\n fullPath: string,\r\n selector: (\r\n store: ReturnType<typeof getGlobalStore.getState>,\r\n path: string\r\n ) => T,\r\n compare: (a: T, b: T) => boolean = (a, b) =>\r\n JSON.stringify(a) === JSON.stringify(b)\r\n) => {\r\n const [value, setValue] = useState<T>(() =>\r\n selector(getGlobalStore.getState(), fullPath)\r\n );\r\n const previousValueRef = useRef<T>(value);\r\n const fullPathRef = useRef(fullPath);\r\n useEffect(() => {\r\n fullPathRef.current = fullPath; // Ensure latest fullPath is always used\r\n\r\n setValue(selector(getGlobalStore.getState(), fullPath));\r\n\r\n const callback = (store: any) => {\r\n const newValue = selector(store, fullPathRef.current);\r\n\r\n if (!compare(previousValueRef.current, newValue)) {\r\n previousValueRef.current = newValue;\r\n setValue(newValue);\r\n }\r\n };\r\n const unsubscribe = getGlobalStore.subscribe(callback);\r\n return () => {\r\n unsubscribe();\r\n };\r\n }, [fullPath]);\r\n return value;\r\n};\r\nexport const useGetValidationErrors = (\r\n validationKey: string,\r\n path: string[],\r\n validIndices?: number[]\r\n) => {\r\n const fullPath =\r\n validationKey +\r\n \".\" +\r\n (path.length > 0 ? [path.join(\".\")] : []) +\r\n (validIndices && validIndices.length > 0 ? \".\" + validIndices : \"\");\r\n\r\n const returnresult = useStoreSubscription(\r\n fullPath,\r\n (store, path) => store.getValidationErrors(path) || []\r\n );\r\n\r\n return returnresult;\r\n};\r\n\r\nexport const useGetSyncInfo = (key: string, path: string[]) => {\r\n const syncKey = `${key}:${path.join(\".\")}`;\r\n return useStoreSubscription(syncKey, (store, path) =>\r\n store.getSyncInfo(path)\r\n );\r\n};\r\nexport const useGetKeyState = (key: string, path: string[]) => {\r\n return useStoreSubscription(`${key}:${path.join(\".\")}`, (store, fullPath) =>\r\n store.getNestedState(key, path)\r\n );\r\n};\r\ninterface FormControlComponentProps<TStateObject> {\r\n setState: EffectiveSetState<TStateObject>;\r\n\r\n path: string[];\r\n child: (obj: FormElementParams<TStateObject>) => JSX.Element;\r\n formOpts?: FormOptsType;\r\n stateKey: string;\r\n}\r\n// Find FormControlComponent in your Functions.ts or equivalent file\r\n\r\nexport const FormControlComponent = <TStateObject,>({\r\n setState, // This is the real effectiveSetState from the hook\r\n path,\r\n child,\r\n formOpts,\r\n stateKey,\r\n}: FormControlComponentProps<TStateObject>) => {\r\n const { registerFormRef, getFormRef } = formRefStore.getState();\r\n const {\r\n getValidationErrors,\r\n addValidationError,\r\n getInitialOptions,\r\n removeValidationError,\r\n } = getGlobalStore.getState();\r\n\r\n const refKey = stateKey + \".\" + path.join(\".\");\r\n const localFormRef = useRef<HTMLInputElement>(null);\r\n const existingRef = getFormRef(refKey);\r\n if (!existingRef) {\r\n registerFormRef(refKey, localFormRef);\r\n }\r\n const formRef = existingRef || localFormRef;\r\n\r\n // --- START CHANGES ---\r\n\r\n const globalStateValue = useGetKeyState(stateKey, path);\r\n const [localValue, setLocalValue] = useState<any>(globalStateValue);\r\n const isCurrentlyDebouncing = useRef(false);\r\n const debounceTimeoutRef = useRef<NodeJS.Timeout | null>(null);\r\n\r\n // Effect to sync local state if global state changes externally\r\n useEffect(() => {\r\n // Only update local if not actively debouncing a local change\r\n if (!isCurrentlyDebouncing.current && globalStateValue !== localValue) {\r\n setLocalValue(globalStateValue);\r\n }\r\n }, [globalStateValue]); // Removed localValue dependency\r\n\r\n // Effect for cleanup\r\n useEffect(() => {\r\n return () => {\r\n if (debounceTimeoutRef.current) {\r\n clearTimeout(debounceTimeoutRef.current);\r\n isCurrentlyDebouncing.current = false;\r\n }\r\n };\r\n }, []);\r\n\r\n const debouncedUpdater = (payload: UpdateArg<TStateObject>) => {\r\n setLocalValue(payload); // Update local state immediately\r\n isCurrentlyDebouncing.current = true; // Mark as debouncing\r\n\r\n if (debounceTimeoutRef.current) {\r\n clearTimeout(debounceTimeoutRef.current);\r\n }\r\n\r\n debounceTimeoutRef.current = setTimeout(\r\n () => {\r\n isCurrentlyDebouncing.current = false; // Debounce finished\r\n // Use the state value AT THE TIME OF THE SETTIMEOUT FIRING\r\n // which `payload` captured via closure is correct.\r\n updateFn(setState, payload, path, validationKey);\r\n },\r\n formOpts?.debounceTime ??\r\n (typeof globalStateValue == \"boolean\" ? 20 : 200)\r\n );\r\n };\r\n\r\n // --- END CHANGES ---\r\n\r\n const initialOptions = getInitialOptions(stateKey);\r\n if (!initialOptions?.validation?.key) {\r\n throw new Error(\"Validation key not found.\");\r\n }\r\n const validationKey = initialOptions.validation.key;\r\n const validateOnBlur = initialOptions.validation.onBlur === true;\r\n\r\n const handleBlur = async () => {\r\n // --- Ensure latest value is flushed if debouncing ---\r\n if (debounceTimeoutRef.current) {\r\n clearTimeout(debounceTimeoutRef.current);\r\n isCurrentlyDebouncing.current = false;\r\n updateFn(setState, localValue, path, validationKey); // Use current localValue\r\n }\r\n // --- End modification ---\r\n\r\n if (!initialOptions.validation?.zodSchema || !validateOnBlur) return;\r\n removeValidationError(validationKey + \".\" + path.join(\".\"));\r\n try {\r\n // Use the potentially just flushed value\r\n const fieldValue = getGlobalStore\r\n .getState()\r\n .getNestedState(stateKey, path);\r\n await validateZodPathFunc(\r\n validationKey,\r\n initialOptions.validation.zodSchema,\r\n path,\r\n fieldValue\r\n );\r\n // forceUpdate might be needed if validation state update doesn't trigger render\r\n // Consider using useGetValidationErrors hook result directly for validation display\r\n } catch (error) {\r\n console.error(\"Validation error on blur:\", error);\r\n }\r\n };\r\n\r\n const rawSyncStatus = useGetSyncInfo(stateKey, path);\r\n const syncStatus = rawSyncStatus\r\n ? { ...rawSyncStatus, date: new Date(rawSyncStatus.timeStamp) }\r\n : null;\r\n\r\n const childElement = child({\r\n // --- START CHANGES ---\r\n get: () => localValue, // Get should return the immediate local value\r\n set: debouncedUpdater, // Use the new debounced updater\r\n // --- END CHANGES ---\r\n syncStatus,\r\n path: path,\r\n validationErrors: () =>\r\n getValidationErrors(validationKey + \".\" + path.join(\".\")),\r\n addValidationError: (message?: string) => {\r\n removeValidationError(validationKey + \".\" + path.join(\".\"));\r\n addValidationError(validationKey + \".\" + path.join(\".\"), message ?? \"\");\r\n },\r\n inputProps: {\r\n // --- START CHANGES ---\r\n value: localValue ?? \"\", // Input value is always the local state\r\n onChange: (e: any) => debouncedUpdater(e.target.value), // Use debounced updater\r\n // --- END CHANGES ---\r\n onBlur: handleBlur,\r\n ref: formRef,\r\n },\r\n });\r\n\r\n return (\r\n <>\r\n <ValidationWrapper {...{ formOpts, path, validationKey, stateKey }}>\r\n {childElement}\r\n </ValidationWrapper>\r\n </>\r\n );\r\n};\r\nexport function ValidationWrapper({\r\n formOpts,\r\n path,\r\n validationKey,\r\n stateKey,\r\n children,\r\n validIndices,\r\n}: {\r\n formOpts?: FormOptsType;\r\n path: string[];\r\n validationKey: string;\r\n stateKey?: string;\r\n children: React.ReactNode;\r\n validIndices?: number[];\r\n}) {\r\n const { getInitialOptions } = getGlobalStore.getState();\r\n\r\n const validationErrors = useGetValidationErrors(\r\n validationKey,\r\n path,\r\n validIndices\r\n );\r\n // console.log(\r\n // \"validationErrors ValidationWrapper\",\r\n // stateKey,\r\n // validationKey,\r\n // path,\r\n // validationErrors\r\n // );\r\n const thesMessages: string[] = [];\r\n\r\n if (validationErrors) {\r\n const newMessage = validationErrors!.join(\", \");\r\n if (!thesMessages.includes(newMessage)) {\r\n thesMessages.push(newMessage);\r\n }\r\n }\r\n const thisStateOpts = getInitialOptions(stateKey!);\r\n\r\n return (\r\n <>\r\n {thisStateOpts?.formElements?.validation &&\r\n !formOpts?.validation?.disable ? (\r\n thisStateOpts.formElements!.validation!({\r\n children: (\r\n <React.Fragment key={path.toString()}>{children}</React.Fragment>\r\n ),\r\n active: validationErrors.length > 0 ? true : false,\r\n message: formOpts?.validation?.hideMessage\r\n ? \"\"\r\n : formOpts?.validation?.message\r\n ? formOpts?.validation?.message\r\n : thesMessages.map((m) => m).join(\", \"),\r\n path: path,\r\n\r\n ...(formOpts?.key && { key: formOpts?.key }),\r\n })\r\n ) : (\r\n <React.Fragment key={path.toString()}>{children}</React.Fragment>\r\n )}\r\n </>\r\n );\r\n}\r\n"],"names":["updateFn","setState","payload","path","validationKey","prevState","isFunction","nestedValue","getNestedValue","value","updateNestedProperty","pushFunc","stateKey","index","array","getGlobalStore","arrayToUpdate","returnedArray","cutFunc","indexToCut","updatedArray","useStoreSubscription","fullPath","selector","compare","a","b","setValue","useState","previousValueRef","useRef","fullPathRef","useEffect","callback","store","newValue","unsubscribe","useGetValidationErrors","validIndices","useGetSyncInfo","key","syncKey","useGetKeyState","FormControlComponent","child","formOpts","registerFormRef","getFormRef","formRefStore","getValidationErrors","addValidationError","getInitialOptions","removeValidationError","refKey","localFormRef","existingRef","formRef","globalStateValue","localValue","setLocalValue","isCurrentlyDebouncing","debounceTimeoutRef","debouncedUpdater","initialOptions","validateOnBlur","handleBlur","fieldValue","validateZodPathFunc","error","rawSyncStatus","syncStatus","childElement","message","e","jsx","Fragment","ValidationWrapper","children","validationErrors","thesMessages","newMessage","thisStateOpts","React","m"],"mappings":";;;;;;AAeO,SAASA,EACdC,GACAC,GACAC,GACAC,GACM;AACN,EAAAH;AAAA,IACE,CAACI,MAAc;AACT,UAAAC,EAAcJ,CAAO,GAAG;AAC1B,cAAMK,IAAcL,EAAQM,EAAeH,GAAWF,CAAI,CAAC;AAC3D,YAAIM,IAAQC,EAAqBP,GAAME,GAAWE,CAAW;AACzD,eAAA,OAAOE,KAAS,aAClBA,IAAQA,EAAM,KAAK,IAEdA;AAAA,MAAA,OACF;AACD,YAAAA,IACF,CAACN,KAAQA,EAAK,UAAU,IACpBD,IACAQ,EAAqBP,GAAME,GAAWH,CAAO;AAC/C,eAAA,OAAOO,KAAS,aAClBA,IAAQA,EAAM,KAAK,IAEdA;AAAA,MAAA;AAAA,IAEX;AAAA,IACAN;AAAA,IACA,EAAE,YAAY,SAAS;AAAA,IACvBC;AAAA,EACF;AACF;AAEO,SAASO,EACdV,GACAC,GACAC,GACAS,GACAC,GACM;AACN,QAAMC,IAAQC,EAAe,SAAW,EAAA,eAAeH,GAAUT,CAAI;AACrE,EAAAF;AAAA,IACE,CAACI,MAAc;AACb,UAAIW,IACF,CAACb,KAAQA,EAAK,UAAU,IACpBE,IACAG,EAAeH,GAAW,CAAC,GAAGF,CAAI,CAAC,GACrCc,IAAgB,CAAC,GAAGD,CAAa;AAEvB,aAAAC,EAAA;AAAA,QACH,OAAOJ,CAAK,KAAK,IAAIA,IAAQG,EAAc;AAAA,QACpD;AAAA,QACAV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sources":["../src/Functions.tsx"],"sourcesContent":["import {\r\n notifyComponent,\r\n type EffectiveSetState,\r\n type FormElementParams,\r\n type FormOptsType,\r\n type UpdateArg,\r\n type UpdateOpts,\r\n} from \"./CogsState\";\r\n\r\nimport { getNestedValue, isFunction, updateNestedProperty } from \"./utility\";\r\nimport { useEffect, useRef, useState } from \"react\";\r\nimport React from \"react\";\r\nimport { getGlobalStore, formRefStore } from \"./store\";\r\nimport { validateZodPathFunc } from \"./useValidateZodPath\";\r\n\r\nexport function updateFn<U>(\r\n setState: EffectiveSetState<U>,\r\n payload: UpdateArg<U>,\r\n path: string[],\r\n validationKey?: string\r\n): void {\r\n setState(\r\n (prevState) => {\r\n if (isFunction<U>(payload)) {\r\n const nestedValue = payload(getNestedValue(prevState, path));\r\n let value = updateNestedProperty(path, prevState, nestedValue);\r\n if (typeof value == \"string\") {\r\n value = value.trim();\r\n }\r\n return value;\r\n } else {\r\n let value =\r\n !path || path.length == 0\r\n ? payload\r\n : updateNestedProperty(path, prevState, payload);\r\n if (typeof value == \"string\") {\r\n value = value.trim();\r\n }\r\n return value;\r\n }\r\n },\r\n path,\r\n { updateType: \"update\" },\r\n validationKey\r\n );\r\n}\r\n\r\nexport function pushFunc<U>(\r\n setState: EffectiveSetState<U>,\r\n payload: UpdateArg<U>,\r\n path: string[],\r\n stateKey: string,\r\n index?: number\r\n): void {\r\n const array = getGlobalStore.getState().getNestedState(stateKey, path) as U[];\r\n setState(\r\n (prevState) => {\r\n let arrayToUpdate =\r\n !path || path.length == 0\r\n ? prevState\r\n : getNestedValue(prevState, [...path]);\r\n let returnedArray = [...arrayToUpdate];\r\n\r\n returnedArray.splice(\r\n index || Number(index) == 0 ? index : arrayToUpdate.length,\r\n 0,\r\n isFunction<U>(payload)\r\n ? payload(index == -1 ? undefined : arrayToUpdate)\r\n : payload\r\n );\r\n const value =\r\n path.length == 0\r\n ? returnedArray\r\n : updateNestedProperty([...path], prevState, returnedArray);\r\n\r\n return value as U;\r\n },\r\n [\r\n ...path,\r\n index || index === 0 ? index?.toString() : (array!.length - 1).toString(),\r\n ],\r\n {\r\n updateType: \"insert\",\r\n }\r\n );\r\n}\r\n\r\nexport function cutFunc<U>(\r\n setState: EffectiveSetState<U>,\r\n path: string[],\r\n stateKey: string,\r\n index: number\r\n): void {\r\n const array = getGlobalStore.getState().getNestedState(stateKey, path) as U[];\r\n setState(\r\n (prevState) => {\r\n const arrayToUpdate = getNestedValue(prevState, [...path]);\r\n if (index < 0 || index >= arrayToUpdate?.length) {\r\n throw new Error(`Index ${index} does not exist in the array.`);\r\n }\r\n const indexToCut =\r\n index || Number(index) == 0 ? index : arrayToUpdate.length - 1;\r\n\r\n const updatedArray = [\r\n ...arrayToUpdate.slice(0, indexToCut),\r\n ...arrayToUpdate.slice(indexToCut + 1),\r\n ] as U;\r\n\r\n return path.length == 0\r\n ? updatedArray\r\n : updateNestedProperty([...path], prevState, updatedArray);\r\n },\r\n [\r\n ...path,\r\n index || index === 0 ? index?.toString() : (array!.length - 1).toString(),\r\n ],\r\n { updateType: \"cut\" }\r\n );\r\n}\r\n\r\nexport const useStoreSubscription = <T,>(\r\n fullPath: string,\r\n selector: (\r\n store: ReturnType<typeof getGlobalStore.getState>,\r\n path: string\r\n ) => T,\r\n compare: (a: T, b: T) => boolean = (a, b) =>\r\n JSON.stringify(a) === JSON.stringify(b)\r\n) => {\r\n const [value, setValue] = useState<T>(() =>\r\n selector(getGlobalStore.getState(), fullPath)\r\n );\r\n const previousValueRef = useRef<T>(value);\r\n const fullPathRef = useRef(fullPath);\r\n useEffect(() => {\r\n fullPathRef.current = fullPath; // Ensure latest fullPath is always used\r\n\r\n setValue(selector(getGlobalStore.getState(), fullPath));\r\n\r\n const callback = (store: any) => {\r\n const newValue = selector(store, fullPathRef.current);\r\n\r\n if (!compare(previousValueRef.current, newValue)) {\r\n previousValueRef.current = newValue;\r\n setValue(newValue);\r\n }\r\n };\r\n const unsubscribe = getGlobalStore.subscribe(callback);\r\n return () => {\r\n unsubscribe();\r\n };\r\n }, [fullPath]);\r\n return value;\r\n};\r\nexport const useGetValidationErrors = (\r\n validationKey: string,\r\n path: string[],\r\n validIndices?: number[]\r\n) => {\r\n const fullPath =\r\n validationKey +\r\n \".\" +\r\n (path.length > 0 ? [path.join(\".\")] : []) +\r\n (validIndices && validIndices.length > 0 ? \".\" + validIndices : \"\");\r\n\r\n const returnresult = useStoreSubscription(\r\n fullPath,\r\n (store, path) => store.getValidationErrors(path) || []\r\n );\r\n\r\n return returnresult;\r\n};\r\n\r\nexport const useGetSyncInfo = (key: string, path: string[]) => {\r\n const syncKey = `${key}:${path.join(\".\")}`;\r\n return useStoreSubscription(syncKey, (store, path) =>\r\n store.getSyncInfo(path)\r\n );\r\n};\r\nexport const useGetKeyState = (key: string, path: string[]) => {\r\n return useStoreSubscription(`${key}:${path.join(\".\")}`, (store, fullPath) =>\r\n store.getNestedState(key, path)\r\n );\r\n};\r\ninterface FormControlComponentProps<TStateObject> {\r\n setState: EffectiveSetState<TStateObject>;\r\n\r\n path: string[];\r\n child: (obj: FormElementParams<TStateObject>) => JSX.Element;\r\n formOpts?: FormOptsType;\r\n stateKey: string;\r\n}\r\n// Find FormControlComponent in your Functions.ts or equivalent file\r\n\r\nexport const FormControlComponent = <TStateObject,>({\r\n setState, // This is the real effectiveSetState from the hook\r\n path,\r\n child,\r\n formOpts,\r\n stateKey,\r\n}: FormControlComponentProps<TStateObject>) => {\r\n const { registerFormRef, getFormRef } = formRefStore.getState();\r\n const {\r\n getValidationErrors,\r\n addValidationError,\r\n getInitialOptions,\r\n removeValidationError,\r\n } = getGlobalStore.getState();\r\n\r\n const refKey = stateKey + \".\" + path.join(\".\");\r\n const localFormRef = useRef<HTMLInputElement>(null);\r\n const existingRef = getFormRef(refKey);\r\n if (!existingRef) {\r\n registerFormRef(refKey, localFormRef);\r\n }\r\n const formRef = existingRef || localFormRef;\r\n\r\n // --- START CHANGES ---\r\n\r\n const globalStateValue = useGetKeyState(stateKey, path);\r\n const [localValue, setLocalValue] = useState<any>(globalStateValue);\r\n const isCurrentlyDebouncing = useRef(false);\r\n const debounceTimeoutRef = useRef<NodeJS.Timeout | null>(null);\r\n\r\n // Effect to sync local state if global state changes externally\r\n useEffect(() => {\r\n // Only update local if not actively debouncing a local change\r\n if (!isCurrentlyDebouncing.current && globalStateValue !== localValue) {\r\n setLocalValue(globalStateValue);\r\n }\r\n }, [globalStateValue]); // Removed localValue dependency\r\n\r\n // Effect for cleanup\r\n useEffect(() => {\r\n return () => {\r\n if (debounceTimeoutRef.current) {\r\n clearTimeout(debounceTimeoutRef.current);\r\n debounceTimeoutRef.current = null; // Explicitly nullify\r\n isCurrentlyDebouncing.current = false;\r\n }\r\n };\r\n }, []);\r\n\r\n const debouncedUpdater = (payload: UpdateArg<TStateObject>) => {\r\n setLocalValue(payload); // Update local state immediately\r\n isCurrentlyDebouncing.current = true;\r\n\r\n if (payload === \"\") {\r\n if (debounceTimeoutRef.current) {\r\n clearTimeout(debounceTimeoutRef.current); // Clear pending timer\r\n debounceTimeoutRef.current = null;\r\n }\r\n updateFn(setState, payload, path, validationKey); // Update global state NOW\r\n isCurrentlyDebouncing.current = false; // No longer debouncing\r\n return; // Don't proceed to set another timeout\r\n }\r\n\r\n // If not empty, proceed with normal debouncing\r\n if (debounceTimeoutRef.current) {\r\n clearTimeout(debounceTimeoutRef.current);\r\n }\r\n\r\n debounceTimeoutRef.current = setTimeout(\r\n () => {\r\n isCurrentlyDebouncing.current = false;\r\n updateFn(setState, payload, path, validationKey);\r\n },\r\n formOpts?.debounceTime ??\r\n (typeof globalStateValue == \"boolean\" ? 20 : 200)\r\n );\r\n };\r\n\r\n const initialOptions = getInitialOptions(stateKey);\r\n if (!initialOptions?.validation?.key) {\r\n throw new Error(\"Validation key not found.\");\r\n }\r\n const validationKey = initialOptions.validation.key;\r\n const validateOnBlur = initialOptions.validation.onBlur === true;\r\n\r\n const handleBlur = async () => {\r\n // --- Ensure latest value is flushed if debouncing ---\r\n if (debounceTimeoutRef.current) {\r\n clearTimeout(debounceTimeoutRef.current); // Clear pending timer\r\n debounceTimeoutRef.current = null;\r\n isCurrentlyDebouncing.current = false;\r\n // Ensure the absolute latest local value is committed on blur\r\n updateFn(setState, localValue, path, validationKey);\r\n }\r\n // --- End modification ---\r\n\r\n if (!initialOptions.validation?.zodSchema || !validateOnBlur) return;\r\n removeValidationError(validationKey + \".\" + path.join(\".\"));\r\n try {\r\n // Use the potentially just flushed value\r\n const fieldValue = getGlobalStore\r\n .getState()\r\n .getNestedState(stateKey, path);\r\n await validateZodPathFunc(\r\n validationKey,\r\n initialOptions.validation.zodSchema,\r\n path,\r\n fieldValue\r\n );\r\n // forceUpdate might be needed if validation state update doesn't trigger render\r\n // Consider using useGetValidationErrors hook result directly for validation display\r\n } catch (error) {\r\n console.error(\"Validation error on blur:\", error);\r\n }\r\n };\r\n\r\n const rawSyncStatus = useGetSyncInfo(stateKey, path);\r\n const syncStatus = rawSyncStatus\r\n ? { ...rawSyncStatus, date: new Date(rawSyncStatus.timeStamp) }\r\n : null;\r\n\r\n const childElement = child({\r\n // --- START CHANGES ---\r\n get: () => localValue, // Get should return the immediate local value\r\n set: debouncedUpdater, // Use the new debounced updater\r\n // --- END CHANGES ---\r\n syncStatus,\r\n path: path,\r\n validationErrors: () =>\r\n getValidationErrors(validationKey + \".\" + path.join(\".\")),\r\n addValidationError: (message?: string) => {\r\n removeValidationError(validationKey + \".\" + path.join(\".\"));\r\n addValidationError(validationKey + \".\" + path.join(\".\"), message ?? \"\");\r\n },\r\n inputProps: {\r\n // --- START CHANGES ---\r\n value: localValue ?? \"\", // Input value is always the local state\r\n onChange: (e: any) => debouncedUpdater(e.target.value), // Use debounced updater\r\n // --- END CHANGES ---\r\n onBlur: handleBlur,\r\n ref: formRef,\r\n },\r\n });\r\n\r\n return (\r\n <>\r\n <ValidationWrapper {...{ formOpts, path, validationKey, stateKey }}>\r\n {childElement}\r\n </ValidationWrapper>\r\n </>\r\n );\r\n};\r\nexport function ValidationWrapper({\r\n formOpts,\r\n path,\r\n validationKey,\r\n stateKey,\r\n children,\r\n validIndices,\r\n}: {\r\n formOpts?: FormOptsType;\r\n path: string[];\r\n validationKey: string;\r\n stateKey?: string;\r\n children: React.ReactNode;\r\n validIndices?: number[];\r\n}) {\r\n const { getInitialOptions } = getGlobalStore.getState();\r\n\r\n const validationErrors = useGetValidationErrors(\r\n validationKey,\r\n path,\r\n validIndices\r\n );\r\n // console.log(\r\n // \"validationErrors ValidationWrapper\",\r\n // stateKey,\r\n // validationKey,\r\n // path,\r\n // validationErrors\r\n // );\r\n const thesMessages: string[] = [];\r\n\r\n if (validationErrors) {\r\n const newMessage = validationErrors!.join(\", \");\r\n if (!thesMessages.includes(newMessage)) {\r\n thesMessages.push(newMessage);\r\n }\r\n }\r\n const thisStateOpts = getInitialOptions(stateKey!);\r\n\r\n return (\r\n <>\r\n {thisStateOpts?.formElements?.validation &&\r\n !formOpts?.validation?.disable ? (\r\n thisStateOpts.formElements!.validation!({\r\n children: (\r\n <React.Fragment key={path.toString()}>{children}</React.Fragment>\r\n ),\r\n active: validationErrors.length > 0 ? true : false,\r\n message: formOpts?.validation?.hideMessage\r\n ? \"\"\r\n : formOpts?.validation?.message\r\n ? formOpts?.validation?.message\r\n : thesMessages.map((m) => m).join(\", \"),\r\n path: path,\r\n\r\n ...(formOpts?.key && { key: formOpts?.key }),\r\n })\r\n ) : (\r\n <React.Fragment key={path.toString()}>{children}</React.Fragment>\r\n )}\r\n </>\r\n );\r\n}\r\n"],"names":["updateFn","setState","payload","path","validationKey","prevState","isFunction","nestedValue","getNestedValue","value","updateNestedProperty","pushFunc","stateKey","index","array","getGlobalStore","arrayToUpdate","returnedArray","cutFunc","indexToCut","updatedArray","useStoreSubscription","fullPath","selector","compare","a","b","setValue","useState","previousValueRef","useRef","fullPathRef","useEffect","callback","store","newValue","unsubscribe","useGetValidationErrors","validIndices","useGetSyncInfo","key","syncKey","useGetKeyState","FormControlComponent","child","formOpts","registerFormRef","getFormRef","formRefStore","getValidationErrors","addValidationError","getInitialOptions","removeValidationError","refKey","localFormRef","existingRef","formRef","globalStateValue","localValue","setLocalValue","isCurrentlyDebouncing","debounceTimeoutRef","debouncedUpdater","initialOptions","validateOnBlur","handleBlur","fieldValue","validateZodPathFunc","error","rawSyncStatus","syncStatus","childElement","message","e","jsx","Fragment","ValidationWrapper","children","validationErrors","thesMessages","newMessage","thisStateOpts","React","m"],"mappings":";;;;;;AAeO,SAASA,EACdC,GACAC,GACAC,GACAC,GACM;AACN,EAAAH;AAAA,IACE,CAACI,MAAc;AACT,UAAAC,EAAcJ,CAAO,GAAG;AAC1B,cAAMK,IAAcL,EAAQM,EAAeH,GAAWF,CAAI,CAAC;AAC3D,YAAIM,IAAQC,EAAqBP,GAAME,GAAWE,CAAW;AACzD,eAAA,OAAOE,KAAS,aAClBA,IAAQA,EAAM,KAAK,IAEdA;AAAA,MAAA,OACF;AACD,YAAAA,IACF,CAACN,KAAQA,EAAK,UAAU,IACpBD,IACAQ,EAAqBP,GAAME,GAAWH,CAAO;AAC/C,eAAA,OAAOO,KAAS,aAClBA,IAAQA,EAAM,KAAK,IAEdA;AAAA,MAAA;AAAA,IAEX;AAAA,IACAN;AAAA,IACA,EAAE,YAAY,SAAS;AAAA,IACvBC;AAAA,EACF;AACF;AAEO,SAASO,EACdV,GACAC,GACAC,GACAS,GACAC,GACM;AACN,QAAMC,IAAQC,EAAe,SAAW,EAAA,eAAeH,GAAUT,CAAI;AACrE,EAAAF;AAAA,IACE,CAACI,MAAc;AACb,UAAIW,IACF,CAACb,KAAQA,EAAK,UAAU,IACpBE,IACAG,EAAeH,GAAW,CAAC,GAAGF,CAAI,CAAC,GACrCc,IAAgB,CAAC,GAAGD,CAAa;AAEvB,aAAAC,EAAA;AAAA,QACH,OAAOJ,CAAK,KAAK,IAAIA,IAAQG,EAAc;AAAA,QACpD;AAAA,QACAV,EAAcJ,CAAO,IACjBA,EAAkCc,CAAa,IAC/Cd;AAAA,MACN,GAEEC,EAA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IAAI;AAAA,EAAA,CAC3C,IAED,gBAAA6B,EAACQ,EAAM,UAAN,EAAsC,UAAAL,EAAlB,GAAA1E,EAAK,SAAsB,CAAA,GAEpD;AAEJ;"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cogsbox-state",
3
- "version": "0.5.127",
3
+ "version": "0.5.128",
4
4
  "description": "React state management library with form controls and server sync",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",