cogsbox-state 0.5.4 → 0.5.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/CogsState.d.ts +30 -13
- package/dist/CogsState.jsx +534 -470
- package/dist/CogsState.jsx.map +1 -1
- package/dist/Functions.d.ts +3 -4
- package/dist/Functions.jsx +171 -126
- package/dist/Functions.jsx.map +1 -1
- package/dist/index.js +17 -16
- package/dist/store.d.ts +19 -9
- package/dist/store.js +77 -60
- package/dist/store.js.map +1 -1
- package/dist/useValidateZodPath.d.ts +34 -0
- package/dist/useValidateZodPath.js +60 -0
- package/dist/useValidateZodPath.js.map +1 -0
- package/dist/utility.d.ts +6 -0
- package/dist/utility.js +90 -80
- package/dist/utility.js.map +1 -1
- package/package.json +68 -65
package/dist/CogsState.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { GenericObject } from './utility.js';
|
|
2
2
|
import { UseMutationResult } from '@tanstack/react-query';
|
|
3
|
-
import { ZodObject, ZodRawShape } from 'zod';
|
|
3
|
+
import { ZodArray, ZodObject, ZodRawShape } from 'zod';
|
|
4
|
+
import { ComponentsType } from './store.js';
|
|
4
5
|
|
|
5
6
|
type Prettify<T> = {
|
|
6
7
|
[K in keyof T]: T[K];
|
|
@@ -17,7 +18,7 @@ export type SyncInfo = {
|
|
|
17
18
|
timeStamp: number;
|
|
18
19
|
userId: number;
|
|
19
20
|
};
|
|
20
|
-
export type
|
|
21
|
+
export type FormElementParams<T> = {
|
|
21
22
|
get: () => T;
|
|
22
23
|
set: UpdateType<T>;
|
|
23
24
|
syncStatus: (SyncInfo & {
|
|
@@ -25,9 +26,12 @@ export type FormElementParmas<T> = {
|
|
|
25
26
|
}) | null;
|
|
26
27
|
path: string[];
|
|
27
28
|
validationErrors: () => string[];
|
|
29
|
+
addValidationError: (message?: string) => void;
|
|
28
30
|
inputProps: {
|
|
29
|
-
|
|
31
|
+
ref?: React.RefObject<any>;
|
|
32
|
+
value?: T extends boolean ? never : T;
|
|
30
33
|
onChange?: (event: React.ChangeEvent<HTMLInputElement | HTMLTextAreaElement>) => void;
|
|
34
|
+
onBlur?: () => void;
|
|
31
35
|
};
|
|
32
36
|
};
|
|
33
37
|
export type StateKeys = string;
|
|
@@ -46,10 +50,11 @@ export type ArrayEndType<TShape extends unknown> = {
|
|
|
46
50
|
} & EndType<InferArrayElement<TShape>>;
|
|
47
51
|
insert: PushArgs<InferArrayElement<TShape>>;
|
|
48
52
|
cut: CutFunctionType;
|
|
53
|
+
stateMapNoRender: (callbackfn: (value: InferArrayElement<TShape>, setter: StateObject<InferArrayElement<TShape>>, index: number, array: TShape, arraySetter: StateObject<TShape>) => void) => any;
|
|
49
54
|
stateMap: (callbackfn: (value: InferArrayElement<TShape>, setter: StateObject<InferArrayElement<TShape>>, index: number, array: TShape, arraySetter: StateObject<TShape>) => void) => any;
|
|
50
55
|
$stateMap: (callbackfn: (value: InferArrayElement<TShape>, setter: StateObject<InferArrayElement<TShape>>, index: number, array: TShape, arraySetter: StateObject<TShape>) => void) => any;
|
|
51
56
|
stateFlattenOn: <K extends keyof InferArrayElement<TShape>>(field: K) => StateObject<InferArrayElement<InferArrayElement<TShape>[K]>[]>;
|
|
52
|
-
uniqueInsert: (payload: UpdateArg<InferArrayElement<TShape>>, fields?: (keyof InferArrayElement<TShape>)[]) => void;
|
|
57
|
+
uniqueInsert: (payload: UpdateArg<InferArrayElement<TShape>>, fields?: (keyof InferArrayElement<TShape>)[], onMatch?: (existingItem: any) => any) => void;
|
|
53
58
|
stateFilter: (callbackfn: (value: InferArrayElement<TShape>, index: number) => void) => ArrayEndType<TShape>;
|
|
54
59
|
getSelected: () => StateObject<InferArrayElement<TShape>> | undefined;
|
|
55
60
|
} & EndType<TShape> & {
|
|
@@ -59,6 +64,7 @@ export type UpdateType<T> = (payload: UpdateArg<Prettify<T>>, opts?: UpdateOpts<
|
|
|
59
64
|
export type FormOptsType = {
|
|
60
65
|
key?: string;
|
|
61
66
|
validation?: {
|
|
67
|
+
hideMessage?: boolean;
|
|
62
68
|
message?: string;
|
|
63
69
|
stretch?: boolean;
|
|
64
70
|
props?: GenericObject;
|
|
@@ -68,10 +74,11 @@ export type FormOptsType = {
|
|
|
68
74
|
debounceTime?: number;
|
|
69
75
|
stateServerDifferences?: string[][];
|
|
70
76
|
};
|
|
71
|
-
export type FormControl<T> = (obj:
|
|
77
|
+
export type FormControl<T> = (obj: FormElementParams<T>) => JSX.Element;
|
|
72
78
|
export type UpdateArg<S> = S | ((prevState: S) => S);
|
|
73
79
|
export type UpdateOpts<T> = {
|
|
74
80
|
afterUpdate?: (state: T) => void;
|
|
81
|
+
debounce?: number;
|
|
75
82
|
};
|
|
76
83
|
export type ObjectEndType<T> = EndType<T> & {
|
|
77
84
|
[K in keyof T]-?: ObjectEndType<T[K]>;
|
|
@@ -84,17 +91,18 @@ export type EndType<T, IsArrayElement = false> = {
|
|
|
84
91
|
update: UpdateType<T>;
|
|
85
92
|
_path: string[];
|
|
86
93
|
_stateKey: string;
|
|
87
|
-
formElement: (
|
|
94
|
+
formElement: (control: FormControl<T>, opts?: FormOptsType) => JSX.Element;
|
|
88
95
|
get: () => T;
|
|
89
96
|
$get: () => T;
|
|
90
|
-
$
|
|
97
|
+
$derive: <R>(fn: EffectFunction<T, R>) => R;
|
|
91
98
|
_status: "fresh" | "stale" | "synced";
|
|
92
|
-
showValidationErrors: (
|
|
99
|
+
showValidationErrors: () => string[];
|
|
93
100
|
setValidation: (ctx: string) => void;
|
|
94
101
|
removeValidation: (ctx: string) => void;
|
|
95
102
|
ignoreFields: (fields: string[]) => StateObject<T>;
|
|
96
103
|
_selected: boolean;
|
|
97
104
|
setSelected: (value: boolean) => void;
|
|
105
|
+
getFormRef: () => React.RefObject<any> | undefined;
|
|
98
106
|
validationWrapper: ({ children, hideMessage, }: {
|
|
99
107
|
children: React.ReactNode;
|
|
100
108
|
hideMessage?: boolean;
|
|
@@ -108,7 +116,10 @@ export type EndType<T, IsArrayElement = false> = {
|
|
|
108
116
|
export type StateObject<T> = (T extends any[] ? ArrayEndType<T> : T extends Record<string, unknown> | object ? {
|
|
109
117
|
[K in keyof T]-?: StateObject<T[K]>;
|
|
110
118
|
} & ObjectEndType<T> : T extends string | number | boolean | null ? T : never) & EndType<T, true> & {
|
|
119
|
+
getAllFormRefs: () => Map<string, React.RefObject<any>>;
|
|
111
120
|
_componentId: string | null;
|
|
121
|
+
getComponents: () => ComponentsType;
|
|
122
|
+
validateZodSchema: () => void;
|
|
112
123
|
_initialState: T;
|
|
113
124
|
updateInitialState: (newState: T | null) => {
|
|
114
125
|
fetchId: (field: keyof T) => string | number;
|
|
@@ -155,15 +166,20 @@ type CookieType<T> = {
|
|
|
155
166
|
OnUnMountCookie?: Boolean;
|
|
156
167
|
};
|
|
157
168
|
export type CogsCookiesType<T extends string[] = string[]> = CookieType<ArrayToObject<T>>;
|
|
169
|
+
export type ReactivityType = "none" | "component" | "deps" | "all";
|
|
158
170
|
export type OptionsType<T extends unknown = unknown> = {
|
|
171
|
+
componentId?: string;
|
|
159
172
|
serverSync?: ServerSyncType<T>;
|
|
160
|
-
|
|
173
|
+
validation?: {
|
|
174
|
+
key?: string;
|
|
175
|
+
zodSchema?: ZodObject<ZodRawShape> | ZodArray<ZodObject<ZodRawShape>>;
|
|
176
|
+
onBlur?: boolean;
|
|
177
|
+
};
|
|
161
178
|
enableServerState?: boolean;
|
|
162
179
|
middleware?: ({ updateLog, update, }: {
|
|
163
180
|
updateLog: UpdateTypeDetail[] | undefined;
|
|
164
181
|
update: UpdateTypeDetail;
|
|
165
182
|
}) => void;
|
|
166
|
-
zodSchema?: ZodObject<ZodRawShape>;
|
|
167
183
|
modifyState?: (state: T) => T;
|
|
168
184
|
localStorage?: {
|
|
169
185
|
key: string | ((state: T) => string);
|
|
@@ -171,9 +187,10 @@ export type OptionsType<T extends unknown = unknown> = {
|
|
|
171
187
|
formElements?: FormsElementsType;
|
|
172
188
|
enabledSync?: (state: T) => boolean;
|
|
173
189
|
reactiveDeps?: (state: T) => any[] | true;
|
|
190
|
+
reactiveType?: ReactivityType[] | ReactivityType;
|
|
174
191
|
syncUpdate?: Partial<UpdateTypeDetail>;
|
|
192
|
+
localStorageKey?: string;
|
|
175
193
|
initState?: {
|
|
176
|
-
localStorageKey?: string;
|
|
177
194
|
ctx?: Record<string, any>;
|
|
178
195
|
initialState: T;
|
|
179
196
|
dependencies?: any[];
|
|
@@ -248,7 +265,6 @@ export type CogsInitialState<T> = {
|
|
|
248
265
|
export type TransformedStateType<T> = {
|
|
249
266
|
[P in keyof T]: T[P] extends CogsInitialState<infer U> ? U : T[P];
|
|
250
267
|
};
|
|
251
|
-
export declare function addStateOptions<T extends unknown>(initialState: T, { formElements, zodSchema }: OptionsType<T>): T;
|
|
252
268
|
export declare const createCogsState: <State extends Record<string, unknown>>(initialState: State, opts?: {
|
|
253
269
|
reRenderType?: "get" | "state" | "none";
|
|
254
270
|
}) => {
|
|
@@ -261,7 +277,8 @@ type LocalStorageData<T> = {
|
|
|
261
277
|
lastSyncedWithServer?: number;
|
|
262
278
|
baseServerState?: T;
|
|
263
279
|
};
|
|
264
|
-
export declare
|
|
280
|
+
export declare const notifyComponent: (stateKey: string, componentId: string) => void;
|
|
281
|
+
export declare function useCogsStateFn<TStateObject extends unknown>(stateObject: TStateObject, { stateKey, serverSync, localStorage, formElements, middleware, reactiveDeps, reactiveType, componentId, localStorageKey, initState, syncUpdate, }?: {
|
|
265
282
|
stateKey?: string;
|
|
266
283
|
componentId?: string;
|
|
267
284
|
} & OptionsType<TStateObject>): [TStateObject, StateObject<TStateObject>];
|