cogsbox-state 0.5.424 → 0.5.425

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.
@@ -17,10 +17,9 @@ interface FormControlComponentProps<TStateObject> {
17
17
  stateKey: string;
18
18
  }
19
19
  export declare const FormControlComponent: <TStateObject>({ setState, path, child, formOpts, stateKey, }: FormControlComponentProps<TStateObject>) => import("react/jsx-runtime").JSX.Element;
20
- export declare function ValidationWrapper({ formOpts, path, validationKey, stateKey, children, validIndices, }: {
20
+ export declare function ValidationWrapper({ formOpts, path, stateKey, children, validIndices, }: {
21
21
  formOpts?: FormOptsType;
22
22
  path: string[];
23
- validationKey: string;
24
23
  stateKey?: string;
25
24
  children: React.ReactNode;
26
25
  validIndices?: number[];
@@ -1,13 +1,13 @@
1
1
  import { jsx as V, Fragment as M } from "react/jsx-runtime";
2
2
  import "./CogsState.jsx";
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
- import { getGlobalStore as d, formRefStore as O } from "./store.js";
3
+ import { getNestedValue as N, isFunction as O, updateNestedProperty as T } from "./utility.js";
4
+ import I, { useRef as b, useState as U, useEffect as R } from "react";
5
+ import { getGlobalStore as m, formRefStore as L } from "./store.js";
6
6
  import { validateZodPathFunc as W } from "./useValidateZodPath.js";
7
- function j(n, e, t, r) {
8
- n(
7
+ function j(r, e, t, n) {
8
+ r(
9
9
  (o) => {
10
- if (U(e)) {
10
+ if (O(e)) {
11
11
  const u = e(N(o, t));
12
12
  let i = T(t, o, u);
13
13
  return typeof i == "string" && (i = i.trim()), i;
@@ -18,19 +18,19 @@ function j(n, e, t, r) {
18
18
  },
19
19
  t,
20
20
  { updateType: "update" },
21
- r
21
+ n
22
22
  );
23
23
  }
24
- function p(n, e, t, r, o) {
25
- const u = d.getState().getNestedState(r, t);
26
- n(
24
+ function p(r, e, t, n, o) {
25
+ const u = m.getState().getNestedState(n, t);
26
+ r(
27
27
  (i) => {
28
- let s = !t || t.length == 0 ? i : N(i, [...t]), c = [...s];
29
- return c.splice(
30
- Number(o) == 0 ? o : s.length,
28
+ let c = !t || t.length == 0 ? i : N(i, [...t]), s = [...c];
29
+ return s.splice(
30
+ Number(o) == 0 ? o : c.length,
31
31
  0,
32
- U(e) ? e(s) : e
33
- ), t.length == 0 ? c : T([...t], i, c);
32
+ O(e) ? e(c) : e
33
+ ), t.length == 0 ? s : T([...t], i, s);
34
34
  },
35
35
  [
36
36
  ...t,
@@ -41,98 +41,98 @@ function p(n, e, t, r, o) {
41
41
  }
42
42
  );
43
43
  }
44
- function ee(n, e, t, r) {
45
- const o = d.getState().getNestedState(t, e);
46
- n(
44
+ function ee(r, e, t, n) {
45
+ const o = m.getState().getNestedState(t, e);
46
+ r(
47
47
  (u) => {
48
48
  const i = N(u, [...e]);
49
- if (r < 0 || r >= i?.length)
50
- throw new Error(`Index ${r} does not exist in the array.`);
51
- const s = r || Number(r) == 0 ? r : i.length - 1, c = [
52
- ...i.slice(0, s),
53
- ...i.slice(s + 1)
49
+ if (n < 0 || n >= i?.length)
50
+ throw new Error(`Index ${n} does not exist in the array.`);
51
+ const c = n || Number(n) == 0 ? n : i.length - 1, s = [
52
+ ...i.slice(0, c),
53
+ ...i.slice(c + 1)
54
54
  ];
55
- return e.length == 0 ? c : T([...e], u, c);
55
+ return e.length == 0 ? s : T([...e], u, s);
56
56
  },
57
57
  [
58
58
  ...e,
59
- r || r === 0 ? r?.toString() : (o.length - 1).toString()
59
+ n || n === 0 ? n?.toString() : (o.length - 1).toString()
60
60
  ],
61
61
  { updateType: "cut" }
62
62
  );
63
63
  }
64
- const w = (n, e, t = (r, o) => JSON.stringify(r) === JSON.stringify(o)) => {
65
- const [r, o] = z(
66
- () => e(d.getState(), n)
67
- ), u = b(r), i = b(n);
64
+ const w = (r, e, t = (n, o) => JSON.stringify(n) === JSON.stringify(o)) => {
65
+ const [n, o] = U(
66
+ () => e(m.getState(), r)
67
+ ), u = b(n), i = b(r);
68
68
  return R(() => {
69
- i.current = n, o(e(d.getState(), n));
70
- const s = (m) => {
71
- const g = e(m, i.current);
69
+ i.current = r, o(e(m.getState(), r));
70
+ const c = (f) => {
71
+ const g = e(f, i.current);
72
72
  t(u.current, g) || (u.current = g, o(g));
73
- }, c = d.subscribe(s);
73
+ }, s = m.subscribe(c);
74
74
  return () => {
75
- c();
75
+ s();
76
76
  };
77
- }, [n]), r;
78
- }, Z = (n, e, t) => {
79
- const r = n + "." + (e.length > 0 ? [e.join(".")] : []) + (t && t.length > 0 ? "." + t : "");
77
+ }, [r]), n;
78
+ }, Z = (r, e, t) => {
79
+ const n = r + "." + (e.length > 0 ? [e.join(".")] : []) + (t && t.length > 0 ? "." + t : "");
80
80
  return w(
81
- r,
81
+ n,
82
82
  (u, i) => u.getValidationErrors(i) || []
83
83
  );
84
- }, q = (n, e) => {
85
- const t = `${n}:${e.join(".")}`;
84
+ }, q = (r, e) => {
85
+ const t = `${r}:${e.join(".")}`;
86
86
  return w(
87
87
  t,
88
- (r, o) => r.getSyncInfo(o)
88
+ (n, o) => n.getSyncInfo(o)
89
89
  );
90
- }, H = (n, e) => w(
91
- `${n}:${e.join(".")}`,
92
- (t, r) => t.getNestedState(n, e)
90
+ }, H = (r, e) => w(
91
+ `${r}:${e.join(".")}`,
92
+ (t, n) => t.getNestedState(r, e)
93
93
  ), te = ({
94
- setState: n,
94
+ setState: r,
95
95
  // This is the real effectiveSetState from the hook
96
96
  path: e,
97
97
  child: t,
98
- formOpts: r,
98
+ formOpts: n,
99
99
  stateKey: o
100
100
  }) => {
101
- const { registerFormRef: u, getFormRef: i } = O.getState(), {
102
- getValidationErrors: s,
103
- addValidationError: c,
104
- getInitialOptions: m,
101
+ const { registerFormRef: u, getFormRef: i } = L.getState(), {
102
+ getValidationErrors: c,
103
+ addValidationError: s,
104
+ getInitialOptions: f,
105
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);
106
+ } = m.getState(), C = o + "." + e.join("."), $ = b(null), k = i(C);
107
+ k || u(C, $);
108
+ const z = k || $, v = H(o, e), [E, B] = U(v), S = b(!1), l = b(null);
109
109
  R(() => {
110
- !S.current && y !== E && B(y);
111
- }, [y]), R(() => () => {
110
+ !S.current && v !== E && B(v);
111
+ }, [v]), R(() => () => {
112
112
  l.current && (clearTimeout(l.current), l.current = null, S.current = !1);
113
113
  }, []);
114
114
  const G = (a) => {
115
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;
116
+ l.current && (clearTimeout(l.current), l.current = null), j(r, a, e, d), S.current = !1;
117
117
  return;
118
118
  }
119
119
  l.current && clearTimeout(l.current), l.current = setTimeout(
120
120
  () => {
121
- S.current = !1, j(n, a, e, f);
121
+ S.current = !1, j(r, a, e, d);
122
122
  },
123
- r?.debounceTime ?? (typeof y == "boolean" ? 20 : 200)
123
+ n?.debounceTime ?? (typeof v == "boolean" ? 20 : 200)
124
124
  );
125
- }, v = m(o);
126
- if (!v?.validation?.key)
125
+ }, y = f(o);
126
+ if (!y?.validation?.key)
127
127
  throw new Error("Validation key not found.");
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("."));
128
+ const d = y.validation.key, A = y.validation.onBlur === !0, D = async () => {
129
+ if (l.current && (clearTimeout(l.current), l.current = null, S.current = !1, j(r, E, e, d)), !(!y.validation?.zodSchema || !A)) {
130
+ g(d + "." + e.join("."));
131
131
  try {
132
- const a = d.getState().getNestedState(o, e);
132
+ const a = m.getState().getNestedState(o, e);
133
133
  await W(
134
- f,
135
- v.validation.zodSchema,
134
+ d,
135
+ y.validation.zodSchema,
136
136
  e,
137
137
  a
138
138
  );
@@ -140,18 +140,18 @@ const w = (n, e, t = (r, o) => JSON.stringify(r) === JSON.stringify(o)) => {
140
140
  console.error("Validation error on blur:", a);
141
141
  }
142
142
  }
143
- }, F = q(o, e), P = F ? { ...F, date: new Date(F.timeStamp) } : null, L = t({
143
+ }, F = q(o, e), J = F ? { ...F, date: new Date(F.timeStamp) } : null, P = t({
144
144
  // --- START CHANGES ---
145
145
  get: () => E,
146
146
  // Get should return the immediate local value
147
147
  set: G,
148
148
  // Use the new debounced updater
149
149
  // --- END CHANGES ---
150
- syncStatus: P,
150
+ syncStatus: J,
151
151
  path: e,
152
- validationErrors: () => s(f + "." + e.join(".")),
152
+ validationErrors: () => c(d + "." + e.join(".")),
153
153
  addValidationError: (a) => {
154
- g(f + "." + e.join(".")), c(f + "." + e.join("."), a ?? "");
154
+ g(d + "." + e.join(".")), s(d + "." + e.join("."), a ?? "");
155
155
  },
156
156
  inputProps: {
157
157
  // --- START CHANGES ---
@@ -160,37 +160,34 @@ const w = (n, e, t = (r, o) => JSON.stringify(r) === JSON.stringify(o)) => {
160
160
  onChange: (a) => G(a.target.value),
161
161
  // Use debounced updater
162
162
  // --- END CHANGES ---
163
- onBlur: J,
164
- ref: A
163
+ onBlur: D,
164
+ ref: z
165
165
  }
166
166
  });
167
- return /* @__PURE__ */ V(M, { children: /* @__PURE__ */ V(Q, { formOpts: r, path: e, validationKey: f, stateKey: o, children: L }) });
167
+ return /* @__PURE__ */ V(M, { children: /* @__PURE__ */ V(Q, { formOpts: n, path: e, stateKey: o, children: P }) });
168
168
  };
169
169
  function Q({
170
- formOpts: n,
170
+ formOpts: r,
171
171
  path: e,
172
- validationKey: t,
173
- stateKey: r,
174
- children: o,
175
- validIndices: u
172
+ stateKey: t,
173
+ children: n,
174
+ validIndices: o
176
175
  }) {
177
- const { getInitialOptions: i } = d.getState(), s = Z(
178
- t,
176
+ const { getInitialOptions: u } = m.getState(), i = u(t), c = i?.validation?.key ?? t, s = Z(
177
+ c,
179
178
  e,
180
- u
181
- ), c = [];
179
+ o
180
+ ), f = [];
182
181
  if (s) {
183
182
  const g = s.join(", ");
184
- c.includes(g) || c.push(g);
183
+ f.includes(g) || f.push(g);
185
184
  }
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()),
185
+ return /* @__PURE__ */ V(M, { children: i?.formElements?.validation && !r?.validation?.disable ? i.formElements.validation({
186
+ children: /* @__PURE__ */ V(I.Fragment, { children: n }, e.toString()),
189
187
  active: s.length > 0,
190
- message: n?.validation?.hideMessage ? "" : n?.validation?.message ? n?.validation?.message : c.map((g) => g).join(", "),
191
- path: e,
192
- ...n?.key && { key: n?.key }
193
- }) : /* @__PURE__ */ V(I.Fragment, { children: o }, e.toString()) });
188
+ message: r?.validation?.hideMessage ? "" : r?.validation?.message ? r?.validation?.message : f.map((g) => g).join(", "),
189
+ path: e
190
+ }) : /* @__PURE__ */ V(I.Fragment, { children: n }, e.toString()) });
194
191
  }
195
192
  export {
196
193
  te as FormControlComponent,
@@ -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 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,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G;AACD,QAAM,EAAE,mBAAAa,EAAA,IAAsBpC,EAAe,SAAS,GAEhD+D,IAAmBzC;AAAA,IACvBjC;AAAA,IACAD;AAAA,IACAmC;AAAA,EACF,GAQMyC,IAAyB,CAAC;AAEhC,MAAID,GAAkB;AACd,UAAAE,IAAaF,EAAkB,KAAK,IAAI;AAC9C,IAAKC,EAAa,SAASC,CAAU,KACnCD,EAAa,KAAKC,CAAU;AAAA,EAC9B;AAEI,QAAAC,IAAgB9B,EAAkBvC,CAAS;AAG/C,SAAA,gBAAA8D,EAAAC,GAAA,EACG,UAAeM,GAAA,cAAc,cAC9B,CAACpC,GAAU,YAAY,UACrBoC,EAAc,aAAc,WAAY;AAAA,IACtC,4BACGC,EAAM,UAAN,EAAsC,UAAAL,KAAlB1E,EAAK,UAAsB;AAAA,IAElD,QAAQ2E,EAAiB,SAAS;AAAA,IAClC,SAASjC,GAAU,YAAY,cAC3B,KACAA,GAAU,YAAY,UACpBA,GAAU,YAAY,UACtBkC,EAAa,IAAI,CAACI,MAAMA,CAAC,EAAE,KAAK,IAAI;AAAA,IAC1C,MAAAhF;AAAA,IAEA,GAAI0C,GAAU,OAAO,EAAE,KAAKA,GAAU,IAAI;AAAA,EAAA,CAC3C,IAED,gBAAA6B,EAACQ,EAAM,UAAN,EAAsC,UAAAL,EAAlB,GAAA1E,EAAK,SAAsB,CAAA,GAEpD;AAEJ;"}
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 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, 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\r\n stateKey,\r\n children,\r\n validIndices,\r\n}: {\r\n formOpts?: FormOptsType;\r\n path: string[];\r\n\r\n stateKey?: string;\r\n children: React.ReactNode;\r\n validIndices?: number[];\r\n}) {\r\n const { getInitialOptions } = getGlobalStore.getState();\r\n const thisStateOpts = getInitialOptions(stateKey!);\r\n const validationKey = thisStateOpts?.validation?.key ?? stateKey!;\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\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 ) : (\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","thisStateOpts","validationErrors","thesMessages","newMessage","React","m"],"mappings":";;;;;;AAeO,SAASA,EACdC,GACAC,GACAC,GACAC,GACM;AACN,EAAAH;AAAA,IACE,CAACI,MAAc;AACT,UAAAC,E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AA,EAChC,UAAA/B;AAAA,EACA,MAAA1C;AAAA,EAEA,UAAAS;AAAA,EACA,UAAAiE;AAAA,EACA,cAAAvC;AACF,GAOG;AACD,QAAM,EAAE,mBAAAa,EAAA,IAAsBpC,EAAe,SAAS,GAChD+D,IAAgB3B,EAAkBvC,CAAS,GAC3CR,IAAgB0E,GAAe,YAAY,OAAOlE,GAClDmE,IAAmB1C;AAAA,IACvBjC;AAAA,IACAD;AAAA,IACAmC;AAAA,EACF,GAQM0C,IAAyB,CAAC;AAEhC,MAAID,GAAkB;AACd,UAAAE,IAAaF,EAAkB,KAAK,IAAI;AAC9C,IAAKC,EAAa,SAASC,CAAU,KACnCD,EAAa,KAAKC,CAAU;AAAA,EAC9B;AAIA,SAAA,gBAAAP,EAAAC,GAAA,EACG,UAAeG,GAAA,cAAc,cAC9B,CAACjC,GAAU,YAAY,UACrBiC,EAAc,aAAc,WAAY;AAAA,IACtC,4BACGI,EAAM,UAAN,EAAsC,UAAAL,KAAlB1E,EAAK,UAAsB;AAAA,IAElD,QAAQ4E,EAAiB,SAAS;AAAA,IAClC,SAASlC,GAAU,YAAY,cAC3B,KACAA,GAAU,YAAY,UACpBA,GAAU,YAAY,UACtBmC,EAAa,IAAI,CAACG,MAAMA,CAAC,EAAE,KAAK,IAAI;AAAA,IAC1C,MAAAhF;AAAA,EAAA,CACD,IAED,gBAAAuE,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.424",
3
+ "version": "0.5.425",
4
4
  "description": "React state management library with form controls and server sync",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
package/src/CogsState.tsx CHANGED
@@ -229,7 +229,6 @@ export type ArrayEndType<TShape extends unknown> = {
229
229
  } & EndType<TShape>;
230
230
 
231
231
  export type FormOptsType = {
232
- key?: string;
233
232
  validation?: {
234
233
  hideMessage?: boolean;
235
234
  message?: string;
@@ -237,9 +236,7 @@ export type FormOptsType = {
237
236
  props?: GenericObject;
238
237
  disable?: boolean;
239
238
  };
240
- formElements?: boolean;
241
239
  debounceTime?: number;
242
- stateServerDifferences?: string[][];
243
240
  };
244
241
 
245
242
  export type FormControl<T> = (obj: FormElementParams<T>) => JSX.Element;
@@ -2169,7 +2166,8 @@ function createProxyHandler<T>(
2169
2166
  index: { localIndex: number; originalIndex: number },
2170
2167
  array: T,
2171
2168
  arraySetter: StateObject<T>
2172
- ) => any
2169
+ ) => any,
2170
+ formOpts?: FormOptsType
2173
2171
  ) => {
2174
2172
  const arrayToMap = getGlobalStore
2175
2173
  .getState()
@@ -2196,6 +2194,7 @@ function createProxyHandler<T>(
2196
2194
  key: originalIndex,
2197
2195
  stateKey,
2198
2196
  itemComponentId,
2197
+ formOpts,
2199
2198
  itemPath: finalPath,
2200
2199
  children: callbackfn(
2201
2200
  item,
@@ -2732,10 +2731,6 @@ function createProxyHandler<T>(
2732
2731
  hideMessage ? { validation: { message: "" } } : undefined
2733
2732
  }
2734
2733
  path={path}
2735
- validationKey={
2736
- getGlobalStore.getState().getInitialOptions(stateKey)
2737
- ?.validation?.key || ""
2738
- }
2739
2734
  stateKey={stateKey}
2740
2735
  validIndices={meta?.validIndices}
2741
2736
  >
@@ -2952,11 +2947,13 @@ function CogsItemWrapper({
2952
2947
  stateKey,
2953
2948
  itemComponentId,
2954
2949
  itemPath,
2950
+ formOpts,
2955
2951
  children,
2956
2952
  }: {
2957
2953
  stateKey: string;
2958
2954
  itemComponentId: string;
2959
2955
  itemPath: string[];
2956
+ formOpts?: FormOptsType;
2960
2957
  children: React.ReactNode;
2961
2958
  }) {
2962
2959
  // This hook handles the re-rendering when the item's own data changes.
@@ -3022,5 +3019,9 @@ function CogsItemWrapper({
3022
3019
  }, [stateKey, itemComponentId, itemPath.join(".")]);
3023
3020
 
3024
3021
  // The rendered output is a simple div that gets measured.
3025
- return <div ref={setRefs}>{children}</div>;
3022
+ return (
3023
+ <ValidationWrapper {...{ formOpts, path: itemPath, stateKey }}>
3024
+ {children}
3025
+ </ValidationWrapper>
3026
+ );
3026
3027
  }
package/src/Functions.tsx CHANGED
@@ -338,7 +338,7 @@ export const FormControlComponent = <TStateObject,>({
338
338
 
339
339
  return (
340
340
  <>
341
- <ValidationWrapper {...{ formOpts, path, validationKey, stateKey }}>
341
+ <ValidationWrapper {...{ formOpts, path, stateKey }}>
342
342
  {childElement}
343
343
  </ValidationWrapper>
344
344
  </>
@@ -347,20 +347,21 @@ export const FormControlComponent = <TStateObject,>({
347
347
  export function ValidationWrapper({
348
348
  formOpts,
349
349
  path,
350
- validationKey,
350
+
351
351
  stateKey,
352
352
  children,
353
353
  validIndices,
354
354
  }: {
355
355
  formOpts?: FormOptsType;
356
356
  path: string[];
357
- validationKey: string;
357
+
358
358
  stateKey?: string;
359
359
  children: React.ReactNode;
360
360
  validIndices?: number[];
361
361
  }) {
362
362
  const { getInitialOptions } = getGlobalStore.getState();
363
-
363
+ const thisStateOpts = getInitialOptions(stateKey!);
364
+ const validationKey = thisStateOpts?.validation?.key ?? stateKey!;
364
365
  const validationErrors = useGetValidationErrors(
365
366
  validationKey,
366
367
  path,
@@ -381,7 +382,6 @@ export function ValidationWrapper({
381
382
  thesMessages.push(newMessage);
382
383
  }
383
384
  }
384
- const thisStateOpts = getInitialOptions(stateKey!);
385
385
 
386
386
  return (
387
387
  <>
@@ -398,8 +398,6 @@ export function ValidationWrapper({
398
398
  ? formOpts?.validation?.message
399
399
  : thesMessages.map((m) => m).join(", "),
400
400
  path: path,
401
-
402
- ...(formOpts?.key && { key: formOpts?.key }),
403
401
  })
404
402
  ) : (
405
403
  <React.Fragment key={path.toString()}>{children}</React.Fragment>